1 |
#Region "Microsoft.VisualBasic::f21617a6bd6c02a95a5068172cc7c004, Microsoft.VisualBasic.Core\Extensions\Image\Colors\GDIColors.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
#End Region
|
47 |
|
48 |
Imports System.Drawing
|
49 |
Imports System.Reflection
|
50 |
Imports System.Runtime.CompilerServices
|
51 |
Imports System.Text.RegularExpressions
|
52 |
Imports Microsoft.VisualBasic.CommandLine.Reflection
|
53 |
Imports Microsoft.VisualBasic.Language
|
54 |
Imports Microsoft.VisualBasic.Language.Default
|
55 |
|
56 |
Namespace Imaging
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
Public Module GDIColors
|
62 |
|
63 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
64 |
<Extension>
|
65 |
Public Function HTMLColors(colors As IEnumerable(Of Color)) As IEnumerable(Of String)
|
66 |
Return colors.Select(AddressOf ToHtmlColor)
|
67 |
End Function
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
<Extension> Public Function Middle(x As Color, y As Color) As Color
|
76 |
Dim r% = (y.R - x.R) / 2 + x.R
|
77 |
Dim g% = (y.G - x.G) / 2 + x.G
|
78 |
Dim b% = (y.B - x.B) / 2 + x.B
|
79 |
Dim a% = (y.A - x.A) / 2 + x.A
|
80 |
Dim c As Color = Color.FromArgb(a, r, g, b)
|
81 |
Return c
|
82 |
End Function
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
<Extension>
|
91 |
Public Function Alpha(colors As IEnumerable(Of Color), alphaValue%) As Color()
|
92 |
Dim out As New List(Of Color)
|
93 |
For Each c As Color In colors
|
94 |
With c
|
95 |
out += Color.FromArgb(alphaValue, .R, .G, .B)
|
96 |
End With
|
97 |
Next
|
98 |
Return out
|
99 |
End Function
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
108 |
<Extension>
|
109 |
Public Function Alpha(c As Color, alphaValue%) As Color
|
110 |
Return Color.FromArgb(alphaValue, c.R, c.G, c.B)
|
111 |
End Function
|
112 |
|
113 |
<Extension>
|
114 |
Public Function Average(colors As IEnumerable(Of Color)) As Color
|
115 |
Dim data As Color() = colors.ToArray
|
116 |
Dim A% = data.Select(Function(c) CDbl(c.A)).Average
|
117 |
Dim R% = data.Select(Function(c) CDbl(c.R)).Average
|
118 |
Dim G% = data.Select(Function(c) CDbl(c.G)).Average
|
119 |
Dim B% = data.Select(Function(c) CDbl(c.B)).Average
|
120 |
|
121 |
Return Color.FromArgb(A, R, G, B)
|
122 |
End Function
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
133 |
<Extension>
|
134 |
Public Function Light(base As Color, percent!) As Color
|
135 |
Return ControlPaint.Light(base, percent)
|
136 |
End Function
|
137 |
|
138 |
|
139 |
Creates a new dark color object for the control from the specified color and
|
140 |
darkens it by the specified percentage.
|
141 |
|
142 |
<param name="base">The <see cref="System.Drawing.Color"/> to be darkened.</param>
|
143 |
<param name="percent!">The percentage to darken the specified <see cref="System.Drawing.Color"/>.</param>
|
144 |
<returns>A <see cref="System.Drawing.Color"/> that represent the dark color on the control.</returns>
|
145 |
|
146 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
147 |
<Extension>
|
148 |
Public Function Dark(base As Color, percent!) As Color
|
149 |
Return ControlPaint.Dark(base, percent)
|
150 |
End Function
|
151 |
|
152 |
|
153 |
``rgb(r,g,b)``
|
154 |
|
155 |
|
156 |
|
157 |
<Extension>
|
158 |
Public Function RGBExpression(c As Color) As String
|
159 |
With c
|
160 |
Return $"rgb({ .R},{ .G},{ .B})"
|
161 |
End With
|
162 |
End Function
|
163 |
|
164 |
|
165 |
``rgb(a,r,g,b)``
|
166 |
|
167 |
|
168 |
|
169 |
<Extension>
|
170 |
Public Function ARGBExpression(c As Color) As String
|
171 |
With c
|
172 |
Return $"rgb({ .A},{ .R},{ .G},{ .B})"
|
173 |
End With
|
174 |
End Function
|
175 |
|
176 |
#If NET_40 = 0 Then
|
177 |
|
178 |
|
179 |
Reads all of the color property from <see cref="Color"/> and then creates the color dictionary based on the property name.
|
180 |
|
181 |
|
182 |
Private Function __getDotNetColors() As Dictionary(Of String, Color)
|
183 |
Dim props As IEnumerable(Of PropertyInfo) =
|
184 |
GetType(Color).GetProperties(BindingFlags.Public Or BindingFlags.Static)
|
185 |
Dim getValues = From p As PropertyInfo
|
186 |
In props
|
187 |
Where p.PropertyType = GetType(Color)
|
188 |
Let ColorValue As Color = DirectCast(p.GetValue(Nothing), Color)
|
189 |
Select name = p.Name,
|
190 |
ColorValue
|
191 |
Dim hash As Dictionary(Of String, Color) =
|
192 |
getValues.ToDictionary(Function(x) x.name.ToLower,
|
193 |
Function(x) x.ColorValue)
|
194 |
Return hash
|
195 |
End Function
|
196 |
|
197 |
|
198 |
Key都是小写的
|
199 |
|
200 |
ReadOnly __allDotNETPrefixColors As Dictionary(Of String, Color) =
|
201 |
__getDotNetColors()
|
202 |
|
203 |
|
204 |
Gets all of the known name color from the Color object its shared property.
|
205 |
|
206 |
|
207 |
Public ReadOnly Property AllDotNetPrefixColors As Color()
|
208 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
209 |
Get
|
210 |
Return __allDotNETPrefixColors.Values.Shuffles
|
211 |
End Get
|
212 |
End Property
|
213 |
|
214 |
|
215 |
经过人工筛选的颜色,不会出现过白或者过黑,过度相似的情况
|
216 |
|
217 |
|
218 |
Public ReadOnly Property ChartColors As Color() = {
|
219 |
Color.AliceBlue, Color.Aquamarine, Color.BlueViolet, Color.BurlyWood,
|
220 |
Color.CadetBlue, Color.Chartreuse, Color.Chocolate, Color.Coral,
|
221 |
Color.CornflowerBlue, Color.Crimson, Color.Cyan, Color.DarkBlue,
|
222 |
Color.DarkCyan, Color.DarkGoldenrod, Color.DarkGray, Color.DarkMagenta,
|
223 |
Color.DarkOliveGreen, Color.DarkOrchid, Color.DarkSeaGreen, Color.DarkSlateBlue,
|
224 |
Color.DarkSlateGray, Color.DeepPink, Color.DeepSkyBlue, Color.DodgerBlue,
|
225 |
Color.GreenYellow, Color.ForestGreen, Color.Firebrick, Color.Gold, Color.Indigo,
|
226 |
Color.LightSeaGreen, Color.LightSkyBlue, Color.LimeGreen, Color.MediumSeaGreen,
|
227 |
Color.MediumTurquoise, Color.MidnightBlue, Color.Orchid, Color.OrangeRed, Color.Red,
|
228 |
Color.RoyalBlue, Color.SeaGreen, Color.SpringGreen, Color.SteelBlue, Color.Teal,
|
229 |
Color.YellowGreen
|
230 |
}
|
231 |
#End If
|
232 |
|
233 |
Regex expression for parsing the rgb(a,r,g,b) expression of the color.(解析颜色表达式里面的RGB的正则表达式)
|
234 |
|
235 |
Public Const rgbExprValues$ = "\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d+)?"
|
236 |
Public Const rgbExpr$ = "rgb\(" & rgbExprValues & "\)"
|
237 |
|
238 |
|
239 |
<see cref="Color"/>.Name, rgb(a,r,g,b)
|
240 |
|
241 |
<param name="str">颜色表达式或者名称</param>
|
242 |
|
243 |
<ExportAPI("Get.Color")>
|
244 |
<Extension> Public Function ToColor(str As String, Optional onFailure As Color = Nothing, Optional throwEx As Boolean = True) As Color
|
245 |
#If NET_40 = 0 Then
|
246 |
If String.IsNullOrEmpty(str) Then
|
247 |
Return Color.Black
|
248 |
ElseIf str.TextEquals("transparent") Then
|
249 |
Return Color.Transparent
|
250 |
End If
|
251 |
|
252 |
Dim s As String = Regex.Match(str, rgbExprValues).Value
|
253 |
|
254 |
If String.IsNullOrEmpty(s) Then
|
255 |
Dim key As String = str.ToLower
|
256 |
|
257 |
If __allDotNETPrefixColors.ContainsKey(key) Then
|
258 |
Return __allDotNETPrefixColors(key)
|
259 |
Else
|
260 |
Return Color.FromName(str)
|
261 |
End If
|
262 |
Else
|
263 |
Dim tokens As String() = s.Split(","c)
|
264 |
|
265 |
If tokens.Length = 3 Then
|
266 |
Dim R As Integer = CInt(Val(tokens(0)))
|
267 |
Dim G As Integer = CInt(Val(tokens(1)))
|
268 |
Dim B As Integer = CInt(Val(tokens(2)))
|
269 |
|
270 |
Return Color.FromArgb(R, G, B)
|
271 |
ElseIf tokens.Length = 4 Then
|
272 |
Dim A As Integer = CInt(Val(tokens(0)))
|
273 |
Dim R As Integer = CInt(Val(tokens(1)))
|
274 |
Dim G As Integer = CInt(Val(tokens(2)))
|
275 |
Dim B As Integer = CInt(Val(tokens(3)))
|
276 |
|
277 |
Return Color.FromArgb(A, R, G, B)
|
278 |
Else
|
279 |
If Not onFailure.IsEmpty Then
|
280 |
Return onFailure
|
281 |
Else
|
282 |
If throwEx Then
|
283 |
Throw New Exception("Unable parsing any color information from expression: " & str)
|
284 |
Else
|
285 |
Return Nothing
|
286 |
End If
|
287 |
End If
|
288 |
End If
|
289 |
End If
|
290 |
#Else
|
291 |
Throw New NotSupportedException
|
292 |
#End If
|
293 |
End Function
|
294 |
|
295 |
|
296 |
这个函数会尝试用不同的模式来解析颜色表达式
|
297 |
|
298 |
<param name="exp$"></param>
|
299 |
|
300 |
<Extension> Public Function TranslateColor(exp$, Optional throwEx As Boolean = True) As Color
|
301 |
If exp.StringEmpty Then
|
302 |
Return Color.Black
|
303 |
End If
|
304 |
If exp.First = "#"c Then
|
305 |
|
306 |
|
307 |
Return HexColor.ConvertToRbg(exp)
|
308 |
End If
|
309 |
If Regex.Match(exp, "\d+").Value = exp Then
|
310 |
Return ColorTranslator.FromOle(CInt(exp))
|
311 |
End If
|
312 |
|
313 |
Return exp.ToColor(throwEx:=throwEx)
|
314 |
End Function
|
315 |
|
316 |
<Extension>
|
317 |
Public Function IsColorExpression(expression$) As Boolean
|
318 |
If expression.MatchPattern(rgbExpr, RegexICSng) Then
|
319 |
Return True
|
320 |
ElseIf __allDotNETPrefixColors.ContainsKey(expression.ToLower) Then
|
321 |
Return True
|
322 |
ElseIf expression.MatchPattern("\d+") Then
|
323 |
Return True
|
324 |
ElseIf expression.MatchPattern("#[a-z0-9]+", RegexICSng) Then
|
325 |
Return True
|
326 |
Else
|
327 |
Return False
|
328 |
End If
|
329 |
End Function
|
330 |
|
331 |
|
332 |
Determine that the target color value is a empty variable.(判断目标颜色值是否为空值)
|
333 |
|
334 |
|
335 |
<remarks></remarks>
|
336 |
|
337 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
338 |
<Extension> Public Function IsNullOrEmpty(Color As Color) As Boolean
|
339 |
Return Color = Nothing OrElse Color.IsEmpty
|
340 |
End Function
|
341 |
|
342 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
343 |
<Extension>
|
344 |
Public Function AsDefaultColor(color As Color) As DefaultValue(Of Color)
|
345 |
Return color.AsDefault(Function(c) DirectCast(c, Color).IsNullOrEmpty)
|
346 |
End Function
|
347 |
|
348 |
|
349 |
|
350 |
A,R,G,B
|
351 |
1. 0,0,0,0
|
352 |
2. 0,255,255,255
|
353 |
3. 255,0,0,0
|
354 |
|
355 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
356 |
<Extension>
|
357 |
Public Function IsTransparent(c As Color) As Boolean
|
358 |
Return c.A = 0 OrElse (c.R = 0 AndAlso c.G = 0 AndAlso c.B = 0)
|
359 |
End Function
|
360 |
|
361 |
|
362 |
分别比较A,R,G,B这些属性值来判断这样个颜色对象值是否相等
|
363 |
|
364 |
<param name="a"></param>
|
365 |
<param name="b"></param>
|
366 |
|
367 |
<Extension> Public Function Equals(a As Color, b As Color) As Boolean
|
368 |
If a.IsTransparent AndAlso b.IsTransparent Then
|
369 |
Return True
|
370 |
End If
|
371 |
|
372 |
If a.A = b.A Then
|
373 |
If a.A = 0 Then
|
374 |
只要是alpha值为零,肯定是透明色
|
375 |
在这里判定为相同的颜色
|
376 |
Return True
|
377 |
End If
|
378 |
Else
|
379 |
Return False alpha值不相等,则颜色值肯定不相等
|
380 |
End If
|
381 |
|
382 |
If a.B <> b.B Then
|
383 |
Return False
|
384 |
End If
|
385 |
If a.G <> b.G Then
|
386 |
Return False
|
387 |
End If
|
388 |
If a.R <> b.R Then
|
389 |
Return False
|
390 |
End If
|
391 |
|
392 |
Return True
|
393 |
End Function
|
394 |
|
395 |
<Extension>
|
396 |
Public Function EuclideanDistance(a As Color, b As Color) As Double
|
397 |
Return Math.EuclideanDistance({a.R, a.G, a.B}, {b.R, b.G, b.B})
|
398 |
End Function
|
399 |
End Module
|
400 |
End Namespace
|