RGB To YUV

This algorithm converts RGB color model to YUV color space.



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

class YUV
{
	public $Y;
	public $U;
	public $V;
}

function RGBToYUV($rgb) {
	$yuv = new YUV();
	$yuv->Y = $rgb->R * .299000 + $rgb->G * .587000 + $rgb->B * .114000;
	$yuv->U = $rgb->R * -.168736 + $rgb->G * -.331264 + $rgb->B * .500000 + 128;
	$yuv->V = $rgb->R * .500000 + $rgb->G * -.418688 + $rgb->B * -.081312 + 128;

	return $yuv;
}
								


Example

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

$value = RGBToYUV($data);
								


Output

									Y: 34.436
U: 157.663648
V: 161.925856