1 | #Region "Microsoft.VisualBasic::2f15d34981f532595ece9379d73c0a6b, Microsoft.VisualBasic.Core\Extensions\Image\Colors\HSLColor.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Structure HSLColor |
35 | ' |
36 | ' Properties: H, L, S |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: GetHSL, hue2rgb, Lighten, ToRGB, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Drawing |
47 | Imports Microsoft.VisualBasic.CommandLine.Reflection |
48 | Imports sys = System.Math |
49 | |
50 | Namespace Imaging |
51 | |
52 | ''' <summary> |
53 | ''' Describes a RGB color in Hue, Saturation, and Luminance values. |
54 | ''' </summary> |
55 | ''' <remarks></remarks> |
56 | Public Structure HSLColor |
57 | |
58 | ''' <summary> |
59 | ''' The color hue. |
60 | ''' </summary> |
61 | ''' <remarks></remarks> |
62 | Public Property H As Double |
63 | ''' <summary> |
64 | ''' The color saturation. |
65 | ''' </summary> |
66 | ''' <remarks></remarks> |
67 | Public Property S As Double |
68 | ''' <summary> |
69 | ''' The color luminance. |
70 | ''' </summary> |
71 | ''' <remarks></remarks> |
72 | Public Property L As Double |
73 | |
74 | Public Sub New(hValue As Double, sValue As Double, lValue As Double) |
75 | H = hValue |
76 | S = sValue |
77 | L = lValue |
78 | End Sub |
79 | |
80 | Public Overrides Function ToString() As String |
81 | Return ToRGB.ToHtmlColor |
82 | End Function |
83 | |
84 | ''' <summary> |
85 | ''' Lighten target color composition. |
86 | ''' </summary> |
87 | ''' <param name="percentage"></param> |
88 | ''' <param name="lightColor"></param> |
89 | ''' <returns></returns> |
90 | Public Function Lighten(percentage As Double, lightColor As Color) As Color |
91 | Dim base As Color = ToRGB() |
92 | Dim newColor As Color = Color.FromArgb( |
93 | base.A, |
94 | (lightColor.R / 255.0) * base.R, |
95 | (lightColor.G / 255.0) * base.G, |
96 | (lightColor.B / 255.0) * base.B) |
97 | Dim hsl As HSLColor = HSLColor.GetHSL(newColor) |
98 | Dim l = sys.Min(hsl.L + percentage, 1) |
99 | |
100 | newColor = New HSLColor(hsl.H, hsl.S, l).ToRGB |
101 | Return newColor |
102 | End Function |
103 | |
104 | Public Function ToRGB() As Color |
105 | Dim r, g, b As Double |
106 | Dim h As Double = Me.H |
107 | Dim s As Double = Me.S |
108 | Dim l As Double = Me.L |
109 | |
110 | If s = 0 Then |
111 | b = l |
112 | g = b |
113 | r = g |
114 | Else |
115 | Dim q As Double = If(l < 0.5, l * (1 + s), l + s - l * s) |
116 | Dim p As Double = 2.0 * l - q |
117 | |
118 | r = HSLColor.hue2rgb(p, q, h + 1 / 3.0) |
119 | g = HSLColor.hue2rgb(p, q, h) |
120 | b = HSLColor.hue2rgb(p, q, h - 1 / 3.0) |
121 | End If |
122 | |
123 | r = r * 255.0 |
124 | g = g * 255.0 |
125 | b = b * 255.0 |
126 | |
127 | Return Color.FromArgb(r, g, b) |
128 | End Function |
129 | |
130 | Private Shared Function hue2rgb(p As Double, q As Double, t As Double) As Double |
131 | If t < 0 Then t += 1 |
132 | If t > 1 Then t -= 1 |
133 | If t < 1 / 6.0 Then Return p + (q - p) * 6.0 * t |
134 | If t < 1 / 2.0 Then Return q |
135 | If t < 2 / 3.0 Then Return p + (q - p) * (2.0 / 3.0 - t) * 6.0 |
136 | Return p |
137 | End Function |
138 | |
139 | ''' <summary> |
140 | ''' Converts a RGB color into its Hue, Saturation, and Luminance (HSL) values. |
141 | ''' </summary> |
142 | ''' <param name="rgb">The color to convert.</param> |
143 | ''' <returns>The HSL representation of the color.</returns> |
144 | ''' <remarks> |
145 | ''' Source algorithm found using web search at: |
146 | ''' http://geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm This link is external to TechNet Wiki. It will open in a new window. |
147 | ''' (Adapted to VB) |
148 | ''' </remarks> |
149 | ''' |
150 | <ExportAPI("Color.HSL")> |
151 | Public Shared Function GetHSL(rgb As Color) As HSLColor |
152 | Dim h, s, l As Double |
153 | Dim r As Double = rgb.R / 255.0 |
154 | Dim g As Double = rgb.G / 255.0 |
155 | Dim b As Double = rgb.B / 255.0 |
156 | Dim max, min As Double |
157 | |
158 | max = sys.Max(r, g) |
159 | max = sys.Max(max, b) |
160 | min = sys.Min(r, g) |
161 | min = sys.Min(min, b) |
162 | |
163 | l = (min + max) / 2.0 |
164 | |
165 | If max = min Then |
166 | s = 0 |
167 | h = s |
168 | Else |
169 | Dim d As Double = max - min |
170 | |
171 | s = If(l > 0.5, d / (2.0 - max - min), d / (max + min)) |
172 | |
173 | If max = r Then |
174 | h = (g - b) / d + (If(g < b, 6.0, 0.0)) |
175 | ElseIf max = g Then |
176 | h = (b - r) / d + 2.0 |
177 | ElseIf max = b Then |
178 | h = (r - g) / d + 4.0 |
179 | End If |
180 | |
181 | h /= 6.0 |
182 | End If |
183 | |
184 | Return New HSLColor(h, s, l) |
185 | End Function |
186 | End Structure |
187 | End Namespace |