View difference between Paste ID: 88Q0wGPf and ZLD0qurn
SHOW: | | - or go back to the newest paste.
1
' ***********************************************************************
2
' Author   : Elektro
3
' Created  : 01-13-2014
4
' Modified : 01-13-2014
5
' ***********************************************************************
6
' <copyright file="ColorTools.vb" company="Elektro Studios">
7
'     Copyright (c) Elektro Studios. All rights reserved.
8
' </copyright>
9
' ***********************************************************************
10
'
11
' --------------
12
' Public Methods
13
' --------------
14
'
15
' Screen.GetPixelColor
16
' Screen.GetPixelBrush
17
' Screen.GetPixelPen
18
'
19
' ColorConvert.ColorToBrush
20
' ColorConvert.ColorToPen
21
' ColorConvert.BrushToColor
22-
' StringConvert.ColortoString
22+
23
'
24
' StringConvert.ColorToString
25
' StringConvert.BrushToString
26
' StringConvert.PenToString
27
' StringConvert.StringToColor
28
' StringConvert.StringToBrush
29
' StringConvert.StringToPen
30
' StringConvert.StringToString
31
'
32
' RandomGenerators.ARGB
33
' RandomGenerators.RGB
34
' RandomGenerators.QB
35
' RandomGenerators.ConsoleColor
36
' RandomGenerators.Brush
37
' RandomGenerators.Pen
38
39
#Region " Usage Examples "
40
41
'' Gets the color of the pixel at the 50,100 coordinates:
42
' Dim c As Color = ColorTools.Screen.GetPixelColor(50, 100)
43
44
'' Generates a random Brush
45
' Dim br As SolidBrush = ColorTools.RandomGenerators.Brush 
46
47
'' Converts a SolidBrush to a Color:
48
' Dim c As Color = ColorTools.ColorConvert.BrushToColor(New SolidBrush(Color.Red))
49
50
'' Converts an HTML Color-String to a Color:
51
' PictureBox1.BackColor = ColorTools.StringConvert.StringToColor("#FF00FFFF",
52
'                                                                ColorTools.StringConvert.ValueFormat.HTML,
53
'                                                                ColorTools.StringConvert.StringSyntax.None)
54
55
'' Converts an Hex Color-String to a Color:
56
' MsgBox(ColorTools.StringConvert.StringToColor("0x003399",
57
'                                               ColorTools.StringConvert.ValueFormat.Hexadecimal,
58
'                                               ColorTools.StringConvert.StringSyntax.None))
59
60
'' Converts a Byte Color-String with VisyalStudio's property grid syntax to a Color:
61
' MsgBox(ColorTools.StringConvert.StringToColor("255; 255; 255; 255",
62
'                                               ColorTools.StringConvert.ValueFormat.Byte,
63
'                                               ColorTools.StringConvert.StringSyntax.VisualStudioPropertyGrid).
64
'                                               Name)
65
66
'' Converts a HEX Color-String with VB.NET syntax to a Color:
67
' MsgBox(ColorTools.StringConvert.StringToColor("Color.FromArgb(&HFF, &H5F, &HEC, &H12)",
68
'                                               ColorTools.StringConvert.ValueFormat.Hexadecimal,
69
'                                               ColorTools.StringConvert.StringSyntax.VBNET).
70
'                                               ToString)
71
72
'' Converts an HTML Color-String with C# Syntax to a Brush:
73
' Dim br As Brush = ColorTools.StringConvert.StringToBrush("ColorTranslator.FromHtml(""#F71608"");",
74
'                                                          ColorTools.StringConvert.ValueFormat.HTML,
75
'                                                          ColorTools.StringConvert.StringSyntax.VBNET)
76
77
'' Converts a Color-String to other Color-String:
78
' MsgBox(ColorTools.StringConvert.StringToString("ColorTranslator.FromHtml(""#AF0CCAFE"");",
79
'                                                ColorTools.StringConvert.ValueFormat.HTML,
80
'                                                ColorTools.StringConvert.StringSyntax.CSharp,
81
'                                                ColorTools.StringConvert.ValueFormat.Byte,
82
'                                                ColorTools.StringConvert.StringSyntax.None, 
83
'                                                True))
84
85
#End Region
86
87
#Region " Imports "
88
89
Imports System.Runtime.InteropServices
90
Imports System.ComponentModel
91
92
#End Region
93
94
''' <summary>
95
''' Native API Methods.
96
''' </summary>
97
Friend NotInheritable Class NativeMethods
98
99
#Region " API Methods "
100
101
    ''' <summary>
102
    ''' Retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.
103
    ''' </summary>
104
    ''' <param name="hdc">A handle to the device context.</param>
105
    ''' <param name="nXPos">The x-coordinate, in logical units, of the pixel to be examined.</param>
106
    ''' <param name="nYPos">The y-coordinate, in logical units, of the pixel to be examined.</param>
107
    ''' <returns>
108
    ''' The return value is the COLORREF value that specifies the RGB of the pixel. 
109
    ''' If the pixel is outside of the current clipping region, the return value is CLR_INVALID (0xFFFFFFFF)
110
    ''' </returns>
111
    <DllImport("gdi32.dll", SetLastError:=True)>
112
    Friend Shared Function GetPixel(hdc As IntPtr,
113
                  ByVal nXPos As Integer,
114
                  ByVal nYPos As Integer
115
    ) As UInteger
116
    End Function
117
118
    ''' <summary>
119
    ''' Retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen. 
120
    ''' You can use the returned handle in subsequent GDI functions to draw in the DC.
121
    ''' The device context is an opaque data structure, whose values are used internally by GDI.
122
    ''' </summary>
123
    ''' <param name="hwnd">
124
    ''' A handle to the window whose DC is to be retrieved. 
125
    ''' If this value is NULL, GetDC retrieves the DC for the entire screen.
126
    ''' </param>
127
    ''' <returns>
128
    ''' If the function succeeds, the return value is a handle to the DC for the specified window's client area.
129
    ''' If the function fails, the return value is NULL.
130
    ''' </returns>
131
    <DllImport("user32.dll", SetLastError:=False)>
132
    Friend Shared Function GetDC(
133
                  ByVal hwnd As IntPtr
134
    ) As IntPtr
135
    End Function
136
137
    ''' <summary>
138
    ''' Releases a device context (DC), freeing it for use by other applications.
139
    ''' The effect of the ReleaseDC function depends on the type of DC. 
140
    ''' It frees only common and window DCs. 
141
    ''' It has no effect on class or private DCs.
142
    ''' </summary>
143
    ''' <param name="hWnd">A handle to the window whose DC is to be released.</param>
144
    ''' <param name="hDC">A handle to the DC to be released.</param>
145
    ''' <returns><c>true</c> if the DC was released, <c>false</c> if the DC was not released.</returns>
146
    <DllImport("user32.dll", SetLastError:=False)>
147
    Friend Shared Function ReleaseDC(
148
                  ByVal hWnd As IntPtr,
149
                  ByVal hDC As IntPtr
150
    ) As <MarshalAs(UnmanagedType.Bool)> Boolean
151
    End Function
152
153
#End Region
154
155
End Class
156
157
''' <summary>
158
''' Tools related about colors.
159
''' </summary>
160
Public Class ColorTools
161
162
#Region " Screen "
163
164
    ''' <summary>
165
    ''' Screen utilities related about colors.
166
    ''' </summary>
167
    Public Class Screen
168
169
#Region " Public Methods "
170
171
        ''' <summary>
172
        ''' Gets the color of a pixel on the specified coordinates.
173
        ''' </summary>
174
        ''' <param name="Position">Indicates the (X,Y) coordinates.</param>
175
        ''' <returns>The Color of the pixel.</returns>
176
        Public Shared Function GetPixelColor(ByVal Position As Point) As Color
177
178
            Dim hdc As IntPtr = NativeMethods.GetDC(IntPtr.Zero)
179
            Dim pixel As UInteger = NativeMethods.GetPixel(hdc, Position.X, Position.Y)
180
            NativeMethods.ReleaseDC(IntPtr.Zero, hdc)
181
182
            Return Color.FromArgb(CType((pixel And &HFFI), Integer),
183
                                  CType((pixel And &HFF00I), Integer) >> 8I,
184
                                  CType((pixel And &HFF0000I), Integer) >> 16I)
185
186
        End Function
187
188
        ''' <summary>
189-
                                             ByVal y As Integer) As Color
189+
190
        ''' </summary>
191-
            Return GetPixelColor(New Point(X, y))
191+
192
        ''' <param name="Y">Indicates the Y coordinate.</param>
193
        ''' <returns>The Color of the pixel.</returns>
194
        Public Shared Function GetPixelColor(ByVal X As Integer,
195
                                             ByVal Y As Integer) As Color
196
197
            Return GetPixelColor(New Point(X, Y))
198
199
        End Function
200
201
        ''' <summary>
202
        ''' Gets the color of a pixel at the specified coordinates and returns a Brush.
203
        ''' </summary>
204
        ''' <param name="Position">Indicates the (X,Y) coordinates.</param>
205
        ''' <returns>The Brush.</returns>
206
        Public Shared Function GetPixelBrush(ByVal Position As Point) As SolidBrush
207
208
            Using br As New SolidBrush(GetPixelColor(Position.X, Position.Y))
209
                Return br
210
            End Using
211
212
        End Function
213
214
        ''' <summary>
215
        ''' Gets the color of a pixel on the specified coordinates and returns a Brush.
216
        ''' </summary>
217
        ''' <param name="X">Indicates the X coordinate.</param>
218
        ''' <param name="Y">Indicates the Y coordinate.</param>
219
        ''' <returns>The Brush.</returns>
220
        Public Shared Function GetPixelBrush(ByVal X As Integer,
221
                                             ByVal Y As Integer) As SolidBrush
222
223
            Using br As New SolidBrush(GetPixelColor(New Point(X, Y)))
224
                Return br
225
            End Using
226
227
        End Function
228
229
        ''' <summary>
230
        ''' Gets the color of a pixel at the specified coordinates and returns a Pen.
231
        ''' </summary>
232
        ''' <param name="Position">Indicates the (X,Y) coordinates.</param>
233
        ''' <returns>The Pen.</returns>
234
        Public Shared Function GetPixelPen(ByVal Position As Point) As Pen
235
236
            Using p As New Pen(GetPixelColor(Position.X, Position.Y))
237
                Return p
238
            End Using
239
240
        End Function
241
242
        ''' <summary>
243
        ''' Gets the color of a pixel on the specified coordinates and returns a Pen.
244
        ''' </summary>
245
        ''' <param name="X">Indicates the X coordinate.</param>
246
        ''' <param name="Y">Indicates the Y coordinate.</param>
247
        ''' <returns>The Pen.</returns>
248
        Public Shared Function GetPixelPen(ByVal X As Integer,
249
                                             ByVal Y As Integer) As Pen
250
251
            Using p As New Pen(GetPixelColor(New Point(X, Y)))
252
                Return p
253
            End Using
254
255
        End Function
256
257
#End Region
258
259
#Region " Hidden methods "
260
261
        ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
262
        ' NOTE: The methods can be re-enabled at any-time if needed.
263
264
        <EditorBrowsable(EditorBrowsableState.Never)>
265
        Public Shadows Sub Equals()
266
        End Sub
267
268
        <EditorBrowsable(EditorBrowsableState.Never)>
269
        Public Shadows Sub ReferenceEquals()
270
        End Sub
271
272
#End Region
273
274
    End Class
275
276
#End Region
277
278
#Region " Color Convert "
279
280
    ''' <summary>
281
    ''' Convert between different types.
282
    ''' </summary>
283
    Public Class ColorConvert
284
285
#Region " Public Methods "
286
287
        ''' <summary>
288
        ''' Converts a Color to a Brush.
289
        ''' </summary>
290
        ''' <param name="Color">Indicates the color to convert.</param>
291
        ''' <returns>SolidBrush.</returns>
292
        Public Shared Function ColorToBrush(ByVal [Color] As Color) As SolidBrush
293
294
            Using br As New SolidBrush([Color])
295
                Return br
296
            End Using
297
298
        End Function
299
300
        ''' <summary>
301
        ''' Converts a Color to a Pen.
302
        ''' </summary>
303
        ''' <param name="Color">Indicates the color to convert.</param>
304
        ''' <returns>Pen.</returns>
305
        Public Shared Function ColorToPen(ByVal [Color] As Color) As Pen
306
307
            Using p As New Pen([Color])
308
                Return p
309
            End Using
310
311
        End Function
312
313
        ''' <summary>
314
        ''' Converts a Brush to a Color.
315
        ''' </summary>
316
        ''' <param name="Brush">Indicates the Brush to convert.</param>
317
        ''' <returns>Color.</returns>
318
        Public Shared Function BrushToColor(ByVal [Brush] As SolidBrush) As Color
319
320
            Return [Brush].Color
321
322
        End Function
323
324
        ''' <summary>
325
        ''' Converts a Pen to a Color.
326
        ''' </summary>
327
        ''' <param name="Pen">Indicates the Pen to convert.</param>
328
        ''' <returns>Color.</returns>
329
        Public Shared Function PenToColor(ByVal [Pen] As Pen) As Color
330
331
            Return [Pen].Color
332
333
        End Function
334
335
#End Region
336
337
#Region " Hidden methods "
338
339
        ' These methods and properties are purposely hidden from Intellisense just to look better without unneeded methods.
340
        ' NOTE: The methods can be re-enabled at any-time if needed.
341
342
        ''' <summary>
343
        ''' Equalses this instance.
344
        ''' </summary>
345
        <EditorBrowsable(EditorBrowsableState.Never)>
346
        Public Shadows Sub Equals()
347
        End Sub
348
349
        ''' <summary>
350
        ''' Gets the hash code.
351
        ''' </summary>
352
        <EditorBrowsable(EditorBrowsableState.Never)>
353
        Public Shadows Sub ReferenceEquals()
354
        End Sub
355
356
#End Region
357
358
    End Class
359
360
#End Region
361
362
#Region " String Convert "
363
364
    ''' <summary>
365
    ''' Convert between different color strings and types.
366
    ''' </summary>
367
    Public Class StringConvert
368
369
#Region " Enumerations "
370
371
        ''' <summary>
372
        ''' Indicates a color value formatting.
373
        ''' </summary>
374
        Public Enum ValueFormat As Short
375
376
            ''' <summary>
377
            ''' Byte Format (0 to 255).
378
            ''' </summary>
379
            [Byte] = 0
380
381
            ''' <summary>
382
            ''' Hexadecimal Format (0xXXXXXX).
383
            ''' </summary>
384
            Hexadecimal = 1
385
386
            ''' <summary>
387
            ''' HTML Format (#XXXXXX).
388
            ''' </summary>
389
            HTML = 2
390
391
        End Enum
392
393
        ''' <summary>
394
        ''' Indicates a syntax to represent a formatted color value.
395
        ''' </summary>
396
        Public Enum StringSyntax As Short
397
398
            ''' <summary>
399
            ''' Don't use any syntax.
400
            ''' </summary>
401
            None = 0
402
403
            ''' <summary>
404
            ''' Use VB.NET language syntax.
405
            ''' </summary>
406
            VBNET = 1
407
408
            ''' <summary>
409
            ''' Use C# language syntax.
410
            ''' </summary>
411
            CSharp = 2
412
413
            ''' <summary>
414
            ''' Use VisualStudio designer's property grid syntax.
415
            ''' </summary>
416
            VisualStudioPropertyGrid = 3
417
418
        End Enum
419
420
#End Region
421
422
#Region " Public Methods "
423
424
        ''' <summary>
425
        ''' Formats a Color to return a Color String representation for the specified format and/or language syntax.
426
        ''' </summary>
427
        ''' <param name="Color">Indicates the Color to format.</param>
428
        ''' <param name="Format">Indicates the resulting color value formatting.</param>
429
        ''' <param name="Syntax">Indicates the resulting string syntax representation.</param>
430
        ''' <param name="IncludeAlphaChannel">
431
        ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
432
        ''' </param>
433
        ''' <returns>The formatted color String.</returns>
434
        Public Shared Function ColorToString(ByVal [Color] As Color,
435
                                             ByVal Format As ValueFormat,
436
                                             ByVal Syntax As StringSyntax,
437
                                             Optional ByVal IncludeAlphaChannel As Boolean = False) As String
438
439
            Select Case IncludeAlphaChannel
440
441
                Case True
442
                    Return GetARGB([Color], Format, Syntax)
443
444
                Case Else
445
                    Return GetRGB([Color], Format, Syntax)
446
447
            End Select
448
449
        End Function
450
451
        ''' <summary>
452
        ''' Formats a Brush object to return a Color String representation for the specified format and/or language syntax.
453
        ''' </summary>
454
        ''' <param name="Brush">Indicates the Brush object to format.</param>
455
        ''' <param name="Format">Indicates the resulting color value formatting.</param>
456
        ''' <param name="Syntax">Indicates the resulting string syntax representation.</param>
457
        ''' <param name="IncludeAlphaChannel">
458
        ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
459
        ''' </param>
460
        ''' <returns>The formatted color String.</returns>
461
        Public Shared Function BrushToString(ByVal [Brush] As SolidBrush,
462
                                             ByVal Format As ValueFormat,
463
                                             ByVal Syntax As StringSyntax,
464
                                             Optional ByVal IncludeAlphaChannel As Boolean = False) As String
465
466
            Select Case IncludeAlphaChannel
467
468
                Case True
469
                    Return GetARGB([Brush].Color, Format, Syntax)
470
471
                Case Else
472
                    Return GetRGB([Brush].Color, Format, Syntax)
473
474
            End Select
475
476
        End Function
477
478
        ''' <summary>
479
        ''' Formats a Pen object to return a Color String representation for the specified format and/or language syntax.
480
        ''' </summary>
481
        ''' <param name="Pen">Indicates the Pen object to format.</param>
482
        ''' <param name="Format">Indicates the resulting color value formatting.</param>
483
        ''' <param name="Syntax">Indicates the resulting string syntax representation.</param>
484
        ''' <param name="IncludeAlphaChannel">
485
        ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
486
        ''' </param>
487
        ''' <returns>The formatted color String.</returns>
488
        Public Shared Function PenToString(ByVal [Pen] As Pen,
489
                                           ByVal Format As ValueFormat,
490
                                           ByVal Syntax As StringSyntax,
491
                                           Optional ByVal IncludeAlphaChannel As Boolean = False) As String
492
493
            Select Case IncludeAlphaChannel
494
495
                Case True
496
                    Return GetARGB([Pen].Color, Format, Syntax)
497
498
                Case Else
499
                    Return GetRGB([Pen].Color, Format, Syntax)
500
501
            End Select
502
503
        End Function
504
505
        ''' <summary>
506
        ''' Formats a Color String of the specified format and/or language syntax to return a Color.
507
        ''' </summary>
508
        ''' <param name="ColorString">The color string.</param>
509
        ''' <param name="Format">Indicates the value format that the input string is written.</param>
510
        ''' <param name="Syntax">Indicates the string syntax that the input string is written.</param>
511
        ''' <returns>The Color</returns>
512
        Public Shared Function StringToColor(ByVal ColorString As String,
513
                                             ByVal Format As ValueFormat,
514
                                             ByVal Syntax As StringSyntax) As Color
515
516
            Dim Converter As New ColorConverter
517
518
            Select Case Format
519
520
                Case ValueFormat.Byte
521
522
                    Select Case Syntax
523
524
                        Case StringSyntax.None,
525
                            StringSyntax.VisualStudioPropertyGrid
526
                            Return Converter.ConvertFromString(ColorString.Trim.
527
                                                               Replace(",", ";"))
528
529
                        Case StringSyntax.VBNET
530
                            Return Converter.ConvertFromString(ColorString.Trim.ToLower.
531
                                                               Replace("color.fromargb(", Nothing).
532
                                                               Replace(")", Nothing).
533
                                                               Replace(",", ";"))
534
535
                        Case StringSyntax.CSharp
536
                            Return Converter.ConvertFromString(ColorString.Trim.ToLower.
537
                                                               Replace("color.fromargb(", Nothing).
538
                                                               Replace(");", Nothing).
539
                                                               Replace(",", ";"))
540
541
                        Case Else
542
                            Return Color.Empty
543
544
                    End Select
545
546
                Case ValueFormat.Hexadecimal
547
548
                    Select Case Syntax
549
550
                        Case StringSyntax.None,
551
                            StringSyntax.VisualStudioPropertyGrid
552
                            Return ColorTranslator.FromHtml(ColorString.Trim.Replace("0x", "#"))
553
554
                        Case StringSyntax.VBNET
555
                            Return Converter.ConvertFromString("&H" & ColorString.Trim.ToLower.
556
                                                               Replace("color.fromargb(", Nothing).
557
                                                               Replace(")", Nothing).
558
                                                               Replace(",", Nothing).
559
                                                               Replace("&h", Nothing).
560
                                                               Replace(" ", Nothing))
561
562
                        Case StringSyntax.CSharp
563
                            Return Converter.ConvertFromString("&H" & ColorString.Trim.ToLower.
564
                                                               Replace("color.fromargb(", Nothing).
565
                                                               Replace(");", Nothing).
566
                                                               Replace(",", Nothing).
567
                                                               Replace("0x", Nothing).
568
                                                               Replace(" ", Nothing))
569
570
                        Case Else
571
                            Return Color.Empty
572
573
                    End Select
574
575
                Case ValueFormat.HTML
576
577
                    Select Case Syntax
578
579
                        Case StringSyntax.None,
580
                            StringSyntax.VisualStudioPropertyGrid
581
                            Return ColorTranslator.FromHtml(ColorString.Trim)
582
583
                        Case StringSyntax.VBNET
584
                            Return ColorTranslator.FromHtml(ColorString.Trim.ToLower.
585
                                                            Replace("colortranslator.fromhtml(", Nothing).
586
                                                            Replace(")", Nothing))
587
588
                        Case StringSyntax.CSharp
589
                            Return ColorTranslator.FromHtml(ColorString.Trim.ToLower.
590
                                                            Replace("colortranslator.fromhtml(", Nothing).
591
                                                            Replace(");", Nothing))
592
593
                        Case Else
594
                            Return Color.Empty
595
596
                    End Select
597
598
            End Select
599
600
        End Function
601
602
        ''' <summary>
603
        ''' Formats a Color String of the specified format and/or language syntax to return a Brush.
604
        ''' </summary>
605
        ''' <param name="ColorString">The color string.</param>
606
        ''' <param name="Format">Indicates the value format that the input string is written.</param>
607
        ''' <param name="Syntax">Indicates the string syntax that the input string is written.</param>
608
        ''' <returns>The Brush</returns>
609
        Public Shared Function StringToBrush(ByVal ColorString As String,
610
                                             ByVal Format As ValueFormat,
611
                                             ByVal Syntax As StringSyntax) As SolidBrush
612
613
            Using br As New SolidBrush(StringToColor(ColorString, Format, Syntax))
614
                Return br
615
            End Using
616
617
        End Function
618
619
        ''' <summary>
620
        ''' Formats a Color String of the specified format and/or language syntax to return a Pen.
621
        ''' </summary>
622
        ''' <param name="ColorString">The color string.</param>
623
        ''' <param name="Format">Indicates the value format that the input string is written.</param>
624
        ''' <param name="Syntax">Indicates the string syntax that the input string is written.</param>
625
        ''' <returns>The Pen</returns>
626
        Public Shared Function StringToPen(ByVal ColorString As String,
627
                                           ByVal Format As ValueFormat,
628
                                           ByVal Syntax As StringSyntax) As Pen
629
630
            Using p As New Pen(StringToColor(ColorString, Format, Syntax))
631
                Return p
632
            End Using
633
634
        End Function
635
636
        ''' <summary>
637
        ''' Formats a Color String of the specified format and/or language syntax to return a 
638
        ''' new String of other format and/or language syntax.
639
        ''' </summary>
640
        ''' <param name="ColorString">The color string.</param>
641
        ''' <param name="Format">The format in which the input string is written.</param>
642
        ''' <param name="Syntax">The syntax of the input string (if any).</param>
643
        ''' <param name="NewFormat">The format in which the input string is written.</param>
644
        ''' <param name="NewSyntax">The syntax of the input string (if any).</param>
645
        ''' <param name="IncludeAlphaChannel">
646
        ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
647
        ''' </param>
648
        ''' <returns>The Color</returns>
649
        Public Shared Function StringToString(ByVal ColorString As String,
650
                                              ByVal Format As ValueFormat,
651
                                              ByVal Syntax As StringSyntax,
652
                                              ByVal NewFormat As ValueFormat,
653
                                              ByVal NewSyntax As StringSyntax,
654
                                              Optional ByVal IncludeAlphaChannel As Boolean = False) As String
655
656
            Dim Converter As New ColorConverter
657
658
            Select Case Format
659
660
                Case ValueFormat.Byte
661
662
                    Select Case Syntax
663
                        Case StringSyntax.None,
664
                             StringSyntax.VisualStudioPropertyGrid
665
666
                            ColorString = ColorString.Trim.
667
                                          Replace(",", ";")
668
669
                        Case StringSyntax.VBNET
670
                            ColorString = ColorString.Trim.ToLower.
671
                                          Replace("color.fromargb(", Nothing).
672
                                          Replace(")", Nothing).
673
                                          Replace(",", ";")
674
675
                        Case StringSyntax.CSharp
676
                            ColorString = ColorString.Trim.ToLower.
677
                                          Replace("color.fromargb(", Nothing).
678
                                          Replace(");", Nothing).
679
                                          Replace(",", ";")
680
681
                        Case Else
682
                            ColorString = String.Empty
683
684
                    End Select
685
686
                Case ValueFormat.Hexadecimal
687
688
                    Select Case Syntax
689
690
                        Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
691
                            ColorString = ColorString.Trim.Replace("0x", "#")
692
693
                        Case StringSyntax.VBNET
694
                            ColorString = "&H" & ColorString.Trim.ToLower.
695
                                          Replace("color.fromargb(", Nothing).
696
                                          Replace(")", Nothing).
697
                                          Replace(",", Nothing).
698
                                          Replace("&h", Nothing).
699
                                          Replace(" ", Nothing)
700
701
                        Case StringSyntax.CSharp
702
                            ColorString = ColorString.Trim.ToLower.
703
                                          Replace("color.fromargb(", Nothing).
704
                                          Replace(");", Nothing).
705
                                          Replace(",", Nothing).
706
                                          Replace("0x", Nothing).
707
                                          Replace(" ", Nothing)
708
709
                        Case Else
710
                            ColorString = String.Empty
711
712
                    End Select
713
714
                Case ValueFormat.HTML
715
716
                    Select Case Syntax
717
718
                        Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
719
                            ColorString = ColorString
720
721
                        Case StringSyntax.VBNET
722
                            ColorString = ColorString.Trim.ToLower.
723
                                          Replace("colortranslator.fromhtml(", Nothing).
724
                                          Replace(")", Nothing).
725
                                          Replace("""", Nothing)
726
727
                        Case StringSyntax.CSharp
728
                            ColorString = ColorString.Trim.ToLower.
729
                                          Replace("colortranslator.fromhtml(", Nothing).
730
                                          Replace(");", Nothing).
731
                                          Replace("""", Nothing)
732
733
                        Case Else
734
                            ColorString = String.Empty
735
736
                    End Select
737
738
            End Select
739
740
            Select Case IncludeAlphaChannel
741
742
                Case True
743
                    Return GetARGB(Converter.ConvertFromString(ColorString), NewFormat, NewSyntax)
744
745
                Case Else
746
                    Return GetRGB(Converter.ConvertFromString(ColorString), NewFormat, NewSyntax)
747
748
            End Select
749
750
        End Function
751
752
#End Region
753
754
#Region " Private Methods "
755
756
        ''' <summary>
757
        ''' Formats a Color to return an RGB String representation for the specified format and/or language syntax.
758
        ''' </summary>
759
        ''' <param name="Color">Indicates the color to format.</param>
760
        ''' <param name="Format">Indicates the resulting RGB value formatting.</param>
761
        ''' <param name="Syntax">Indicates the resulting string syntax to represent the formatted RGB value.</param>
762
        ''' <returns>The formatted RGB String.</returns>
763
        Private Shared Function GetRGB(ByVal [Color] As Color,
764
                                       ByVal Format As ValueFormat,
765
                                       ByVal Syntax As StringSyntax) As String
766
767
            Select Case Format
768
769
                Case ValueFormat.Byte
770
771
                    Dim INTstr As String =
772
                        String.Format("{0}, {1}, {2}",
773
                                      Convert.ToString([Color].R),
774
                                      Convert.ToString([Color].G),
775
                                      Convert.ToString([Color].B))
776
777
                    Select Case Syntax
778
779
                        Case StringSyntax.None
780
                            Return INTstr
781
782
                        Case StringSyntax.VBNET
783
                            Return String.Format("Color.FromArgb({0})", INTstr)
784
785
                        Case StringSyntax.CSharp
786
                            Return String.Format("Color.FromArgb({0});", INTstr)
787
788
                        Case StringSyntax.VisualStudioPropertyGrid
789
                            Return INTstr.Replace(",", ";")
790
791
                        Case Else
792
                            Return String.Empty
793
794
                    End Select
795
796
                Case ValueFormat.Hexadecimal
797
798
                    Dim HEXstr As String =
799
                        String.Format("0x{0}{1}{2}",
800
                                      Convert.ToString([Color].R, 16).ToUpper,
801
                                      Convert.ToString([Color].G, 16).ToUpper,
802
                                      Convert.ToString([Color].B, 16).ToUpper)
803
804
                    Select Case Syntax
805
806
                        Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
807
                            Return HEXstr
808
809
                        Case StringSyntax.VBNET
810
                            Return String.Format("Color.FromArgb(&H{0}, &H{1}, &H{2})",
811
                                                 Convert.ToString([Color].R, 16).ToUpper,
812
                                                 Convert.ToString([Color].G, 16).ToUpper,
813
                                                 Convert.ToString([Color].B, 16).ToUpper)
814
815
                        Case StringSyntax.CSharp
816
                            Return String.Format("Color.FromArgb(0x{0}, 0x{1}, 0x{2});",
817
                                                 Convert.ToString([Color].R, 16).ToUpper,
818
                                                 Convert.ToString([Color].G, 16).ToUpper,
819
                                                 Convert.ToString([Color].B, 16).ToUpper)
820
821
                        Case Else
822
                            Return String.Empty
823
824
                    End Select
825
826
                Case ValueFormat.HTML
827
828
                    Dim HTMLstr As String =
829
                        ColorTranslator.ToHtml([Color])
830
831
                    Select Case Syntax
832
833
                        Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
834
                            Return HTMLstr
835
836
                        Case StringSyntax.VBNET
837
                            Return String.Format("ColorTranslator.FromHtml(""{0}"")", HTMLstr)
838
839
                        Case StringSyntax.CSharp
840
                            Return String.Format("ColorTranslator.FromHtml(""{0}"");", HTMLstr)
841
842
                        Case Else
843
                            Return String.Empty
844
845
                    End Select
846
847
                Case Else
848
                    Return String.Empty
849
850
            End Select
851
852
        End Function
853
854
        ''' <summary>
855
        ''' Formats a Color to return an ARGB String representation for the specified format and/or language syntax.
856
        ''' </summary>
857
        ''' <param name="Color">Indicates the color to format.</param>
858
        ''' <param name="Format">Indicates the resulting ARGB value formatting.</param>
859
        ''' <param name="Syntax">Indicates the resulting string syntax to represent the formatted ARGB value.</param>
860
        ''' <returns>The formatted ARGB String.</returns>
861
        Private Shared Function GetARGB(ByVal [Color] As Color,
862
                                        ByVal Format As ValueFormat,
863
                                        ByVal Syntax As StringSyntax) As String
864
865
            Select Case Format
866
867
                Case ValueFormat.Byte
868
869
                    Dim INTstr As String =
870
                        String.Format("{0}, {1}, {2}, {3}",
871
                                      Convert.ToString([Color].A),
872
                                      Convert.ToString([Color].R),
873
                                      Convert.ToString([Color].G),
874
                                      Convert.ToString([Color].B))
875
876
                    Select Case Syntax
877
878
                        Case StringSyntax.None
879
                            Return INTstr
880
881
                        Case StringSyntax.VBNET
882
                            Return String.Format("Color.FromArgb({0})", INTstr)
883
884
                        Case StringSyntax.CSharp
885
                            Return String.Format("Color.FromArgb({0});", INTstr)
886
887
                        Case StringSyntax.VisualStudioPropertyGrid
888
                            Return INTstr.Replace(",", ";")
889
890
                        Case Else
891
                            Return String.Empty
892
893
                    End Select
894
895
                Case ValueFormat.Hexadecimal
896
897
                    Dim HEXstr As String =
898
                        String.Format("0x{0}{1}{2}{3}",
899
                                      Convert.ToString([Color].A, 16).ToUpper,
900
                                      Convert.ToString([Color].R, 16).ToUpper,
901
                                      Convert.ToString([Color].G, 16).ToUpper,
902
                                      Convert.ToString([Color].B, 16).ToUpper)
903
904
                    Select Case Syntax
905
906
                        Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
907
                            Return HEXstr
908
909
                        Case StringSyntax.VBNET
910
                            Return String.Format("Color.FromArgb(&H{0}, &H{1}, &H{2}, &H{3})",
911
                                                 Convert.ToString([Color].A, 16).ToUpper,
912
                                                 Convert.ToString([Color].R, 16).ToUpper,
913
                                                 Convert.ToString([Color].G, 16).ToUpper,
914
                                                 Convert.ToString([Color].B, 16).ToUpper)
915
916
                        Case StringSyntax.CSharp
917
                            Return String.Format("Color.FromArgb(0x{0}, 0x{1}, 0x{2}, 0x{3});",
918
                                                 Convert.ToString([Color].A, 16).ToUpper,
919
                                                 Convert.ToString([Color].R, 16).ToUpper,
920
                                                 Convert.ToString([Color].G, 16).ToUpper,
921
                                                 Convert.ToString([Color].B, 16).ToUpper)
922
923
                        Case Else
924
                            Return String.Empty
925
926
                    End Select
927
928
                Case ValueFormat.HTML
929
930
                    Dim HTMLstr As String =
931
                        ColorTranslator.ToHtml([Color])
932
933
                    Select Case Syntax
934
935
                        Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
936
                            Return HTMLstr
937
938
                        Case StringSyntax.VBNET
939
                            Return String.Format("ColorTranslator.FromHtml(""{0}"")", HTMLstr)
940
941
                        Case StringSyntax.CSharp
942
                            Return String.Format("ColorTranslator.FromHtml(""{0}"");", HTMLstr)
943
944
                        Case Else
945
                            Return String.Empty
946
947
                    End Select
948
949
                Case Else
950
                    Return String.Empty
951
952
            End Select
953
954
        End Function
955
956
#End Region
957
958
#Region " Hidden methods "
959
960
        ' These methods and properties are purposely hidden from Intellisense just to look better without unneeded methods.
961
        ' NOTE: The methods can be re-enabled at any-time if needed.
962
963
        <EditorBrowsable(EditorBrowsableState.Never)>
964
        Public Shadows Sub Equals()
965
        End Sub
966
967
        <EditorBrowsable(EditorBrowsableState.Never)>
968
        Public Shadows Sub ReferenceEquals()
969
        End Sub
970
971
#End Region
972
973
    End Class
974
975
#End Region
976
977
#Region " Random Generators "
978
979
    ''' <summary>
980
    ''' Random Color generators.
981
    ''' </summary>
982
    Public Class RandomGenerators
983
984
#Region " Public Methods "
985
986
        ''' <summary>
987
        ''' Generates a random RGB color.
988
        ''' </summary>
989
        ''' <returns>Color.</returns>
990
        Public Shared Function RGB() As Color
991
992
            Dim Rand As New Random
993
            Return Color.FromArgb(255,
994
                                  Rand.Next(0, 255),
995
                                  Rand.Next(0, 255),
996
                                  Rand.Next(0, 255))
997
998
        End Function
999
1000
        ''' <summary>
1001
        ''' Generates a random RGB color between the specified minimum/maximum values.
1002
        ''' </summary>
1003
        ''' <param name="RMin">Indicates the RED channel minimum value.</param>
1004
        ''' <param name="RMax">Indicates the RED channel maximum value.</param>
1005
        ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
1006
        ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
1007
        ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
1008
        ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
1009
        ''' <returns>Color.</returns>
1010
        Public Shared Function RGB(Optional ByVal RMin As Byte = 0,
1011
                                   Optional ByVal RMax As Byte = 255,
1012
                                   Optional ByVal GMin As Byte = 0,
1013
                                   Optional ByVal GMax As Byte = 255,
1014
                                   Optional ByVal BMin As Byte = 0,
1015
                                   Optional ByVal BMax As Byte = 255) As Color
1016
1017
            Dim Rand As New Random
1018
            Return Color.FromArgb(255,
1019
                                  Rand.Next(RMin, RMax),
1020
                                  Rand.Next(GMin, GMax),
1021
                                  Rand.Next(BMin, BMax))
1022
1023
        End Function
1024
1025
        ''' <summary>
1026
        ''' Generates a random ARGB color.
1027
        ''' </summary>
1028
        ''' <returns>Color.</returns>
1029
        Public Shared Function ARGB() As Color
1030
1031
            Dim Rand As New Random
1032
            Return Color.FromArgb(Rand.Next(0, 255),
1033
                                  Rand.Next(0, 255),
1034
                                  Rand.Next(0, 255),
1035
                                  Rand.Next(0, 255))
1036
1037
        End Function
1038
1039
        ''' <summary>
1040
        ''' Generates a random RGB color between the specified minimum/maximum values.
1041
        ''' </summary>
1042
        ''' <param name="AMin">Indicates the ALPHA channel minimum value.</param>
1043
        ''' <param name="AMax">Indicates the ALPHA channel maximum value.</param>
1044
        ''' <param name="RMin">Indicates the RED channel minimum value.</param>
1045
        ''' <param name="RMax">Indicates the RED channel maximum value.</param>
1046
        ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
1047
        ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
1048
        ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
1049
        ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
1050
        ''' <returns>Color.</returns>
1051
        Public Shared Function ARGB(Optional ByVal AMin As Byte = 0,
1052
                                    Optional ByVal AMax As Byte = 255,
1053
                                    Optional ByVal RMin As Byte = 0,
1054
                                    Optional ByVal RMax As Byte = 255,
1055
                                    Optional ByVal GMin As Byte = 0,
1056
                                    Optional ByVal GMax As Byte = 255,
1057
                                    Optional ByVal BMin As Byte = 0,
1058
                                    Optional ByVal BMax As Byte = 255) As Color
1059
1060
            Dim Rand As New Random
1061
            Return Color.FromArgb(Rand.Next(AMin, AMax),
1062
                                  Rand.Next(RMin, RMax),
1063
                                  Rand.Next(GMin, GMax),
1064
                                  Rand.Next(BMin, BMax))
1065
1066
        End Function
1067
1068
        ''' <summary>
1069
        ''' Generates a random QB color.
1070
        ''' </summary>
1071
        ''' <returns>Color.</returns>
1072
        Public Shared Function QB() As Color
1073
1074
            Dim Rand As New Random
1075
            Dim c As Color = Color.FromName([Enum].Parse(GetType(ConsoleColor),
1076
                                                         Rand.Next(0, 15)).ToString)
1077
1078
            Select Case c.IsKnownColor
1079
1080
                ' Fix for the 'Consolecolor.DarkYellow' value which doesn't have color information.
1081
                Case False
1082
                    Return Color.FromArgb(255, 128, 128, 0)
1083
1084
                Case Else
1085
                    Return c
1086
1087
            End Select
1088
1089
        End Function
1090
1091
        ''' <summary>
1092
        ''' Generates a random ConsoleColor color.
1093
        ''' </summary>
1094
        ''' <returns>ConsoleColor.</returns>
1095
        Public Shared Function [ConsoleColor]() As ConsoleColor
1096
1097
            Dim Rand As New Random
1098
            Return [Enum].Parse(GetType(ConsoleColor), Rand.Next(0, 15))
1099
1100
        End Function
1101
1102
        ''' <summary>
1103
        ''' Generates a random Brush.
1104
        ''' </summary>
1105
        ''' <returns>SolidBrush.</returns>
1106
        Public Shared Function [Brush]() As SolidBrush
1107
1108
            Using br As New SolidBrush(RGB)
1109
                Return br
1110
            End Using
1111
1112
        End Function
1113
1114
        ''' <summary>
1115
        ''' Generates a random Brush between the specified minimum/maximum values.
1116
        ''' </summary>
1117
        ''' <param name="AMin">Indicates the ALPHA channel minimum value.</param>
1118
        ''' <param name="AMax">Indicates the ALPHA channel maximum value.</param>
1119
        ''' <param name="RMin">Indicates the RED channel minimum value.</param>
1120
        ''' <param name="RMax">Indicates the RED channel maximum value.</param>
1121
        ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
1122
        ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
1123
        ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
1124
        ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
1125
        ''' <returns>SolidBrush.</returns>
1126
        Public Shared Function [Brush](Optional ByVal AMin As Byte = 0,
1127
                                       Optional ByVal AMax As Byte = 255,
1128
                                       Optional ByVal RMin As Byte = 0,
1129
                                       Optional ByVal RMax As Byte = 255,
1130
                                       Optional ByVal GMin As Byte = 0,
1131
                                       Optional ByVal GMax As Byte = 255,
1132
                                       Optional ByVal BMin As Byte = 0,
1133
                                       Optional ByVal BMax As Byte = 255) As SolidBrush
1134
1135
            Using br As New SolidBrush(ARGB(AMin, AMax, RMin, RMax, GMin, GMax, BMin, BMax))
1136
                Return br
1137
            End Using
1138
1139
        End Function
1140
1141
        ''' <summary>
1142
        ''' Generates a random Pen.
1143
        ''' </summary>
1144
        ''' <returns>Pen.</returns>
1145
        Public Shared Function [Pen]() As Pen
1146
1147
            Using p As New [Pen](RGB)
1148
                Return p
1149
            End Using
1150
1151
        End Function
1152
1153
        ''' <summary>
1154
        ''' Generates a random Pen between the specified minimum/maximum values.
1155
        ''' </summary>
1156
        ''' <param name="AMin">Indicates the ALPHA channel minimum value.</param>
1157
        ''' <param name="AMax">Indicates the ALPHA channel maximum value.</param>
1158
        ''' <param name="RMin">Indicates the RED channel minimum value.</param>
1159
        ''' <param name="RMax">Indicates the RED channel maximum value.</param>
1160
        ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
1161
        ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
1162
        ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
1163
        ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
1164
        ''' <returns>Pen.</returns>
1165
        Public Shared Function [Pen](Optional ByVal AMin As Byte = 0,
1166
                                     Optional ByVal AMax As Byte = 255,
1167
                                     Optional ByVal RMin As Byte = 0,
1168
                                     Optional ByVal RMax As Byte = 255,
1169
                                     Optional ByVal GMin As Byte = 0,
1170
                                     Optional ByVal GMax As Byte = 255,
1171
                                     Optional ByVal BMin As Byte = 0,
1172
                                     Optional ByVal BMax As Byte = 255) As Pen
1173
1174
            Using p As New [Pen](ARGB(AMin, AMax, RMin, RMax, GMin, GMax, BMin, BMax))
1175
                Return p
1176
            End Using
1177
1178
        End Function
1179
1180
#End Region
1181
1182
#Region " Hidden methods "
1183
1184
        ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
1185
        ' NOTE: The methods can be re-enabled at any-time if needed.
1186
1187
        <EditorBrowsable(EditorBrowsableState.Never)>
1188
        Public Shadows Sub Equals()
1189
        End Sub
1190
1191
        <EditorBrowsable(EditorBrowsableState.Never)>
1192
        Public Shadows Sub ReferenceEquals()
1193
        End Sub
1194
1195
#End Region
1196
1197
    End Class
1198
1199
#End Region
1200
1201
#Region " Hidden methods "
1202
1203
    ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
1204
    ' NOTE: The methods can be re-enabled at any-time if needed.
1205
1206
    <EditorBrowsable(EditorBrowsableState.Never)>
1207
    Public Shadows Sub Equals()
1208
    End Sub
1209
1210
    <EditorBrowsable(EditorBrowsableState.Never)>
1211
    Public Shadows Sub ReferenceEquals()
1212
    End Sub
1213
1214
#End Region
1215
1216
End Class