RGB To YCbCr

This algorithm converts RGB color model to YCbCr color space.



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

class YCbCr
{
	public $Y;
	public $Cb;
	public $Cr;
}

function RGBToYCbCr($rgb) {
	$fr = (float)$rgb->R / 255;
	$fg = (float)$rgb->G / 255;
	$fb = (float)$rgb->B / 255;

	$ycbcr = new YCbCr();
	$ycbcr->Y = (float)(0.2989 * $fr + 0.5866 * $fg + 0.1145 * $fb);
	$ycbcr->Cb = (float)(-0.1687 * $fr - 0.3313 * $fg + 0.5000 * $fb);
	$ycbcr->Cr = (float)(0.5000 * $fr - 0.4184 * $fg - 0.0816 * $fb);

	return $ycbcr;
}
								


Example

									$data = new RGB();
$data->R = 82;
$data->G = 0;
$data->B = 87;

$value = RGBToYCbCr($data);
								


Output

									Y: 0.13518156862745
Cb: 0.11633960784314
Cr: 0.13294431372549