Advertisement
PiToLoKo

ColorTools by Elektro

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