RGB To YCbCr

This algorithm converts RGB color model to YCbCr 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 YCbCr
{
public:
	float Y;
	float Cb;
	float Cr;

	YCbCr(float y, float cb, float cr)
	{
		Y = y;
		Cb = cb;
		Cr = cr;
	}

	bool Equals(YCbCr ycbcr)
	{
		return (Y == ycbcr.Y) && (Cb == ycbcr.Cb) && (Cr == ycbcr.Cr);
	}
};

static YCbCr RGBToYCbCr(RGB rgb) {
	float fr = (float)rgb.R / 255;
	float fg = (float)rgb.G / 255;
	float fb = (float)rgb.B / 255;

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

	return YCbCr(Y, Cb, Cr);
}
								


Example

									RGB data = RGB(82, 0, 87);
YCbCr value = RGBToYCbCr(data);
								


Output

									Y: 0.135181576
Cb: 0.116339609
Cr: 0.132944316