|
' Hue Saturation Lightness structure.
'
' Copyright (c) 2007 Miran Uhan
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the
' Free Software Foundation, Inc.
' 51 Franklin Street, Fifth Floor
' Boston, MA 02110-1301 USA
'
' For questions you can contact author at
' miran.uhan@gmail.com
' Sugestions and bug reports are also welcome.
'
Imports System.Drawing
Imports System.Drawing.Imaging
Public Structure HslColor
Public H As Double
Public S As Double
Public L As Double
''' <summary>
''' Convert RGB to Hue, Saturation, Lightness.
''' </summary>
''' <param name="R">Value of red component in range [0..1].</param>
''' <param name="G">Value of green component in range [0..1].</param>
''' <param name="B">Value of blue component in range [0..1].</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function FromRgb(ByVal R As Double, ByVal G As Double, ByVal B As Double) As HslColor
Dim result As HslColor
Dim min, max, dif, sum, f1, f2 As Double
min = R
If (G < min) Then min = G
If (B < min) Then min = B
max = R : f1 = 0.0 : f2 = G - B
If (G > max) Then
max = G : f1 = 1 / 3 : f2 = B - R
End If
If (B > max) Then
max = B : f1 = 2 / 3 : f2 = R - G
End If
dif = max - min
sum = max + min
result.L = 0.5 * sum
If (dif = 0) Then
result.H = 0.0
result.S = 0.0
Else
If (result.L < 0.5) Then
result.S = dif / sum
Else
result.S = dif / (2 - sum)
End If
result.H = f1 + f2 / (6 * dif)
End If
If result.H < 0 Then result.H += 1
If result.H > 1 Then result.H -= 1
Return result
End Function
''' <summary>
''' Convert RGB to Hue, Saturation, Lightness.
''' </summary>
''' <param name="C">Color structure.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function FromRgb(ByVal C As Color) As HslColor
Return FromRgb(C.R / 255.0, C.G / 255.0, C.B / 255.0)
End Function
Public Shared Function ToRgb(ByVal H As Double, ByVal S As Double, ByVal L As Double) As Color
Dim result(2) As Double
If (S = 0) Then
result(0) = L * 255
result(1) = result(0)
result(2) = result(0)
Else
Dim v2 As Double
If (L < 0.5) Then
v2 = L * (1 + S)
Else
v2 = (L + S) - (S * L)
End If
Dim v1 As Double = 2 * L - v2
Dim dif As Double = v2 - v1
Dim h1 As Double
'Calc red component.
h1 = H + 1 / 3
If (h1 > 1) Then h1 -= 1
If ((6 * h1) < 1) Then
result(0) = 255 * (v1 + dif * 6 * h1)
ElseIf ((2 * h1) < 1) Then
result(0) = 255 * v2
ElseIf ((3 * h1) < 2) Then
result(0) = 255 * (v1 + dif * (4.0 - 6 * h1))
Else
result(0) = 255 * v1
End If
'Calc green component.
h1 = H
If ((6 * h1) < 1) Then
result(1) = 255 * (v1 + dif * 6 * h1)
ElseIf ((2 * h1) < 1) Then
result(1) = 255 * v2
ElseIf ((3 * h1) < 2) Then
result(1) = 255 * (v1 + dif * (4.0 - 6 * h1))
Else
result(1) = 255 * v1
End If
'Calc blue component.
h1 = H - 1 / 3
If (h1 < 0) Then h1 += 1
If ((6 * h1) < 1) Then
result(2) = 255 * (v1 + dif * 6 * h1)
ElseIf ((2 * h1) < 1) Then
result(2) = 255 * v2
ElseIf ((3 * h1) < 2) Then
result(2) = 255 * (v1 + dif * (4.0 - 6 * h1))
Else
result(2) = 255 * v1
End If
End If
Return Color.FromArgb(result(0), result(1), result(2))
End Function
Public Shared Function ToRgb(ByVal C As HslColor) As Color
ToRgb(C.H, C.S, C.L)
End Function
End Structure
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.