YCbCr To RGB
This algorithm converts YCbCr 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 YCbCr
Private _y As Single
Private _cb As Single
Private _cr As Single
Public Sub New(y As Single, cb As Single, cr As Single)
Me._y = y
Me._cb = cb
Me._cr = cr
End Sub
Public Property Y() As Single
Get
Return Me._y
End Get
Set(value As Single)
Me._y = value
End Set
End Property
Public Property Cb() As Single
Get
Return Me._cb
End Get
Set(value As Single)
Me._cb = value
End Set
End Property
Public Property Cr() As Single
Get
Return Me._cr
End Get
Set(value As Single)
Me._cr = value
End Set
End Property
Public Overloads Function Equals(ycbcr As YCbCr) As Boolean
Return (Me.Y = ycbcr.Y) AndAlso (Me.Cb = ycbcr.Cb) AndAlso (Me.Cr = ycbcr.Cr)
End Function
End Structure
Public Shared Function YCbCrToRGB(ycbcr As YCbCr) As RGB
Dim r As Single = Math.Max(0.0F, Math.Min(1.0F, CSng(ycbcr.Y + 0.0 * ycbcr.Cb + 1.4022 * ycbcr.Cr)))
Dim g As Single = Math.Max(0.0F, Math.Min(1.0F, CSng(ycbcr.Y - 0.3456 * ycbcr.Cb - 0.7145 * ycbcr.Cr)))
Dim b As Single = Math.Max(0.0F, Math.Min(1.0F, CSng(ycbcr.Y + 1.771 * ycbcr.Cb + 0.0 * ycbcr.Cr)))
Return New RGB(CByte(Math.Truncate(r * 255)), CByte(Math.Truncate(g * 255)), CByte(Math.Truncate(b * 255)))
End Function
Example
Dim data As New YCbCr(0.28F, 0.21F, 0.34F)
Dim value = YCbCrToRGB(data)
Output
R: 192
G: 0
B: 166