YCbCr To RGB

This algorithm converts YCbCr color space to RGB color model.



									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 float Min(float a, float b) {
	return a <= b ? a : b;
}

static float Max(float a, float b) {
	return a >= b ? a : b;
}

static RGB YCbCrToRGB(YCbCr ycbcr) {
	float r = Max(0.0f, Min(1.0f, (float)(ycbcr.Y + 0.0000 * ycbcr.Cb + 1.4022 * ycbcr.Cr)));
	float g = Max(0.0f, Min(1.0f, (float)(ycbcr.Y - 0.3456 * ycbcr.Cb - 0.7145 * ycbcr.Cr)));
	float b = Max(0.0f, Min(1.0f, (float)(ycbcr.Y + 1.7710 * ycbcr.Cb + 0.0000 * ycbcr.Cr)));

	return RGB((unsigned char)(r * 255), (unsigned char)(g * 255), (unsigned char)(b * 255));
}
								


Example

									YCbCr data = YCbCr(0.28f, 0.21f, 0.34f);
RGB value = YCbCrToRGB(data);
								


Output

									R: 192
G: 0
B: 166