RGB To CMYK

This algorithm converts RGB color model to CMYK color model.



									class CMYK
{
public:
	double C;
	double M;
	double Y;
	double K;

	CMYK(double c, double m, double y, double k)
	{
		C = c;
		M = m;
		Y = y;
		K = k;
	}

	bool Equals(CMYK cmyk)
	{
		return (C == cmyk.C) && (M == cmyk.M) && (Y == cmyk.Y) && (K == cmyk.K);
	}
};

class RGB
{
public:
	unsigned char R;
	unsigned char G;
	unsigned char B;

	RGB(unsigned char r, unsigned char g, unsigned char b)
	{
		R = r;
		G = g;
		B = b;
	}

	bool Equals(RGB rgb)
	{
		return (R == rgb.R) && (G == rgb.G) && (B == rgb.B);
	}
};

static double Max(double a, double b) {
	return a >= b ? a : b;
}

static CMYK RGBToCMYK(RGB rgb) {
	double dr = (double)rgb.R / 255;
	double dg = (double)rgb.G / 255;
	double db = (double)rgb.B / 255;
	double k = 1 - Max(Max(dr, dg), db);
	double c = (1 - dr - k) / (1 - k);
	double m = (1 - dg - k) / (1 - k);
	double y = (1 - db - k) / (1 - k);

	return CMYK(c, m, y, k);
}
								


Example

									RGB data = RGB(125, 29, 107);
CMYK value = RGBToCMYK(data);
								


Output

									C: 0
M: 0.7679999999999999
Y: 0.14399999999999993
K: 0.50980392156862742