RGB To YUV

This algorithm converts RGB color model to YUV color space.



									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 YUV
{
	private double _y;
	private double _u;
	private double _v;

	public YUV(double y, double u, double v)
	{
		this._y = y;
		this._u = u;
		this._v = v;
	}

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

	public double U
	{
		get { return this._u; }
		set { this._u = value; }
	}

	public double V
	{
		get { return this._v; }
		set { this._v = value; }
	}

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

public 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 new YUV(y, u, v);
}
								


Example

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


Output

									Y: 34.436
U: 157.663648
V: 161.925856