RGB To CMYK

This algorithm converts RGB color model to CMYK color model.



									class CMYK
{
	public $C;
	public $M;
	public $Y;
	public $K;
}

class RGB
{
	public $R;
	public $G;
	public $B;
}

function RGBToCMYK($rgb) {
	$cmyk = new CMYK();

	$dr = (double)$rgb->R / 255;
	$dg = (double)$rgb->G / 255;
	$db = (double)$rgb->B / 255;
	$cmyk->K = 1 - max(max($dr, $dg), $db);
	$cmyk->C = (1 - $dr - $cmyk->K) / (1 - $cmyk->K);
	$cmyk->M = (1 - $dg - $cmyk->K) / (1 - $cmyk->K);
	$cmyk->Y = (1 - $db - $cmyk->K) / (1 - $cmyk->K);

	return $cmyk;
}
								


Example

									$data = new RGB();
$data->R = 125;
$data->G = 29;
$data->B = 107;
$value = RGBToCMYK($data);
								


Output

									C: 0
M: 0.768
Y: 0.144
K: 0.50980392156863