VB source code for color models conversion

Public Function RGBtoHSL(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim HSL As HSB

Dim h As Double = 0.0

Dim s As Double = 0.0

Dim l As Double = 0.0

' normalizes red-green-blue values

Dim nRed As Double = CDbl(red) / 255.0

Dim nGreen As Double = CDbl(green) / 255.0

Dim nBlue As Double = CDbl(blue) / 255.0

Dim max As Double = Math.Max(nRed, Math.Max(nGreen, nBlue))

Dim min As Double = Math.Min(nRed, Math.Min(nGreen, nBlue))

' Hue

If (max = min) Then

h = 0 ' undefined

ElseIf (max = nRed And nGreen >= nBlue) Then

h = 60.0 * (nGreen - nBlue) / (max - min)

ElseIf (max = nRed And nGreen < nBlue) Then

h = 60.0 * (nGreen - nBlue) / (max - min) + 360.0

ElseIf (max = nGreen) Then

h = 60.0 * (nBlue - nRed) / (max - min) + 120.0

ElseIf (max = nBlue) Then

h = 60.0 * (nRed - nGreen) / (max - min) + 240.0

End If

' Luminance

l = (max + min) / 2.0

' Saturation

If (l = 0 Or max = min) Then

s = 0

ElseIf (0 < l And l <= 0.5) Then

s = (max - min) / (max + min)

ElseIf (l > 0.5) Then

s = (max - min) / (2 - (max + min))

End If

HSL.Hue = h

HSL.Saturation = s

HSL.Brightness = l

Return HSL

End Function

 

Public Function RGBtoHSB(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim hhssbb As HSB

Dim h As Double = 0.0

Dim s As Double = 0.0

' normalizes red-green-blue values

Dim nRed As Double = CDbl(red) / 255.0

Dim nGreen As Double = CDbl(green) / 255.0

Dim nBlue As Double = CDbl(blue) / 255.0

Dim max As Double = Math.Max(nRed, Math.Max(nGreen, nBlue))

Dim min As Double = Math.Min(nRed, Math.Min(nGreen, nBlue))

' Hue

If (max = nRed) And (nGreen >= nBlue) Then

If (max - min = 0) Then

h = 0.0

Else

h = 60 * (nGreen - nBlue) / (max - min)

End If

ElseIf (max = nRed) And (nGreen < nBlue) Then

h = 60 * (nGreen - nBlue) / (max - min) + 360

ElseIf (max = nGreen) Then

h = 60 * (nBlue - nRed) / (max - min) + 120

ElseIf (max = nBlue) Then

h = 60 * (nRed - nGreen) / (max - min) + 240

End If

' Saturation

If (max = 0) Then

s = 0.0

Else

s = 1.0 - (min / max)

End If

hhssbb.Hue = h

hhssbb.Saturation = s

hhssbb.Brightness = max

Return hhssbb

' Return New hsb(h, s, max)

End Function

Public Function RGBtoYUV(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim YUV As HSB

YUV.Hue = 0.299 * red + 0.587 * green + 0.114 * blue

YUV.Saturation = -0.14713769751693 * red - 0.28886230248307 * green + 0.436 * blue

YUV.Brightness = 0.615 * red - 0.51498573466476461 * green - 0.10001426533523537 * blue

Return YUV

End Function

Public Function RGBtoCMYK(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim dummi As Double

Dim CMYK As HSB

Dim c As Double = CDbl(255 - red) / 255.0

Dim m As Double = CDbl(255 - green) / 255.0

Dim y As Double = CDbl(255 - blue) / 255.0

Dim min As Double = Math.Min(c, Math.Min(m, y))

If (min = 1.0) Then

CMYK.Hue = 0

CMYK.Saturation = 0

CMYK.Brightness = 0

' Return New CMYK(0, 0, 0, 1)

Else

dummi = (1 - min)

CMYK.Hue = 255.0 * ((c - min) / dummi)

CMYK.Saturation = 255.0 * ((m - min) / dummi)

CMYK.Brightness = 255.0 * ((y - min) / dummi)

' Return New CMYK((c - min) / (1 - min), (m - min) / (1 - min), (y - min) / (1 - min), min)

End If

Return CMYK

End Function

Public Function RGBtoXYZ(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim XYZ As HSB

Dim nRed As Double = CDbl(red) / 255.0

Dim nGreen As Double = CDbl(green) / 255.0

Dim nBlue As Double = CDbl(blue) / 255.0

If (nRed > 0.04045) Then

nRed = Math.Pow((nRed + 0.055) / 1.055, 2.4)

'{ r = Math.pow((r + 0.055) / 1.055, 2.4); }

Else

nRed = nRed / 12.92

End If

If (nGreen > 0.04045) Then

nGreen = Math.Pow((nGreen + 0.055) / 1.055, 2.4)

Else

nGreen = nGreen / 12.92

End If

If (nBlue > 0.04045) Then

nBlue = Math.Pow((nBlue + 0.055) / 1.055, 2.4)

'{ r = Math.pow((r + 0.055) / 1.055, 2.4); }

Else

nBlue = nBlue / 12.92

End If

nRed = 100 * nRed

nGreen = 100 * nGreen

nBlue = 100 * nBlue

XYZ.Hue = nRed * 0.4124 + nGreen * 0.3576 + nBlue * 0.1805

XYZ.Saturation = nRed * 0.2126 + nGreen * 0.7152 + nBlue * 0.0722

XYZ.Brightness = nRed * 0.0193 + nGreen * 0.1192 + nBlue * 0.9505

Return XYZ

 

End Function

Public Function XYZtoLAB(ByVal red As Double, ByVal green As Double, ByVal blue As Double) As HSB

Const REF_X = 95.047

Const REF_Y = 100.0

Const REF_Z = 108.883

Dim LAB As HSB

Dim nred As Double = red / REF_X

Dim ngreen As Double = green / REF_Y

Dim nblue As Double = blue / REF_Z

If nred > 0.008856 Then

nred = Math.Pow(nred, c1n3)

Else

nred = (7.787 * nred) + c16n116 '(16 / 116)

End If

If ngreen > 0.008856 Then

ngreen = Math.Pow(ngreen, c1n3)

Else

ngreen = (7.787 * ngreen) + c16n116 'c16n116'(16 / 116)

End If

If nblue > 0.008856 Then

nblue = Math.Pow(nblue, c1n3)

Else

nblue = (7.787 * nblue) + c16n116 '(16 / 116)

End If

LAB.Hue = (116 * ngreen) - 16

LAB.Saturation = 500 * (nred - ngreen)

LAB.Brightness = 200 * (ngreen - nblue)

Return LAB

End Function

 

Public Function LABtoCH(ByVal red As Double, ByVal green As Double, ByVal blue As Double) As HSB

 

Dim LABCH As HSB

' Dim varH As Double = Math.Atan(green / blue)

Dim varH As Double

If green = 0 And blue = 0 Then

varH = 0

Else

If green = 0 Then

green = 0.00000000001

End If

If green < 0 Then

varH = Math.Atan(blue / green) - Math.PI

Else

varH = Math.Atan(blue / green)

End If

End If

If varH > 0 Then

varH = (varH / Math.PI) * 180

Else

varH = 360 - (Math.Abs(varH) / Math.PI) * 180

End If

LABCH.Hue = red

LABCH.Saturation = Math.Sqrt(green * green + blue * blue)

LABCH.Brightness = varH

Return LABCH

End Function

Public Function XYZtoHunterLAB(ByVal X As Double, ByVal Y As Double, ByVal Z As Double) As HSB

Dim dummi As Double

Dim HunterLAB As HSB

If Y <= 0 Then Y = 0.02

dummi = Math.Sqrt(Y)

HunterLAB.Hue = 10 * dummi 'Math.Sqrt(Y)

HunterLAB.Saturation = 17.5 * ((1.02 * X - Y) / dummi) 'Math.Sqrt(Y))

HunterLAB.Brightness = 7 * ((Y - 0.847 * Z) / dummi) ' Math.Sqrt(Y))

Return HunterLAB

End Function

Public Function XYZtoLUV(ByVal X As Double, ByVal Y As Double, ByVal Z As Double) As HSB

Dim dummi As Double

Dim LUV As HSB

Dim var_U, var_V, var_Y As Double

dummi = (X + (15 * Y) + (3 * Z))

If dummi <= 0 Then

dummi = 0.11

End If

var_U = (4 * X) / dummi '(X + (15 * Y) + (3 * Z))

var_V = (9 * Y) / dummi '(X + (15 * Y) + (3 * Z))

var_Y = Y / 100

If (var_Y > 0.008856) Then

var_Y = Math.Pow(var_Y, c1n3)

Else

var_Y = (7.787 * var_Y) + c16n116 '(16 / 116)

End If

 

Const ref_X = 95.047 ' //Observer= 2, Illuminant= D65

Const ref_Y = 100.0

Const ref_Z = 108.883

Const ref_U = (4 * ref_X) / (ref_X + (15 * ref_Y) + (3 * ref_Z))

Const ref_V = (9 * ref_Y) / (ref_X + (15 * ref_Y) + (3 * ref_Z))

'LUV.Hue = 2.5 * ((116 * var_Y) - 16)

'LUV.Saturation = 13 * LUV.Hue * (var_U - ref_U)

'LUV.Brightness = 0.5 * (13 * LUV.Hue * (var_V - ref_V))

LUV.Hue = ((116 * var_Y) - 16)

LUV.Saturation = 13 * LUV.Hue * (var_U - ref_U)

LUV.Brightness = (13 * LUV.Hue * (var_V - ref_V))

Return LUV

End Function

 

 

Public Function XYZtoYxy(ByVal X As Double, ByVal Y As Double, ByVal Z As Double) As HSB

Dim Yxy As HSB

If (X + Y + Z) <= 0 Then

Yxy.Hue = 0.002

Yxy.Saturation = 0.15 'X / (X + Y + Z)

Yxy.Brightness = 0.06 'Y / (X + Y + Z)

Else

Yxy.Hue = Y

Yxy.Saturation = X / (X + Y + Z)

Yxy.Brightness = Y / (X + Y + Z)

End If

Return Yxy

End Function

Public Function RGBtoYCbCr(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim YUV As HSB

'

YUV.Hue = 0.299 * red + 0.587 * green + 0.114 * blue

YUV.Saturation = -0.169 * red - 0.331 * green + 0.5 * blue + 128

YUV.Brightness = 0.5 * red - 0.419 * green - 0.081 * blue + 128

Return YUV

End Function

Public Function RGBtoYPbPr(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) As HSB

Dim YUV As HSB

YUV.Hue = 0.299 * red + 0.587 * green + 0.114 * blue

YUV.Saturation = -0.169 * red - 0.331 * green + 0.5 * blue

YUV.Brightness = 0.615 * red - 0.419 * green - 0.081 * blue

Return YUV

End Function