RGB To YUV

This algorithm converts RGB color model to YUV color space.



									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);
	}
};

class YUV
{
public:
	double Y;
	double U;
	double V;

	YUV(double y, double u, double v)
	{
		Y = y;
		U = u;
		V = v;
	}

	bool Equals(YUV yuv)
	{
		return (Y == yuv.Y) && (U == yuv.U) && (V == yuv.V);
	}
};

static YUV RGBToYUV(RGB rgb) {
	double y = rgb.R * .299000 + rgb.G * .587000 + rgb.B * .114000;
	double u = rgb.R * -.168736 + rgb.G * -.331264 + rgb.B * .500000 + 128;
	double v = rgb.R * .500000 + rgb.G * -.418688 + rgb.B * -.081312 + 128;

	return YUV(y, u, v);
}
								


Example

									RGB data = RGB(82, 0, 87);
YUV value = RGBToYUV(data);
								


Output

									Y: 34.436
U: 157.663648
V: 161.925856