RGB To YCbCr
This algorithm converts RGB color model to YCbCr 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 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 RGBToYCbCr(rgb As RGB) As YCbCr
Dim fr As Single = CSng(rgb.R) / 255
Dim fg As Single = CSng(rgb.G) / 255
Dim fb As Single = CSng(rgb.B) / 255
Dim Y As Single = CSng(0.2989 * fr + 0.5866 * fg + 0.1145 * fb)
Dim Cb As Single = CSng(-0.1687 * fr - 0.3313 * fg + 0.5 * fb)
Dim Cr As Single = CSng(0.5 * fr - 0.4184 * fg - 0.0816 * fb)
Return New YCbCr(Y, Cb, Cr)
End Function
Example
Dim data As New RGB(82, 0, 87)
Dim value = RGBToYCbCr(data)
Output
Y: 0.135181576
Cb: 0.116339609
Cr: 0.132944316