// File: /workspace/github_c_makefile_star2_O3/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 0x5eaf6
leaq 0x94aff(%rip), %rcx     # 0xf33b8
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmpq *%rdi
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x94bc0(%rip), %rcx     # 0xf3494
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsi), %rax
jmp 0x5eae7
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x94b14(%rip), %rcx     # 0xf340c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0x8(%rsi), %eax
notl %eax
jmp 0x5ea66
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x94b27(%rip), %rcx     # 0xf3444
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 0x94a27(%rip), %xmm2    # 0xf3368
movl %eax, %ecx
shrl $0x10, %ecx
andl %edx, %ecx
cvtsi2sd %ecx, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x5edbc
jp 0x5edbc
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
movapd %xmm1, %xmm4
cmpltsd %xmm0, %xmm4
movsd 0x94a17(%rip), %xmm5    # 0xf3390
movapd %xmm4, %xmm6
andpd %xmm5, %xmm6
movsd 0x94a0f(%rip), %xmm7    # 0xf3398
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 0x5eae7
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x94a2a(%rip), %rcx     # 0xf3428
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 0x5eae7
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x94a1f(%rip), %rcx     # 0xf3478
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rsi), %rax
andl $0xffffff, %eax         # imm = 0xFFFFFF
jmp 0x5eae7
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x94971(%rip), %rcx     # 0xf33f0
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 0x5eae7
cmpl $0x6, %edx
ja 0x5eaf6
movl %edx, %eax
leaq 0x9490a(%rip), %rcx     # 0xf33d4
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)
movl $0x0, (%rbx)
jmp 0x5ebb3
movq 0x8(%rsi), %rcx
movq %rcx, 0x8(%rbx)
movl %eax, (%rbx)
jmp 0x5ebb3
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
jmp 0x5ebae
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x5, %edx
jmp 0x5ebae
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x4, %edx
jmp 0x5ebae
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x6, %edx
jmp 0x5ebae
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
jmp 0x5ebae
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3, %edx
callq 0x57d10
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r14
retq
movl 0x8(%rsi), %eax
notl %eax
andl $0xffffff, %eax         # imm = 0xFFFFFF
jmp 0x5ec60
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 0x5ec3a
jnp 0x5ef33
ucomisd %xmm4, %xmm0
jne 0x5ee69
jp 0x5ee69
subl %ecx, %edx
xorps %xmm4, %xmm4
cvtsi2sd %edx, %xmm4
divsd %xmm3, %xmm4
jmp 0x5ef06
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x4, (%rbx)
jmp 0x5ebb3
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 %eax, %esi
imull $0x5556, %esi, %ecx     # imm = 0x5556
shrl $0x10, %ecx
cvtsi2sd %ecx, %xmm0
addsd 0x946f8(%rip), %xmm0    # 0xf3398
cvttsd2si %xmm0, %rcx
shll $0x10, %edx
shll $0x8, %eax
orl %edx, %eax
orl %ecx, %eax
cltq
jmp 0x5ece0
movq $-0x1000000, %rax       # imm = 0xFF000000
orq 0x8(%rsi), %rax
jmp 0x5ecc4
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x1, (%rbx)
jmp 0x5ebb3
movq 0x8(%rsi), %rax
jmp 0x5efac
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x3, (%rbx)
jmp 0x5ebb3
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 0x9464d(%rip), %xmm3    # 0xf3368
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 0x5ed9a
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
imull $0x5556, %eax, %eax     # imm = 0x5556
shrl $0x10, %eax
jmp 0x5edad
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x5, (%rbx)
jmp 0x5ebb3
movq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x6, (%rbx)
jmp 0x5ebb3
movzbl %al, %eax
cvtsi2sd %eax, %xmm3
divsd 0x9459d(%rip), %xmm3    # 0xf3368
movsd 0x9459d(%rip), %xmm4    # 0xf3370
mulsd %xmm4, %xmm3
ucomisd %xmm4, %xmm3
jb 0x5edef
movsd 0x94593(%rip), %xmm5    # 0xf3378
addsd %xmm5, %xmm3
ucomisd %xmm4, %xmm3
jae 0x5ede5
divsd 0x94589(%rip), %xmm3    # 0xf3380
cvttsd2si %xmm3, %eax
cmpl $0x5, %eax
ja 0x5eef1
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
movsd 0x94570(%rip), %xmm5    # 0xf3388
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 0x9460d(%rip), %rcx     # 0xf3460
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movapd %xmm4, %xmm2
movapd %xmm5, %xmm3
jmp 0x5e968
ucomisd %xmm5, %xmm0
jne 0x5ee88
jp 0x5ee88
subl %eax, %ecx
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
divsd %xmm3, %xmm4
addsd 0x94522(%rip), %xmm4    # 0xf33a8
jmp 0x5ef06
ucomisd %xmm6, %xmm0
jne 0x5ef02
jp 0x5ef02
subl %edx, %eax
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
divsd %xmm3, %xmm4
addsd 0x944fb(%rip), %xmm4    # 0xf33a0
jmp 0x5ef06
movapd %xmm1, %xmm2
movapd %xmm4, %xmm3
movapd %xmm5, %xmm1
jmp 0x5e968
movapd %xmm5, %xmm2
movapd %xmm1, %xmm3
jmp 0x5eeca
movapd %xmm1, %xmm2
movapd %xmm6, %xmm3
movapd %xmm4, %xmm1
jmp 0x5e968
movapd %xmm4, %xmm2
movapd %xmm1, %xmm3
movapd %xmm6, %xmm1
jmp 0x5e968
movapd %xmm6, %xmm2
movapd %xmm4, %xmm3
jmp 0x5e968
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm1, %xmm1
jmp 0x5e968
xorpd %xmm4, %xmm4
mulsd 0x94472(%rip), %xmm4    # 0xf3380
movsd 0x9445a(%rip), %xmm3    # 0xf3370
addsd %xmm4, %xmm3
xorpd %xmm5, %xmm5
movapd %xmm4, %xmm7
cmpltsd %xmm5, %xmm7
andpd %xmm7, %xmm3
andnpd %xmm4, %xmm7
orpd %xmm3, %xmm7
divsd 0x94435(%rip), %xmm7    # 0xf3370
movsd 0x94425(%rip), %xmm3    # 0xf3368
mulsd %xmm3, %xmm7
mulsd %xmm2, %xmm3
movapd %xmm7, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0x94434(%rip), %xmm4    # 0xf3390
movapd %xmm2, %xmm5
andpd %xmm4, %xmm5
movsd 0x9442c(%rip), %xmm6    # 0xf3398
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 0x5ebb3
nop