RGB To YUV

This algorithm converts RGB color model to YUV color space.



									Public Structure RGB
	Private _r As Byte
	Private _g As Byte
	Private _b As Byte

	Public Sub New(r As Byte, g As Byte, b As Byte)
		Me._r = r
		Me._g = g
		Me._b = b
	End Sub

	Public Property R() As Byte
		Get
			Return Me._r
		End Get
		Set(value As Byte)
			Me._r = value
		End Set
	End Property

	Public Property G() As Byte
		Get
			Return Me._g
		End Get
		Set(value As Byte)
			Me._g = value
		End Set
	End Property

	Public Property B() As Byte
		Get
			Return Me._b
		End Get
		Set(value As Byte)
			Me._b = value
		End Set
	End Property

	Public Overloads Function Equals(rgb As RGB) As Boolean
		Return (Me.R = rgb.R) AndAlso (Me.G = rgb.G) AndAlso (Me.B = rgb.B)
	End Function
End Structure

Public Structure YUV
	Private _y As Double
	Private _u As Double
	Private _v As Double

	Public Sub New(y As Double, u As Double, v As Double)
		Me._y = y
		Me._u = u
		Me._v = v
	End Sub

	Public Property Y() As Double
		Get
			Return Me._y
		End Get
		Set(value As Double)
			Me._y = value
		End Set
	End Property

	Public Property U() As Double
		Get
			Return Me._u
		End Get
		Set(value As Double)
			Me._u = value
		End Set
	End Property

	Public Property V() As Double
		Get
			Return Me._v
		End Get
		Set(value As Double)
			Me._v = value
		End Set
	End Property

	Public Overloads Function Equals(yuv As YUV) As Boolean
		Return (Me.Y = yuv.Y) AndAlso (Me.U = yuv.U) AndAlso (Me.V = yuv.V)
	End Function
End Structure

Public Shared Function RGBToYUV(rgb As RGB) As YUV
	Dim y As Double = rgb.R * 0.299 + rgb.G * 0.587 + rgb.B * 0.114
	Dim u As Double = rgb.R * -0.168736 + rgb.G * -0.331264 + rgb.B * 0.5 + 128
	Dim v As Double = rgb.R * 0.5 + rgb.G * -0.418688 + rgb.B * -0.081312 + 128

	Return New YUV(y, u, v)
End Function
								


Example

									Dim data As New RGB(82, 0, 87)
Dim value = RGBToYUV(data)
								


Output

									Y: 34.436
U: 157.663648
V: 161.925856