Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Created : 01-13-2014
- ' Modified : 01-13-2014
- ' ***********************************************************************
- ' <copyright file="ColorTools.vb" company="Elektro Studios">
- ' Copyright (c) Elektro Studios. All rights reserved.
- ' </copyright>
- ' ***********************************************************************
- '
- ' --------------
- ' Public Methods
- ' --------------
- '
- ' Screen.GetPixelColor
- ' Screen.GetPixelBrush
- ' Screen.GetPixelPen
- '
- ' ColorConvert.ColorToBrush
- ' ColorConvert.ColorToPen
- ' ColorConvert.BrushToColor
- ' ColorConvert.PentoColor
- '
- ' StringConvert.ColorToString
- ' StringConvert.BrushToString
- ' StringConvert.PenToString
- ' StringConvert.StringToColor
- ' StringConvert.StringToBrush
- ' StringConvert.StringToPen
- ' StringConvert.StringToString
- '
- ' RandomGenerators.ARGB
- ' RandomGenerators.RGB
- ' RandomGenerators.QB
- ' RandomGenerators.ConsoleColor
- ' RandomGenerators.Brush
- ' RandomGenerators.Pen
- #Region " Usage Examples "
- '' Gets the color of the pixel at the 50,100 coordinates:
- ' Dim c As Color = ColorTools.Screen.GetPixelColor(50, 100)
- '' Generates a random Brush
- ' Dim br As SolidBrush = ColorTools.RandomGenerators.Brush
- '' Converts a SolidBrush to a Color:
- ' Dim c As Color = ColorTools.ColorConvert.BrushToColor(New SolidBrush(Color.Red))
- '' Converts an HTML Color-String to a Color:
- ' PictureBox1.BackColor = ColorTools.StringConvert.StringToColor("#FF00FFFF",
- ' ColorTools.StringConvert.ValueFormat.HTML,
- ' ColorTools.StringConvert.StringSyntax.None)
- '' Converts an Hex Color-String to a Color:
- ' MsgBox(ColorTools.StringConvert.StringToColor("0x003399",
- ' ColorTools.StringConvert.ValueFormat.Hexadecimal,
- ' ColorTools.StringConvert.StringSyntax.None))
- '' Converts a Byte Color-String with VisyalStudio's property grid syntax to a Color:
- ' MsgBox(ColorTools.StringConvert.StringToColor("255; 255; 255; 255",
- ' ColorTools.StringConvert.ValueFormat.Byte,
- ' ColorTools.StringConvert.StringSyntax.VisualStudioPropertyGrid).
- ' Name)
- '' Converts a HEX Color-String with VB.NET syntax to a Color:
- ' MsgBox(ColorTools.StringConvert.StringToColor("Color.FromArgb(&HFF, &H5F, &HEC, &H12)",
- ' ColorTools.StringConvert.ValueFormat.Hexadecimal,
- ' ColorTools.StringConvert.StringSyntax.VBNET).
- ' ToString)
- '' Converts an HTML Color-String with C# Syntax to a Brush:
- ' Dim br As Brush = ColorTools.StringConvert.StringToBrush("ColorTranslator.FromHtml(""#F71608"");",
- ' ColorTools.StringConvert.ValueFormat.HTML,
- ' ColorTools.StringConvert.StringSyntax.VBNET)
- '' Converts a Color-String to other Color-String:
- ' MsgBox(ColorTools.StringConvert.StringToString("ColorTranslator.FromHtml(""#AF0CCAFE"");",
- ' ColorTools.StringConvert.ValueFormat.HTML,
- ' ColorTools.StringConvert.StringSyntax.CSharp,
- ' ColorTools.StringConvert.ValueFormat.Byte,
- ' ColorTools.StringConvert.StringSyntax.None,
- ' True))
- #End Region
- #Region " Imports "
- Imports System.Runtime.InteropServices
- Imports System.ComponentModel
- #End Region
- ''' <summary>
- ''' Native API Methods.
- ''' </summary>
- Friend NotInheritable Class NativeMethods
- #Region " API Methods "
- ''' <summary>
- ''' Retrieves the red, green, blue (RGB) color value of the pixel at the specified coordinates.
- ''' </summary>
- ''' <param name="hdc">A handle to the device context.</param>
- ''' <param name="nXPos">The x-coordinate, in logical units, of the pixel to be examined.</param>
- ''' <param name="nYPos">The y-coordinate, in logical units, of the pixel to be examined.</param>
- ''' <returns>
- ''' The return value is the COLORREF value that specifies the RGB of the pixel.
- ''' If the pixel is outside of the current clipping region, the return value is CLR_INVALID (0xFFFFFFFF)
- ''' </returns>
- <DllImport("gdi32.dll", SetLastError:=True)>
- Friend Shared Function GetPixel(hdc As IntPtr,
- ByVal nXPos As Integer,
- ByVal nYPos As Integer
- ) As UInteger
- End Function
- ''' <summary>
- ''' Retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen.
- ''' You can use the returned handle in subsequent GDI functions to draw in the DC.
- ''' The device context is an opaque data structure, whose values are used internally by GDI.
- ''' </summary>
- ''' <param name="hwnd">
- ''' A handle to the window whose DC is to be retrieved.
- ''' If this value is NULL, GetDC retrieves the DC for the entire screen.
- ''' </param>
- ''' <returns>
- ''' If the function succeeds, the return value is a handle to the DC for the specified window's client area.
- ''' If the function fails, the return value is NULL.
- ''' </returns>
- <DllImport("user32.dll", SetLastError:=False)>
- Friend Shared Function GetDC(
- ByVal hwnd As IntPtr
- ) As IntPtr
- End Function
- ''' <summary>
- ''' Releases a device context (DC), freeing it for use by other applications.
- ''' The effect of the ReleaseDC function depends on the type of DC.
- ''' It frees only common and window DCs.
- ''' It has no effect on class or private DCs.
- ''' </summary>
- ''' <param name="hWnd">A handle to the window whose DC is to be released.</param>
- ''' <param name="hDC">A handle to the DC to be released.</param>
- ''' <returns><c>true</c> if the DC was released, <c>false</c> if the DC was not released.</returns>
- <DllImport("user32.dll", SetLastError:=False)>
- Friend Shared Function ReleaseDC(
- ByVal hWnd As IntPtr,
- ByVal hDC As IntPtr
- ) As <MarshalAs(UnmanagedType.Bool)> Boolean
- End Function
- #End Region
- End Class
- ''' <summary>
- ''' Tools related about colors.
- ''' </summary>
- Public Class ColorTools
- #Region " Screen "
- ''' <summary>
- ''' Screen utilities related about colors.
- ''' </summary>
- Public Class Screen
- #Region " Public Methods "
- ''' <summary>
- ''' Gets the color of a pixel on the specified coordinates.
- ''' </summary>
- ''' <param name="Position">Indicates the (X,Y) coordinates.</param>
- ''' <returns>The Color of the pixel.</returns>
- Public Shared Function GetPixelColor(ByVal Position As Point) As Color
- Dim hdc As IntPtr = NativeMethods.GetDC(IntPtr.Zero)
- Dim pixel As UInteger = NativeMethods.GetPixel(hdc, Position.X, Position.Y)
- NativeMethods.ReleaseDC(IntPtr.Zero, hdc)
- Return Color.FromArgb(CType((pixel And &HFFI), Integer),
- CType((pixel And &HFF00I), Integer) >> 8I,
- CType((pixel And &HFF0000I), Integer) >> 16I)
- End Function
- ''' <summary>
- ''' Gets the color of a pixel on the specified coordinates.
- ''' </summary>
- ''' <param name="X">Indicates the X coordinate.</param>
- ''' <param name="Y">Indicates the Y coordinate.</param>
- ''' <returns>The Color of the pixel.</returns>
- Public Shared Function GetPixelColor(ByVal X As Integer,
- ByVal Y As Integer) As Color
- Return GetPixelColor(New Point(X, Y))
- End Function
- ''' <summary>
- ''' Gets the color of a pixel at the specified coordinates and returns a Brush.
- ''' </summary>
- ''' <param name="Position">Indicates the (X,Y) coordinates.</param>
- ''' <returns>The Brush.</returns>
- Public Shared Function GetPixelBrush(ByVal Position As Point) As SolidBrush
- Using br As New SolidBrush(GetPixelColor(Position.X, Position.Y))
- Return br
- End Using
- End Function
- ''' <summary>
- ''' Gets the color of a pixel on the specified coordinates and returns a Brush.
- ''' </summary>
- ''' <param name="X">Indicates the X coordinate.</param>
- ''' <param name="Y">Indicates the Y coordinate.</param>
- ''' <returns>The Brush.</returns>
- Public Shared Function GetPixelBrush(ByVal X As Integer,
- ByVal Y As Integer) As SolidBrush
- Using br As New SolidBrush(GetPixelColor(New Point(X, Y)))
- Return br
- End Using
- End Function
- ''' <summary>
- ''' Gets the color of a pixel at the specified coordinates and returns a Pen.
- ''' </summary>
- ''' <param name="Position">Indicates the (X,Y) coordinates.</param>
- ''' <returns>The Pen.</returns>
- Public Shared Function GetPixelPen(ByVal Position As Point) As Pen
- Using p As New Pen(GetPixelColor(Position.X, Position.Y))
- Return p
- End Using
- End Function
- ''' <summary>
- ''' Gets the color of a pixel on the specified coordinates and returns a Pen.
- ''' </summary>
- ''' <param name="X">Indicates the X coordinate.</param>
- ''' <param name="Y">Indicates the Y coordinate.</param>
- ''' <returns>The Pen.</returns>
- Public Shared Function GetPixelPen(ByVal X As Integer,
- ByVal Y As Integer) As Pen
- Using p As New Pen(GetPixelColor(New Point(X, Y)))
- Return p
- End Using
- End Function
- #End Region
- #Region " Hidden methods "
- ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
- ' NOTE: The methods can be re-enabled at any-time if needed.
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub Equals()
- End Sub
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub ReferenceEquals()
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Color Convert "
- ''' <summary>
- ''' Convert between different types.
- ''' </summary>
- Public Class ColorConvert
- #Region " Public Methods "
- ''' <summary>
- ''' Converts a Color to a Brush.
- ''' </summary>
- ''' <param name="Color">Indicates the color to convert.</param>
- ''' <returns>SolidBrush.</returns>
- Public Shared Function ColorToBrush(ByVal [Color] As Color) As SolidBrush
- Using br As New SolidBrush([Color])
- Return br
- End Using
- End Function
- ''' <summary>
- ''' Converts a Color to a Pen.
- ''' </summary>
- ''' <param name="Color">Indicates the color to convert.</param>
- ''' <returns>Pen.</returns>
- Public Shared Function ColorToPen(ByVal [Color] As Color) As Pen
- Using p As New Pen([Color])
- Return p
- End Using
- End Function
- ''' <summary>
- ''' Converts a Brush to a Color.
- ''' </summary>
- ''' <param name="Brush">Indicates the Brush to convert.</param>
- ''' <returns>Color.</returns>
- Public Shared Function BrushToColor(ByVal [Brush] As SolidBrush) As Color
- Return [Brush].Color
- End Function
- ''' <summary>
- ''' Converts a Pen to a Color.
- ''' </summary>
- ''' <param name="Pen">Indicates the Pen to convert.</param>
- ''' <returns>Color.</returns>
- Public Shared Function PenToColor(ByVal [Pen] As Pen) As Color
- Return [Pen].Color
- End Function
- #End Region
- #Region " Hidden methods "
- ' These methods and properties are purposely hidden from Intellisense just to look better without unneeded methods.
- ' NOTE: The methods can be re-enabled at any-time if needed.
- ''' <summary>
- ''' Equalses this instance.
- ''' </summary>
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub Equals()
- End Sub
- ''' <summary>
- ''' Gets the hash code.
- ''' </summary>
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub ReferenceEquals()
- End Sub
- #End Region
- End Class
- #End Region
- #Region " String Convert "
- ''' <summary>
- ''' Convert between different color strings and types.
- ''' </summary>
- Public Class StringConvert
- #Region " Enumerations "
- ''' <summary>
- ''' Indicates a color value formatting.
- ''' </summary>
- Public Enum ValueFormat As Short
- ''' <summary>
- ''' Byte Format (0 to 255).
- ''' </summary>
- [Byte] = 0
- ''' <summary>
- ''' Hexadecimal Format (0xXXXXXX).
- ''' </summary>
- Hexadecimal = 1
- ''' <summary>
- ''' HTML Format (#XXXXXX).
- ''' </summary>
- HTML = 2
- End Enum
- ''' <summary>
- ''' Indicates a syntax to represent a formatted color value.
- ''' </summary>
- Public Enum StringSyntax As Short
- ''' <summary>
- ''' Don't use any syntax.
- ''' </summary>
- None = 0
- ''' <summary>
- ''' Use VB.NET language syntax.
- ''' </summary>
- VBNET = 1
- ''' <summary>
- ''' Use C# language syntax.
- ''' </summary>
- CSharp = 2
- ''' <summary>
- ''' Use VisualStudio designer's property grid syntax.
- ''' </summary>
- VisualStudioPropertyGrid = 3
- End Enum
- #End Region
- #Region " Public Methods "
- ''' <summary>
- ''' Formats a Color to return a Color String representation for the specified format and/or language syntax.
- ''' </summary>
- ''' <param name="Color">Indicates the Color to format.</param>
- ''' <param name="Format">Indicates the resulting color value formatting.</param>
- ''' <param name="Syntax">Indicates the resulting string syntax representation.</param>
- ''' <param name="IncludeAlphaChannel">
- ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
- ''' </param>
- ''' <returns>The formatted color String.</returns>
- Public Shared Function ColorToString(ByVal [Color] As Color,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax,
- Optional ByVal IncludeAlphaChannel As Boolean = False) As String
- Select Case IncludeAlphaChannel
- Case True
- Return GetARGB([Color], Format, Syntax)
- Case Else
- Return GetRGB([Color], Format, Syntax)
- End Select
- End Function
- ''' <summary>
- ''' Formats a Brush object to return a Color String representation for the specified format and/or language syntax.
- ''' </summary>
- ''' <param name="Brush">Indicates the Brush object to format.</param>
- ''' <param name="Format">Indicates the resulting color value formatting.</param>
- ''' <param name="Syntax">Indicates the resulting string syntax representation.</param>
- ''' <param name="IncludeAlphaChannel">
- ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
- ''' </param>
- ''' <returns>The formatted color String.</returns>
- Public Shared Function BrushToString(ByVal [Brush] As SolidBrush,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax,
- Optional ByVal IncludeAlphaChannel As Boolean = False) As String
- Select Case IncludeAlphaChannel
- Case True
- Return GetARGB([Brush].Color, Format, Syntax)
- Case Else
- Return GetRGB([Brush].Color, Format, Syntax)
- End Select
- End Function
- ''' <summary>
- ''' Formats a Pen object to return a Color String representation for the specified format and/or language syntax.
- ''' </summary>
- ''' <param name="Pen">Indicates the Pen object to format.</param>
- ''' <param name="Format">Indicates the resulting color value formatting.</param>
- ''' <param name="Syntax">Indicates the resulting string syntax representation.</param>
- ''' <param name="IncludeAlphaChannel">
- ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
- ''' </param>
- ''' <returns>The formatted color String.</returns>
- Public Shared Function PenToString(ByVal [Pen] As Pen,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax,
- Optional ByVal IncludeAlphaChannel As Boolean = False) As String
- Select Case IncludeAlphaChannel
- Case True
- Return GetARGB([Pen].Color, Format, Syntax)
- Case Else
- Return GetRGB([Pen].Color, Format, Syntax)
- End Select
- End Function
- ''' <summary>
- ''' Formats a Color String of the specified format and/or language syntax to return a Color.
- ''' </summary>
- ''' <param name="ColorString">The color string.</param>
- ''' <param name="Format">Indicates the value format that the input string is written.</param>
- ''' <param name="Syntax">Indicates the string syntax that the input string is written.</param>
- ''' <returns>The Color</returns>
- Public Shared Function StringToColor(ByVal ColorString As String,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax) As Color
- Dim Converter As New ColorConverter
- Select Case Format
- Case ValueFormat.Byte
- Select Case Syntax
- Case StringSyntax.None,
- StringSyntax.VisualStudioPropertyGrid
- Return Converter.ConvertFromString(ColorString.Trim.
- Replace(",", ";"))
- Case StringSyntax.VBNET
- Return Converter.ConvertFromString(ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(")", Nothing).
- Replace(",", ";"))
- Case StringSyntax.CSharp
- Return Converter.ConvertFromString(ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(");", Nothing).
- Replace(",", ";"))
- Case Else
- Return Color.Empty
- End Select
- Case ValueFormat.Hexadecimal
- Select Case Syntax
- Case StringSyntax.None,
- StringSyntax.VisualStudioPropertyGrid
- Return ColorTranslator.FromHtml(ColorString.Trim.Replace("0x", "#"))
- Case StringSyntax.VBNET
- Return Converter.ConvertFromString("&H" & ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(")", Nothing).
- Replace(",", Nothing).
- Replace("&h", Nothing).
- Replace(" ", Nothing))
- Case StringSyntax.CSharp
- Return Converter.ConvertFromString("&H" & ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(");", Nothing).
- Replace(",", Nothing).
- Replace("0x", Nothing).
- Replace(" ", Nothing))
- Case Else
- Return Color.Empty
- End Select
- Case ValueFormat.HTML
- Select Case Syntax
- Case StringSyntax.None,
- StringSyntax.VisualStudioPropertyGrid
- Return ColorTranslator.FromHtml(ColorString.Trim)
- Case StringSyntax.VBNET
- Return ColorTranslator.FromHtml(ColorString.Trim.ToLower.
- Replace("colortranslator.fromhtml(", Nothing).
- Replace(")", Nothing))
- Case StringSyntax.CSharp
- Return ColorTranslator.FromHtml(ColorString.Trim.ToLower.
- Replace("colortranslator.fromhtml(", Nothing).
- Replace(");", Nothing))
- Case Else
- Return Color.Empty
- End Select
- End Select
- End Function
- ''' <summary>
- ''' Formats a Color String of the specified format and/or language syntax to return a Brush.
- ''' </summary>
- ''' <param name="ColorString">The color string.</param>
- ''' <param name="Format">Indicates the value format that the input string is written.</param>
- ''' <param name="Syntax">Indicates the string syntax that the input string is written.</param>
- ''' <returns>The Brush</returns>
- Public Shared Function StringToBrush(ByVal ColorString As String,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax) As SolidBrush
- Using br As New SolidBrush(StringToColor(ColorString, Format, Syntax))
- Return br
- End Using
- End Function
- ''' <summary>
- ''' Formats a Color String of the specified format and/or language syntax to return a Pen.
- ''' </summary>
- ''' <param name="ColorString">The color string.</param>
- ''' <param name="Format">Indicates the value format that the input string is written.</param>
- ''' <param name="Syntax">Indicates the string syntax that the input string is written.</param>
- ''' <returns>The Pen</returns>
- Public Shared Function StringToPen(ByVal ColorString As String,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax) As Pen
- Using p As New Pen(StringToColor(ColorString, Format, Syntax))
- Return p
- End Using
- End Function
- ''' <summary>
- ''' Formats a Color String of the specified format and/or language syntax to return a
- ''' new String of other format and/or language syntax.
- ''' </summary>
- ''' <param name="ColorString">The color string.</param>
- ''' <param name="Format">The format in which the input string is written.</param>
- ''' <param name="Syntax">The syntax of the input string (if any).</param>
- ''' <param name="NewFormat">The format in which the input string is written.</param>
- ''' <param name="NewSyntax">The syntax of the input string (if any).</param>
- ''' <param name="IncludeAlphaChannel">
- ''' If set to <c>true</c> the Alpha channel would be included in the resulting String.
- ''' </param>
- ''' <returns>The Color</returns>
- Public Shared Function StringToString(ByVal ColorString As String,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax,
- ByVal NewFormat As ValueFormat,
- ByVal NewSyntax As StringSyntax,
- Optional ByVal IncludeAlphaChannel As Boolean = False) As String
- Dim Converter As New ColorConverter
- Select Case Format
- Case ValueFormat.Byte
- Select Case Syntax
- Case StringSyntax.None,
- StringSyntax.VisualStudioPropertyGrid
- ColorString = ColorString.Trim.
- Replace(",", ";")
- Case StringSyntax.VBNET
- ColorString = ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(")", Nothing).
- Replace(",", ";")
- Case StringSyntax.CSharp
- ColorString = ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(");", Nothing).
- Replace(",", ";")
- Case Else
- ColorString = String.Empty
- End Select
- Case ValueFormat.Hexadecimal
- Select Case Syntax
- Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
- ColorString = ColorString.Trim.Replace("0x", "#")
- Case StringSyntax.VBNET
- ColorString = "&H" & ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(")", Nothing).
- Replace(",", Nothing).
- Replace("&h", Nothing).
- Replace(" ", Nothing)
- Case StringSyntax.CSharp
- ColorString = ColorString.Trim.ToLower.
- Replace("color.fromargb(", Nothing).
- Replace(");", Nothing).
- Replace(",", Nothing).
- Replace("0x", Nothing).
- Replace(" ", Nothing)
- Case Else
- ColorString = String.Empty
- End Select
- Case ValueFormat.HTML
- Select Case Syntax
- Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
- ColorString = ColorString
- Case StringSyntax.VBNET
- ColorString = ColorString.Trim.ToLower.
- Replace("colortranslator.fromhtml(", Nothing).
- Replace(")", Nothing).
- Replace("""", Nothing)
- Case StringSyntax.CSharp
- ColorString = ColorString.Trim.ToLower.
- Replace("colortranslator.fromhtml(", Nothing).
- Replace(");", Nothing).
- Replace("""", Nothing)
- Case Else
- ColorString = String.Empty
- End Select
- End Select
- Select Case IncludeAlphaChannel
- Case True
- Return GetARGB(Converter.ConvertFromString(ColorString), NewFormat, NewSyntax)
- Case Else
- Return GetRGB(Converter.ConvertFromString(ColorString), NewFormat, NewSyntax)
- End Select
- End Function
- #End Region
- #Region " Private Methods "
- ''' <summary>
- ''' Formats a Color to return an RGB String representation for the specified format and/or language syntax.
- ''' </summary>
- ''' <param name="Color">Indicates the color to format.</param>
- ''' <param name="Format">Indicates the resulting RGB value formatting.</param>
- ''' <param name="Syntax">Indicates the resulting string syntax to represent the formatted RGB value.</param>
- ''' <returns>The formatted RGB String.</returns>
- Private Shared Function GetRGB(ByVal [Color] As Color,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax) As String
- Select Case Format
- Case ValueFormat.Byte
- Dim INTstr As String =
- String.Format("{0}, {1}, {2}",
- Convert.ToString([Color].R),
- Convert.ToString([Color].G),
- Convert.ToString([Color].B))
- Select Case Syntax
- Case StringSyntax.None
- Return INTstr
- Case StringSyntax.VBNET
- Return String.Format("Color.FromArgb({0})", INTstr)
- Case StringSyntax.CSharp
- Return String.Format("Color.FromArgb({0});", INTstr)
- Case StringSyntax.VisualStudioPropertyGrid
- Return INTstr.Replace(",", ";")
- Case Else
- Return String.Empty
- End Select
- Case ValueFormat.Hexadecimal
- Dim HEXstr As String =
- String.Format("0x{0}{1}{2}",
- Convert.ToString([Color].R, 16).ToUpper,
- Convert.ToString([Color].G, 16).ToUpper,
- Convert.ToString([Color].B, 16).ToUpper)
- Select Case Syntax
- Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
- Return HEXstr
- Case StringSyntax.VBNET
- Return String.Format("Color.FromArgb(&H{0}, &H{1}, &H{2})",
- Convert.ToString([Color].R, 16).ToUpper,
- Convert.ToString([Color].G, 16).ToUpper,
- Convert.ToString([Color].B, 16).ToUpper)
- Case StringSyntax.CSharp
- Return String.Format("Color.FromArgb(0x{0}, 0x{1}, 0x{2});",
- Convert.ToString([Color].R, 16).ToUpper,
- Convert.ToString([Color].G, 16).ToUpper,
- Convert.ToString([Color].B, 16).ToUpper)
- Case Else
- Return String.Empty
- End Select
- Case ValueFormat.HTML
- Dim HTMLstr As String =
- ColorTranslator.ToHtml([Color])
- Select Case Syntax
- Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
- Return HTMLstr
- Case StringSyntax.VBNET
- Return String.Format("ColorTranslator.FromHtml(""{0}"")", HTMLstr)
- Case StringSyntax.CSharp
- Return String.Format("ColorTranslator.FromHtml(""{0}"");", HTMLstr)
- Case Else
- Return String.Empty
- End Select
- Case Else
- Return String.Empty
- End Select
- End Function
- ''' <summary>
- ''' Formats a Color to return an ARGB String representation for the specified format and/or language syntax.
- ''' </summary>
- ''' <param name="Color">Indicates the color to format.</param>
- ''' <param name="Format">Indicates the resulting ARGB value formatting.</param>
- ''' <param name="Syntax">Indicates the resulting string syntax to represent the formatted ARGB value.</param>
- ''' <returns>The formatted ARGB String.</returns>
- Private Shared Function GetARGB(ByVal [Color] As Color,
- ByVal Format As ValueFormat,
- ByVal Syntax As StringSyntax) As String
- Select Case Format
- Case ValueFormat.Byte
- Dim INTstr As String =
- String.Format("{0}, {1}, {2}, {3}",
- Convert.ToString([Color].A),
- Convert.ToString([Color].R),
- Convert.ToString([Color].G),
- Convert.ToString([Color].B))
- Select Case Syntax
- Case StringSyntax.None
- Return INTstr
- Case StringSyntax.VBNET
- Return String.Format("Color.FromArgb({0})", INTstr)
- Case StringSyntax.CSharp
- Return String.Format("Color.FromArgb({0});", INTstr)
- Case StringSyntax.VisualStudioPropertyGrid
- Return INTstr.Replace(",", ";")
- Case Else
- Return String.Empty
- End Select
- Case ValueFormat.Hexadecimal
- Dim HEXstr As String =
- String.Format("0x{0}{1}{2}{3}",
- Convert.ToString([Color].A, 16).ToUpper,
- Convert.ToString([Color].R, 16).ToUpper,
- Convert.ToString([Color].G, 16).ToUpper,
- Convert.ToString([Color].B, 16).ToUpper)
- Select Case Syntax
- Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
- Return HEXstr
- Case StringSyntax.VBNET
- Return String.Format("Color.FromArgb(&H{0}, &H{1}, &H{2}, &H{3})",
- Convert.ToString([Color].A, 16).ToUpper,
- Convert.ToString([Color].R, 16).ToUpper,
- Convert.ToString([Color].G, 16).ToUpper,
- Convert.ToString([Color].B, 16).ToUpper)
- Case StringSyntax.CSharp
- Return String.Format("Color.FromArgb(0x{0}, 0x{1}, 0x{2}, 0x{3});",
- Convert.ToString([Color].A, 16).ToUpper,
- Convert.ToString([Color].R, 16).ToUpper,
- Convert.ToString([Color].G, 16).ToUpper,
- Convert.ToString([Color].B, 16).ToUpper)
- Case Else
- Return String.Empty
- End Select
- Case ValueFormat.HTML
- Dim HTMLstr As String =
- ColorTranslator.ToHtml([Color])
- Select Case Syntax
- Case StringSyntax.None, StringSyntax.VisualStudioPropertyGrid
- Return HTMLstr
- Case StringSyntax.VBNET
- Return String.Format("ColorTranslator.FromHtml(""{0}"")", HTMLstr)
- Case StringSyntax.CSharp
- Return String.Format("ColorTranslator.FromHtml(""{0}"");", HTMLstr)
- Case Else
- Return String.Empty
- End Select
- Case Else
- Return String.Empty
- End Select
- End Function
- #End Region
- #Region " Hidden methods "
- ' These methods and properties are purposely hidden from Intellisense just to look better without unneeded methods.
- ' NOTE: The methods can be re-enabled at any-time if needed.
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub Equals()
- End Sub
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub ReferenceEquals()
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Random Generators "
- ''' <summary>
- ''' Random Color generators.
- ''' </summary>
- Public Class RandomGenerators
- #Region " Public Methods "
- ''' <summary>
- ''' Generates a random RGB color.
- ''' </summary>
- ''' <returns>Color.</returns>
- Public Shared Function RGB() As Color
- Dim Rand As New Random
- Return Color.FromArgb(255,
- Rand.Next(0, 255),
- Rand.Next(0, 255),
- Rand.Next(0, 255))
- End Function
- ''' <summary>
- ''' Generates a random RGB color between the specified minimum/maximum values.
- ''' </summary>
- ''' <param name="RMin">Indicates the RED channel minimum value.</param>
- ''' <param name="RMax">Indicates the RED channel maximum value.</param>
- ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
- ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
- ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
- ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
- ''' <returns>Color.</returns>
- Public Shared Function RGB(Optional ByVal RMin As Byte = 0,
- Optional ByVal RMax As Byte = 255,
- Optional ByVal GMin As Byte = 0,
- Optional ByVal GMax As Byte = 255,
- Optional ByVal BMin As Byte = 0,
- Optional ByVal BMax As Byte = 255) As Color
- Dim Rand As New Random
- Return Color.FromArgb(255,
- Rand.Next(RMin, RMax),
- Rand.Next(GMin, GMax),
- Rand.Next(BMin, BMax))
- End Function
- ''' <summary>
- ''' Generates a random ARGB color.
- ''' </summary>
- ''' <returns>Color.</returns>
- Public Shared Function ARGB() As Color
- Dim Rand As New Random
- Return Color.FromArgb(Rand.Next(0, 255),
- Rand.Next(0, 255),
- Rand.Next(0, 255),
- Rand.Next(0, 255))
- End Function
- ''' <summary>
- ''' Generates a random RGB color between the specified minimum/maximum values.
- ''' </summary>
- ''' <param name="AMin">Indicates the ALPHA channel minimum value.</param>
- ''' <param name="AMax">Indicates the ALPHA channel maximum value.</param>
- ''' <param name="RMin">Indicates the RED channel minimum value.</param>
- ''' <param name="RMax">Indicates the RED channel maximum value.</param>
- ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
- ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
- ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
- ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
- ''' <returns>Color.</returns>
- Public Shared Function ARGB(Optional ByVal AMin As Byte = 0,
- Optional ByVal AMax As Byte = 255,
- Optional ByVal RMin As Byte = 0,
- Optional ByVal RMax As Byte = 255,
- Optional ByVal GMin As Byte = 0,
- Optional ByVal GMax As Byte = 255,
- Optional ByVal BMin As Byte = 0,
- Optional ByVal BMax As Byte = 255) As Color
- Dim Rand As New Random
- Return Color.FromArgb(Rand.Next(AMin, AMax),
- Rand.Next(RMin, RMax),
- Rand.Next(GMin, GMax),
- Rand.Next(BMin, BMax))
- End Function
- ''' <summary>
- ''' Generates a random QB color.
- ''' </summary>
- ''' <returns>Color.</returns>
- Public Shared Function QB() As Color
- Dim Rand As New Random
- Dim c As Color = Color.FromName([Enum].Parse(GetType(ConsoleColor),
- Rand.Next(0, 15)).ToString)
- Select Case c.IsKnownColor
- ' Fix for the 'Consolecolor.DarkYellow' value which doesn't have color information.
- Case False
- Return Color.FromArgb(255, 128, 128, 0)
- Case Else
- Return c
- End Select
- End Function
- ''' <summary>
- ''' Generates a random ConsoleColor color.
- ''' </summary>
- ''' <returns>ConsoleColor.</returns>
- Public Shared Function [ConsoleColor]() As ConsoleColor
- Dim Rand As New Random
- Return [Enum].Parse(GetType(ConsoleColor), Rand.Next(0, 15))
- End Function
- ''' <summary>
- ''' Generates a random Brush.
- ''' </summary>
- ''' <returns>SolidBrush.</returns>
- Public Shared Function [Brush]() As SolidBrush
- Using br As New SolidBrush(RGB)
- Return br
- End Using
- End Function
- ''' <summary>
- ''' Generates a random Brush between the specified minimum/maximum values.
- ''' </summary>
- ''' <param name="AMin">Indicates the ALPHA channel minimum value.</param>
- ''' <param name="AMax">Indicates the ALPHA channel maximum value.</param>
- ''' <param name="RMin">Indicates the RED channel minimum value.</param>
- ''' <param name="RMax">Indicates the RED channel maximum value.</param>
- ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
- ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
- ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
- ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
- ''' <returns>SolidBrush.</returns>
- Public Shared Function [Brush](Optional ByVal AMin As Byte = 0,
- Optional ByVal AMax As Byte = 255,
- Optional ByVal RMin As Byte = 0,
- Optional ByVal RMax As Byte = 255,
- Optional ByVal GMin As Byte = 0,
- Optional ByVal GMax As Byte = 255,
- Optional ByVal BMin As Byte = 0,
- Optional ByVal BMax As Byte = 255) As SolidBrush
- Using br As New SolidBrush(ARGB(AMin, AMax, RMin, RMax, GMin, GMax, BMin, BMax))
- Return br
- End Using
- End Function
- ''' <summary>
- ''' Generates a random Pen.
- ''' </summary>
- ''' <returns>Pen.</returns>
- Public Shared Function [Pen]() As Pen
- Using p As New [Pen](RGB)
- Return p
- End Using
- End Function
- ''' <summary>
- ''' Generates a random Pen between the specified minimum/maximum values.
- ''' </summary>
- ''' <param name="AMin">Indicates the ALPHA channel minimum value.</param>
- ''' <param name="AMax">Indicates the ALPHA channel maximum value.</param>
- ''' <param name="RMin">Indicates the RED channel minimum value.</param>
- ''' <param name="RMax">Indicates the RED channel maximum value.</param>
- ''' <param name="GMin">Indicates the GREEN channel minimum value.</param>
- ''' <param name="GMax">Indicates the GREEN channel maximum value.</param>
- ''' <param name="BMin">Indicates the BLUE channel minimum value.</param>
- ''' <param name="BMax">Indicates the BLUE channel maximum value.</param>
- ''' <returns>Pen.</returns>
- Public Shared Function [Pen](Optional ByVal AMin As Byte = 0,
- Optional ByVal AMax As Byte = 255,
- Optional ByVal RMin As Byte = 0,
- Optional ByVal RMax As Byte = 255,
- Optional ByVal GMin As Byte = 0,
- Optional ByVal GMax As Byte = 255,
- Optional ByVal BMin As Byte = 0,
- Optional ByVal BMax As Byte = 255) As Pen
- Using p As New [Pen](ARGB(AMin, AMax, RMin, RMax, GMin, GMax, BMin, BMax))
- Return p
- End Using
- End Function
- #End Region
- #Region " Hidden methods "
- ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
- ' NOTE: The methods can be re-enabled at any-time if needed.
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub Equals()
- End Sub
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub ReferenceEquals()
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Hidden methods "
- ' These methods are purposely hidden from Intellisense just to look better without unneeded methods.
- ' NOTE: The methods can be re-enabled at any-time if needed.
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub Equals()
- End Sub
- <EditorBrowsable(EditorBrowsableState.Never)>
- Public Shadows Sub ReferenceEquals()
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement