YUV To RGB

This algorithm converts YUV color space to RGB color model.



									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 YUVToRGB(yuv As YUV) As RGB
	Dim r As Byte = CByte(Math.Truncate(yuv.Y + 1.4075 * (yuv.V - 128)))
	Dim g As Byte = CByte(Math.Truncate(yuv.Y - 0.3455 * (yuv.U - 128) - (0.7169 * (yuv.V - 128))))
	Dim b As Byte = CByte(Math.Truncate(yuv.Y + 1.779 * (yuv.U - 128)))

	Return New RGB(r, g, b)
End Function
								


Example

									Dim data As New YUV(82, 140, 87)
Dim value = YUVToRGB(data)
								


Output

									R: 24
G: 107
B: 103