
Nota: los colores no se ven como deberían por la compresión de imagen del software que he usado para capturar el GIF animado xD
WebColorConverter.vb
Código
#Region " Option Statements " Option Strict On Option Explicit On Option Infer Off #End Region #Region " Imports " Imports System.ComponentModel Imports System.Globalization #End Region Public Class WebColorConverter : Inherits ColorConverter <DebuggerStepThrough> Public Overrides Function GetStandardValuesSupported(context As ITypeDescriptorContext) As Boolean Return True End Function <DebuggerStepThrough> Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) As StandardValuesCollection For Each kc As KnownColor In [Enum].GetValues(GetType(KnownColor)) Dim color As Color = Color.FromKnownColor(kc) If Not color.IsSystemColor AndAlso color.IsNamedColor Then End If Next End Function <DebuggerStepThrough> Public Overrides Function ConvertTo(context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object If destinationType Is GetType(String) AndAlso TypeOf value Is Color Then Dim color As Color = DirectCast(value, Color) Dim html As String = If(color.A <> 255, $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}", ColorTranslator.ToHtml(Color.FromArgb(color.R, color.G, color.B))) Dim name As String = Nothing For Each knownColor As KnownColor In [Enum].GetValues(GetType(KnownColor)) Dim k As Color = Color.FromKnownColor(knownColor) If k.A = color.A AndAlso k.R = color.R AndAlso k.G = color.G AndAlso k.B = color.B AndAlso Not k.IsSystemColor Then name = k.Name Exit For End If Next Return If(name IsNot Nothing, $"{html} ({name})", html) End If Return MyBase.ConvertTo(context, culture, value, destinationType) End Function <DebuggerStepThrough> Public Overrides Function ConvertFrom(context As ITypeDescriptorContext, culture As CultureInfo, value As Object) As Object Dim s As String = TryCast(value, String) If s IsNot Nothing Then s = s.Trim() ' If input is like "xxx (Name)" remove the part in parentheses Dim idx As Integer = s.IndexOf("("c) If idx >= 0 Then s = s.Substring(0, idx).Trim() End If If s.StartsWith("#") Then Dim hx As String = s.Substring(1).Trim() ' Support #RRGGBB and #AARRGGBB If hx.Length = 6 Then Dim r As Integer = Convert.ToInt32(hx.Substring(0, 2), 16) Dim g As Integer = Convert.ToInt32(hx.Substring(2, 2), 16) Dim b As Integer = Convert.ToInt32(hx.Substring(4, 2), 16) Return Color.FromArgb(255, r, g, b) ElseIf hx.Length = 8 Then Dim a As Integer = Convert.ToInt32(hx.Substring(0, 2), 16) Dim r As Integer = Convert.ToInt32(hx.Substring(2, 2), 16) Dim g As Integer = Convert.ToInt32(hx.Substring(4, 2), 16) Dim b As Integer = Convert.ToInt32(hx.Substring(6, 2), 16) Return Color.FromArgb(a, r, g, b) Else ' Support short hex format (#RGB) by expanding to 6 digits If hx.Length = 3 Then Dim r As Integer = Convert.ToInt32(String.Concat(hx(0), hx(0)), 16) Dim g As Integer = Convert.ToInt32(String.Concat(hx(1), hx(1)), 16) Dim b As Integer = Convert.ToInt32(String.Concat(hx(2), hx(2)), 16) Return Color.FromArgb(255, r, g, b) End If End If Else ' Try ColorTranslator for standard names and "Transparent" Try Dim c As Color = ColorTranslator.FromHtml(s) ' ColorTranslator.FromHtml returns A=255 for known names; A=0 for "Transparent" Return c Catch ex As Exception ' Try KnownColor enum (e.g. user types "White" or "WhiteSmoke") Try Dim kc As KnownColor If [Enum].TryParse(s, True, kc) Then Return Color.FromKnownColor(kc) End If Catch ' Continue to fallback End Try End Try End If ' If everything fails, fallback to the base converter (throws or defaults) End If Return MyBase.ConvertFrom(context, culture, value) End Function End Class
WebColorEditor.vb
Código
#Region " Option Statements " Option Strict On Option Explicit On Option Infer Off #End Region #Region " Imports " Imports System.ComponentModel Imports System.Drawing.Design #End Region Public Class WebColorEditor : Inherits UITypeEditor <DebuggerStepThrough> Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle Return UITypeEditorEditStyle.None End Function <DebuggerStepThrough> Public Overrides Function GetPaintValueSupported(context As ITypeDescriptorContext) As Boolean Return True End Function <DebuggerStepThrough> Public Overrides Sub PaintValue(e As PaintValueEventArgs) Dim color As Color If TypeOf e.Value Is Color Then color = DirectCast(e.Value, Color) Else Exit Sub End If Using brush As New SolidBrush(color) e.Graphics.FillRectangle(brush, e.Bounds) End Using End Sub End Class
Modo de empleo:
Código
Imports System.ComponentModel Imports System.Drawing.Design Public Class Form1 Private ReadOnly ClassTest As New MyClassTest Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.PropertyGrid1.SelectedObject = Me.ClassTest End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Me.ClassTest.MyColor = Color.FromArgb(255, 116, 222, 4) Me.PropertyGrid1.Refresh() End Sub End Class Friend NotInheritable Class MyClassTest <TypeConverter(GetType(WebColorConverter))> <Editor(GetType(WebColorEditor), GetType(UITypeEditor))> ' El editor es opcional. Public Property MyColor As Color = Color.Transparent End Class
Ejemplo de utilidad en la vida real: un programa open-source para mostrar en tiempo real el color del pixel actual en la pantalla (entre otras cosas):






Autor




En línea




