YCbCr To RGB

This algorithm converts YCbCr color space to RGB color model.



									public struct RGB
{
	private byte _r;
	private byte _g;
	private byte _b;

	public RGB(byte r, byte g, byte b)
	{
		this._r = r;
		this._g = g;
		this._b = b;
	}

	public byte R
	{
		get { return this._r; }
		set { this._r = value; }
	}

	public byte G
	{
		get { return this._g; }
		set { this._g = value; }
	}

	public byte B
	{
		get { return this._b; }
		set { this._b = value; }
	}

	public bool Equals(RGB rgb)
	{
		return (this.R == rgb.R) && (this.G == rgb.G) && (this.B == rgb.B);
	}
}

public struct YCbCr
{
	private float _y;
	private float _cb;
	private float _cr;

	public YCbCr(float y, float cb, float cr)
	{
		this._y = y;
		this._cb = cb;
		this._cr = cr;
	}

	public float Y
	{
		get { return this._y; }
		set { this._y = value; }
	}

	public float Cb
	{
		get { return this._cb; }
		set { this._cb = value; }
	}

	public float Cr
	{
		get { return this._cr; }
		set { this._cr = value; }
	}

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

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

	return new RGB((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}
								


Example

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


Output

									R: 192
G: 0
B: 166