// File: /workspace/github_c_makefile_star2_O1/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 0x5eb43
leaq 0x95ac3(%rip), %rcx     # 0xf43b8
movslq (%rcx,%rax,4), %rdi
addq %rcx, %rdi
jmpq *%rdi
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x95b84(%rip), %rdx     # 0xf4494
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq $-0x1000000, %rax       # imm = 0xFF000000
orq 0x8(%rsi), %rax
movq %rax, 0x8(%rbx)
movl $0x1, (%rbx)
jmp 0x5ec00
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x95ac7(%rip), %rdx     # 0xf440c
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl 0x8(%rsi), %eax
notl %eax
jmp 0x5eab3
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x95ada(%rip), %rdx     # 0xf4444
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movl 0x8(%rsi), %eax
movl %eax, %ecx
shrl $0x8, %ecx
movl $0xff, %edx
andl %edx, %ecx
cvtsi2sd %ecx, %xmm2
divsd 0x959da(%rip), %xmm2    # 0xf4368
movl %eax, %ecx
shrl $0x10, %ecx
andl %edx, %ecx
cvtsi2sd %ecx, %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
jne 0x5edc6
jp 0x5edc6
movapd %xmm1, %xmm2
movapd %xmm1, %xmm3
movapd %xmm1, %xmm4
cmpltsd %xmm0, %xmm4
movsd 0x959ca(%rip), %xmm5    # 0xf4390
movapd %xmm4, %xmm6
andpd %xmm5, %xmm6
movsd 0x959c2(%rip), %xmm7    # 0xf4398
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 0x5eb34
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x959dd(%rip), %rdx     # 0xf4428
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
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 0x5eb34
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x959d2(%rip), %rdx     # 0xf4478
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movq 0x8(%rsi), %rax
andl $0xffffff, %eax         # imm = 0xFFFFFF
jmp 0x5eb34
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x95924(%rip), %rdx     # 0xf43f0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
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 0x5eb34
cmpl $0x6, %edx
ja 0x5eb43
movl %edx, %ecx
leaq 0x958bd(%rip), %rdx     # 0xf43d4
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
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 0x5ec00
movq 0x8(%rsi), %rcx
movq %rcx, 0x8(%rbx)
movl %eax, (%rbx)
jmp 0x5ec00
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x6, %edx
jmp 0x5ebfb
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3, %edx
jmp 0x5ebfb
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x5, %edx
jmp 0x5ebfb
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x4, %edx
jmp 0x5ebfb
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x2, %edx
jmp 0x5ebfb
leaq 0x8(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x57d10
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %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
movq %rax, 0x8(%rbx)
movl $0x4, (%rbx)
jmp 0x5ec00
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 0x5ec8e
jnp 0x5ef3d
ucomisd %xmm4, %xmm0
jne 0x5ee73
jp 0x5ee73
subl %ecx, %edx
xorps %xmm4, %xmm4
cvtsi2sd %edx, %xmm4
divsd %xmm3, %xmm4
jmp 0x5ef10
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
imulq $0x55555556, %rsi, %rcx # imm = 0x55555556
shrq $0x20, %rcx
cvtsi2sd %ecx, %xmm0
addsd 0x956b5(%rip), %xmm0    # 0xf4398
cvttsd2si %xmm0, %rcx
shll $0x10, %edx
shll $0x8, %eax
orl %edx, %eax
orl %ecx, %eax
cltq
movq %rax, 0x8(%rbx)
movl $0x3, (%rbx)
jmp 0x5ec00
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 0x95639(%rip), %xmm3    # 0xf4368
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
movq %rax, 0x8(%rbx)
movl $0x5, (%rbx)
jmp 0x5ec00
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
imulq $0x55555556, %rax, %rax # imm = 0x55555556
shrq $0x20, %rax
movq %rax, 0x8(%rbx)
movl $0x6, (%rbx)
jmp 0x5ec00
movzbl %al, %eax
cvtsi2sd %eax, %xmm3
divsd 0x95593(%rip), %xmm3    # 0xf4368
movsd 0x95593(%rip), %xmm4    # 0xf4370
mulsd %xmm4, %xmm3
ucomisd %xmm4, %xmm3
jb 0x5edf9
movsd 0x95589(%rip), %xmm5    # 0xf4378
addsd %xmm5, %xmm3
ucomisd %xmm4, %xmm3
jae 0x5edef
divsd 0x9557f(%rip), %xmm3    # 0xf4380
cvttsd2si %xmm3, %eax
cmpl $0x5, %eax
ja 0x5eefb
cvttpd2dq %xmm3, %xmm4
cvtdq2pd %xmm4, %xmm4
subsd %xmm4, %xmm3
movsd 0x95566(%rip), %xmm5    # 0xf4388
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 0x95603(%rip), %rcx     # 0xf4460
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movapd %xmm4, %xmm2
movapd %xmm5, %xmm3
jmp 0x5e9b5
ucomisd %xmm5, %xmm0
jne 0x5ee92
jp 0x5ee92
subl %eax, %ecx
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
divsd %xmm3, %xmm4
addsd 0x95518(%rip), %xmm4    # 0xf43a8
jmp 0x5ef10
ucomisd %xmm6, %xmm0
jne 0x5ef0c
jp 0x5ef0c
subl %edx, %eax
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
divsd %xmm3, %xmm4
addsd 0x954f1(%rip), %xmm4    # 0xf43a0
jmp 0x5ef10
movapd %xmm1, %xmm2
movapd %xmm4, %xmm3
movapd %xmm5, %xmm1
jmp 0x5e9b5
movapd %xmm5, %xmm2
movapd %xmm1, %xmm3
jmp 0x5eed4
movapd %xmm1, %xmm2
movapd %xmm6, %xmm3
movapd %xmm4, %xmm1
jmp 0x5e9b5
movapd %xmm4, %xmm2
movapd %xmm1, %xmm3
movapd %xmm6, %xmm1
jmp 0x5e9b5
movapd %xmm6, %xmm2
movapd %xmm4, %xmm3
jmp 0x5e9b5
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
xorpd %xmm1, %xmm1
jmp 0x5e9b5
xorpd %xmm4, %xmm4
mulsd 0x95468(%rip), %xmm4    # 0xf4380
movsd 0x95450(%rip), %xmm3    # 0xf4370
addsd %xmm4, %xmm3
xorpd %xmm5, %xmm5
movapd %xmm4, %xmm7
cmpltsd %xmm5, %xmm7
andpd %xmm7, %xmm3
andnpd %xmm4, %xmm7
orpd %xmm3, %xmm7
divsd 0x9542b(%rip), %xmm7    # 0xf4370
movsd 0x9541b(%rip), %xmm3    # 0xf4368
mulsd %xmm3, %xmm7
mulsd %xmm2, %xmm3
movapd %xmm7, %xmm2
cmpltsd %xmm1, %xmm2
movsd 0x9542a(%rip), %xmm4    # 0xf4390
movapd %xmm2, %xmm5
andpd %xmm4, %xmm5
movsd 0x95422(%rip), %xmm6    # 0xf4398
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 0x5ec00
nop