// File: /workspace/github_c_makefile_star2_O2/enzo1982[P]smooth/classes/graphics/color.cpp
// Function: smooth::GUI::Color::ConvertTo(smooth::GUI::Color::ColorSpace) const
S::GUI::Color S::GUI::Color::ConvertTo(ColorSpace cs) const
{
	double	p1 = 0;
	double	p2 = 0;
	double	p3 = 0;
	double	p4 = 0;
	double	r;
	double	g;
	double	b;
	double	h;
	double	s;
	double	v;
	double	min;
	double	max;

	switch (colorSpace)
	{
		case RGB:
			switch (cs)
			{
				case RGB:
					return *this;
				case RGBA:
					return Color(255 << 24 | color, RGBA);
				case HSV:
					r = GetRed();
					g = GetGreen();
					b = GetBlue();

					max = Math::Max(Math::Max(r, g), b);
					min = Math::Min(Math::Min(r, g), b);

					v = max;

					if (max != 0) s = (max - min) / max;
					else	      s = 0;

					h = 0;

					if (s != 0)
					{
						double	 delta = max - min;

						if	(r == max) h =	   (g - b) / delta;
						else if (g == max) h = 2 + (b - r) / delta;
						else if (b == max) h = 4 + (r - g) / delta;

						h *= 60;

						if (h < 0) h += 360;
					}

					p1 = h / 360 * 255;
					p2 = s * 255;
					p3 = v;

					p1 = Math::Round(p1);
					p2 = Math::Round(p2);

					return Color((int) p1, (int) p2, (int) p3, HSV);
				case YUV:
					p1 = (GetRed() + GetGreen() + GetBlue()) / 3;
					p2 =  GetBlue();
					p3 =  GetRed();

					p1 = Math::Round(p1);

					return Color((int) p1, (int) p2, (int) p3, YUV);
				case CMY:
					p1 = 255 - GetRed();
					p2 = 255 - GetGreen();
					p3 = 255 - GetBlue();

					return Color((int) p1, (int) p2, (int) p3, CMY);
				case CMYK:
					r = GetRed();
					g = GetGreen();
					b = GetBlue();

					p4 = 255 - Math::Max(Math::Max(r, g), b);
					p1 = 255 - r - p4;
					p2 = 255 - g - p4;
					p3 = 255 - b - p4;

					return Color(((Int) p1) | ((Int) p2) << 8 | ((Int) p3) << 16 | ((Int) p4) << 24, CMYK);
				case GRAY:
					return Color((GetRed() + GetGreen() + GetBlue()) / 3, GRAY);
				default:
					break;
			}

			break;
		case RGBA:
			switch (cs)
			{
				case RGB:
					return Color(GetRed(), GetGreen(), GetBlue(), RGB);
				case RGBA:
					return *this;
				case HSV:
					return ConvertTo(RGB).ConvertTo(HSV);
				case YUV:
					return ConvertTo(RGB).ConvertTo(YUV);
				case CMY:
					return ConvertTo(RGB).ConvertTo(CMY);
				case CMYK:
					return ConvertTo(RGB).ConvertTo(CMYK);
				case GRAY:
					return ConvertTo(RGB).ConvertTo(GRAY);
				default:
					break;
			}

			break;
		case HSV:
			switch (cs)
			{
				case RGB:
					h = ((double) GetRed()) / 255 * 360;
					s = ((double) GetGreen()) / 255;
					v = GetBlue();

					if (s == 0)
					{
						p1 = v;
						p2 = v;
						p3 = v;
					}
					else
					{
						while (h <    0) h += 360;
						while (h >= 360) h -= 360;

						h /= 60;

						double	 f = h - (int) h;
						double	 p = v * (1 -  s	   );
						double	 q = v * (1 - (s *      f ));
						double	 t = v * (1 - (s * (1 - f)));

						switch ((int) h)
						{
							case 0:
								p1 = v;
								p2 = t;
								p3 = p;
								break;
							case 1:
								p1 = q;
								p2 = v;
								p3 = p;
								break;
							case 2:
								p1 = p;
								p2 = v;
								p3 = t;
								break;
							case 3:
								p1 = p;
								p2 = q;
								p3 = v;
								break;
							case 4:
								p1 = t;
								p2 = p;
								p3 = v;
								break;
							case 5:
								p1 = v;
								p2 = p;
								p3 = q;
								break;
							default:
								break;
						}
					}

					p1 = Math::Round(p1);
					p2 = Math::Round(p2);
					p3 = Math::Round(p3);

					return Color((int) p1, (int) p2, (int) p3, RGB);
				case RGBA:
					return ConvertTo(RGB).ConvertTo(RGBA);
				case HSV:
					return *this;
				case YUV:
					return ConvertTo(RGB).ConvertTo(YUV);
				case CMY:
					return ConvertTo(RGB).ConvertTo(CMY);
				case CMYK:
					return ConvertTo(RGB).ConvertTo(CMYK);
				case GRAY:
					return ConvertTo(RGB).ConvertTo(GRAY);
				default:
					break;
			}

			break;
		case YUV:
			switch (cs)
			{
				case RGB:
					p1 =	 GetBlue();
					p3 =	 GetGreen();
					p2 = 3 * GetRed() - p1 - p3;

					if (p2 < 0) p2 = 0;

					return Color((int) p1, (int) p2, (int) p3, RGB);
				case RGBA:
					return ConvertTo(RGB).ConvertTo(RGBA);
				case HSV:
					return ConvertTo(RGB).ConvertTo(HSV);
				case YUV:
					return *this;
				case CMY:
					return ConvertTo(RGB).ConvertTo(CMY);
				case CMYK:
					return ConvertTo(RGB).ConvertTo(CMYK);
				case GRAY:
					return ConvertTo(RGB).ConvertTo(GRAY);
				default:
					break;
			}

			break;
		case CMY:
			switch (cs)
			{
				case RGB:
					p1 = 255 - GetRed();
					p2 = 255 - GetGreen();
					p3 = 255 - GetBlue();

					return Color((int) p1, (int) p2, (int) p3, RGB);
				case RGBA:
					return ConvertTo(RGB).ConvertTo(RGBA);
				case HSV:
					return ConvertTo(RGB).ConvertTo(HSV);
				case YUV:
					return ConvertTo(RGB).ConvertTo(YUV);
				case CMY:
					return *this;
				case CMYK:
					return ConvertTo(RGB).ConvertTo(CMYK);
				case GRAY:
					return ConvertTo(RGB).ConvertTo(GRAY);
				default:
					break;
			}

			break;
		case CMYK:
			switch (cs)
			{
				case RGB:
					p4 =	   GetAlpha();
					p1 = 255 - GetRed()   - p4;
					p2 = 255 - GetGreen() - p4;
					p3 = 255 - GetBlue()  - p4;

					return Color((int) p1, (int) p2, (int) p3, RGB);
				case RGBA:
					return ConvertTo(RGB).ConvertTo(RGBA);
				case HSV:
					return ConvertTo(RGB).ConvertTo(HSV);
				case YUV:
					return ConvertTo(RGB).ConvertTo(YUV);
				case CMY:
					return ConvertTo(RGB).ConvertTo(CMY);
				case CMYK:
					return *this;
				case GRAY:
					return ConvertTo(RGB).ConvertTo(GRAY);
				default:
					break;
			}

			break;
		case GRAY:
			switch (cs)
			{
				case RGB:
					return Color(color, color, color, RGB);
				case RGBA:
					return ConvertTo(RGB).ConvertTo(RGBA);
				case HSV:
					return ConvertTo(RGB).ConvertTo(HSV);
				case YUV:
					return ConvertTo(RGB).ConvertTo(YUV);
				case CMY:
					return ConvertTo(RGB).ConvertTo(CMY);
				case CMYK:
					return ConvertTo(RGB).ConvertTo(CMYK);
				case GRAY:
					return *this;
				default:
					break;
			}

			break;
		default:
			break;
	}

	return *this;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl (%rsi), %eax
cmpq $0x6, %rax
ja 0x5fa43
leaq 0x83b93(%rip), %rcx     # 0xe33a0
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmpq *%rdi
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x83c54(%rip), %rcx     # 0xe347c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsi), %rax
jmp 0x5fa37
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x83ba8(%rip), %rcx     # 0xe33f4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x8(%rsi), %eax
notl %eax
jmp 0x5f9ba
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x83bbb(%rip), %rcx     # 0xe342c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x8(%rsi), %eax
movl %eax, %ecx
shrl $0x8, %ecx
movl $0xff, %edx
andl %edx, %ecx
cvtsi2sd %ecx, %xmm2
divsd 0x83abb(%rip), %xmm2    # 0xe3350
movl %eax, %ecx
shrl $0x10, %ecx
andl %edx, %ecx
cvtsi2sd %ecx, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x5fcd6
jp 0x5fcd6
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
movapd %xmm1, %xmm4
cmpltsd %xmm0, %xmm4
movsd 0x83aab(%rip), %xmm5    # 0xe3378
movapd %xmm4, %xmm6
andpd %xmm5, %xmm6
movsd 0x83aa3(%rip), %xmm7    # 0xe3380
andnpd %xmm7, %xmm4
orpd %xmm6, %xmm4
addsd %xmm1, %xmm4
cvttsd2si %xmm4, %rax
movapd %xmm3, %xmm1
cmpltsd %xmm0, %xmm1
movapd %xmm1, %xmm4
andpd %xmm5, %xmm4
andnpd %xmm7, %xmm1
orpd %xmm4, %xmm1
addsd %xmm3, %xmm1
cvttsd2si %xmm1, %rcx
movapd %xmm2, %xmm1
cmpltsd %xmm0, %xmm1
andpd %xmm1, %xmm5
andnpd %xmm7, %xmm1
orpd %xmm5, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %rdx
shll $0x10, %edx
shll $0x8, %ecx
orl %eax, %edx
orl %ecx, %edx
movslq %edx, %rax
jmp 0x5fa37
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x83abe(%rip), %rcx     # 0xe3410
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x8(%rsi), %eax
movl %eax, %ecx
shrl $0x10, %ecx
movl $0xff, %edx
andl %edx, %ecx
movl %eax, %esi
shrl $0x8, %esi
andl %edx, %esi
andl %edx, %eax
leal (%rax,%rax,2), %eax
subl %ecx, %eax
subl %esi, %eax
cvtsi2sd %eax, %xmm0
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
cvttsd2si %xmm1, %eax
shll $0x10, %esi
shll $0x8, %eax
orl %esi, %eax
orl %ecx, %eax
cltq
jmp 0x5fa37
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x83ab3(%rip), %rcx     # 0xe3460
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsi), %rax
andl $0xffffff, %eax         # imm = 0xFFFFFF
jmp 0x5fa37
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x83a09(%rip), %rcx     # 0xe33d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x8(%rsi), %eax
movl %eax, %ecx
shrl $0x18, %ecx
notl %eax
movl $0xff, %edx
movl %eax, %esi
andl %edx, %esi
subl %ecx, %esi
movl %eax, %edi
shrl $0x8, %edi
andl %edx, %edi
subl %ecx, %edi
shrl $0x10, %eax
andl %edx, %eax
subl %ecx, %eax
shll $0x10, %eax
shll $0x8, %edi
orl %esi, %edi
orl %eax, %edi
movslq %edi, %rax
jmp 0x5fa37
cmpl $0x6, %edx
ja 0x5fa43
movl %edx, %eax
leaq 0x839a2(%rip), %rcx     # 0xe33bc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x8(%rsi), %eax
movl %eax, %ecx
shll $0x10, %ecx
movl %eax, %edx
shll $0x8, %edx
orl %ecx, %edx
orl %eax, %edx
movslq %edx, %rax
movq %rax, 0x8(%rbx)
andl $0x0, (%rbx)
jmp 0x5facb
movq 0x8(%rsi), %rcx
movq %rcx, 0x8(%rbx)
movl %eax, (%rbx)
jmp 0x5facb
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x58f90
pushq $0x1
jmp 0x5fabf
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x58f90
pushq $0x5
jmp 0x5fabf
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x58f90
pushq $0x4
jmp 0x5fabf
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x58f90
pushq $0x6
jmp 0x5fabf
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x58f90
pushq $0x2
jmp 0x5fabf
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x58f90
pushq $0x3
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x58f90
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
retq
movl 0x8(%rsi), %eax
notl %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
jmp 0x5fb75
movl 0x8(%rsi), %ecx
movl $0xff, %esi
movl %ecx, %eax
andl %esi, %eax
cvtsi2sd %eax, %xmm4
movl %ecx, %edx
shrl $0x8, %edx
andl %esi, %edx
cvtsi2sd %edx, %xmm5
shrl $0x10, %ecx
andl %esi, %ecx
cvtsi2sd %ecx, %xmm6
movapd %xmm4, %xmm0
maxsd %xmm5, %xmm0
maxsd %xmm6, %xmm0
movapd %xmm4, %xmm1
minsd %xmm5, %xmm1
minsd %xmm6, %xmm1
movapd %xmm0, %xmm3
subsd %xmm1, %xmm3
movapd %xmm3, %xmm7
divsd %xmm0, %xmm7
xorpd %xmm1, %xmm1
movapd %xmm0, %xmm2
cmpneqsd %xmm1, %xmm2
andpd %xmm7, %xmm2
ucomisd %xmm1, %xmm2
xorpd %xmm7, %xmm7
jne 0x5fb52
jnp 0x5fe53
ucomisd %xmm4, %xmm0
jne 0x5fd8f
jp 0x5fd8f
subl %ecx, %edx
cvtsi2sd %edx, %xmm4
divsd %xmm3, %xmm4
jmp 0x5fe26
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x4, (%rbx)
jmp 0x5facb
movl 0x8(%rsi), %ecx
movl $0xff, %edx
movl %ecx, %esi
andl %edx, %esi
movl %ecx, %eax
shrl $0x8, %eax
andl %edx, %eax
addl %esi, %eax
shrl $0x10, %ecx
andl %edx, %ecx
addl %ecx, %eax
movw $0x3, %di
xorl %edx, %edx
divw %di
cwtl
cvtsi2sd %eax, %xmm0
addsd 0x837ca(%rip), %xmm0    # 0xe3380
cvttsd2si %xmm0, %rax
shll $0x10, %esi
shll $0x8, %ecx
orl %esi, %ecx
orl %eax, %ecx
movslq %ecx, %rax
jmp 0x5fbf7
movq $-0x1000000, %rax       # imm = 0xFF000000
orq 0x8(%rsi), %rax
jmp 0x5fbdb
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x1, (%rbx)
jmp 0x5facb
movq 0x8(%rsi), %rax
jmp 0x5fecc
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x3, (%rbx)
jmp 0x5facb
movl 0x8(%rsi), %eax
movl $0xff, %ecx
movl %eax, %edx
andl %ecx, %edx
cvtsi2sd %edx, %xmm0
movl %eax, %edx
shrl $0x8, %edx
andl %ecx, %edx
cvtsi2sd %edx, %xmm1
shrl $0x10, %eax
andl %ecx, %eax
cvtsi2sd %eax, %xmm2
movsd 0x8371e(%rip), %xmm3    # 0xe3350
movapd %xmm3, %xmm4
subsd %xmm0, %xmm4
maxsd %xmm1, %xmm0
maxsd %xmm2, %xmm0
movapd %xmm3, %xmm5
subsd %xmm0, %xmm5
subsd %xmm5, %xmm4
movapd %xmm3, %xmm0
subsd %xmm1, %xmm0
subsd %xmm5, %xmm0
subsd %xmm2, %xmm3
cvttsd2si %xmm4, %eax
subsd %xmm5, %xmm3
cvttsd2si %xmm0, %ecx
shll $0x8, %ecx
cvttsd2si %xmm3, %edx
cvttsd2si %xmm5, %esi
shll $0x10, %edx
shll $0x18, %esi
orl %eax, %esi
orl %ecx, %esi
orl %edx, %esi
movslq %esi, %rax
jmp 0x5fcb4
movl 0x8(%rsi), %eax
movl $0xff, %ecx
movl %eax, %edx
andl %ecx, %edx
movl %eax, %esi
shrl $0x8, %esi
andl %ecx, %esi
addl %edx, %esi
shrl $0x10, %eax
andl %ecx, %eax
addl %esi, %eax
movw $0x3, %cx
xorl %edx, %edx
divw %cx
movzwl %ax, %eax
jmp 0x5fcc7
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x5, (%rbx)
jmp 0x5facb
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x6, (%rbx)
jmp 0x5facb
movzbl %al, %eax
cvtsi2sd %eax, %xmm3
divsd 0x8366b(%rip), %xmm3    # 0xe3350
mulsd 0x8366b(%rip), %xmm3    # 0xe3358
movsd 0x83663(%rip), %xmm4    # 0xe3358
ucomisd %xmm3, %xmm0
jbe 0x5fd01
addsd %xmm4, %xmm3
jmp 0x5fcf5
movsd 0x83657(%rip), %xmm5    # 0xe3360
ucomisd %xmm4, %xmm3
jb 0x5fd15
addsd %xmm5, %xmm3
jmp 0x5fd09
divsd 0x8364b(%rip), %xmm3    # 0xe3368
cvttsd2si %xmm3, %eax
cmpl $0x5, %eax
ja 0x5fe11
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
movsd 0x83632(%rip), %xmm5    # 0xe3370
movapd %xmm5, %xmm4
subsd %xmm2, %xmm4
mulsd %xmm1, %xmm4
movapd %xmm2, %xmm7
mulsd %xmm3, %xmm7
movapd %xmm5, %xmm6
subsd %xmm7, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm5, %xmm7
subsd %xmm3, %xmm7
mulsd %xmm2, %xmm7
subsd %xmm7, %xmm5
mulsd %xmm1, %xmm5
leaq 0x836cf(%rip), %rcx     # 0xe3448
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movapd %xmm4, %xmm2
movapd %xmm5, %xmm3
jmp 0x5f8bc
ucomisd %xmm5, %xmm0
jne 0x5fdab
jp 0x5fdab
subl %eax, %ecx
cvtsi2sd %ecx, %xmm4
divsd %xmm3, %xmm4
addsd 0x835e7(%rip), %xmm4    # 0xe3390
jmp 0x5fe26
ucomisd %xmm6, %xmm0
jne 0x5fe22
jp 0x5fe22
subl %edx, %eax
cvtsi2sd %eax, %xmm4
divsd %xmm3, %xmm4
addsd 0x835c3(%rip), %xmm4    # 0xe3388
jmp 0x5fe26
movapd %xmm1, %xmm2
movapd %xmm4, %xmm3
movapd %xmm5, %xmm1
jmp 0x5f8bc
movapd %xmm5, %xmm2
movapd %xmm1, %xmm3
jmp 0x5fdea
movapd %xmm1, %xmm2
movapd %xmm6, %xmm3
movapd %xmm4, %xmm1
jmp 0x5f8bc
movapd %xmm4, %xmm2
movapd %xmm1, %xmm3
movapd %xmm6, %xmm1
jmp 0x5f8bc
movapd %xmm6, %xmm2
movapd %xmm4, %xmm3
jmp 0x5f8bc
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm1, %xmm1
jmp 0x5f8bc
xorpd %xmm4, %xmm4
mulsd 0x8353a(%rip), %xmm4    # 0xe3368
movsd 0x83522(%rip), %xmm3    # 0xe3358
addsd %xmm4, %xmm3
xorpd %xmm5, %xmm5
movapd %xmm4, %xmm7
cmpltsd %xmm5, %xmm7
andpd %xmm7, %xmm3
andnpd %xmm4, %xmm7
orpd %xmm3, %xmm7
divsd 0x834fd(%rip), %xmm7    # 0xe3358
movsd 0x834ed(%rip), %xmm3    # 0xe3350
mulsd %xmm3, %xmm7
mulsd %xmm2, %xmm3
movapd %xmm7, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0x834fc(%rip), %xmm4    # 0xe3378
movapd %xmm2, %xmm5
andpd %xmm4, %xmm5
movsd 0x834f4(%rip), %xmm6    # 0xe3380
andnpd %xmm6, %xmm2
orpd %xmm5, %xmm2
addsd %xmm7, %xmm2
cvttsd2si %xmm2, %rax
movapd %xmm3, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm2, %xmm4
andnpd %xmm6, %xmm2
orpd %xmm4, %xmm2
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %rcx
cvttsd2si %xmm0, %edx
shll $0x10, %edx
shll $0x8, %ecx
orl %eax, %edx
orl %ecx, %edx
movslq %edx, %rax
movq %rax, 0x8(%rbx)
movl $0x2, (%rbx)
jmp 0x5facb
nop