Color-picker showing color names

2019-06-09 08:41发布

Does anyone know of a color-picker for Visual Studio (Visual Basic) that shows the names of the standard colors?

For example, in Visual Studio, you can alter the color of a control using a color-picker that has tabs of "Custom", "Web" and "System". The Web & System options show a list of the color names, whereas Custom supplies (mainly) RGB (which is what the VB ColorPicker control does).

Thanks!

2条回答
Juvenile、少年°
2楼-- · 2019-06-09 09:19

I don't know about an existing control but you can use the KnownColor enumeration and the SystemColors class to get all the names of those Color values. You can then build your own control, e.g. custom ComboBox, with that data.

查看更多
小情绪 Triste *
3楼-- · 2019-06-09 09:25

there is precious little to one of these until you want to do like VS and present System Colors apart from Named Colors, make it a popup or some such. Example using colors as the BackGround:

' capture the names
Private _Colors As String()

' get the names 
' add qualifiers to skip SystemCOlors or
' Transparent as needed
Function GetColorNames As String()
    For Each colorName As String In KnownColor.GetNames(GetType(KnownColor))
       _Colors.Add(colorName)
    End If
Next

' post the names to a CBO:
cboBackColor.Items.AddRange(_Colors)

On the form CBO, set the DrawMode to OwnerDrawFixed, then:

Private Sub cboSheetBackColor_DrawItem(ByVal sender As Object, 
           ByVal e As System.Windows.Forms.DrawItemEventArgs) 
           Handles cboSheetBackColor.DrawItem
    Dim Bclr As Color, Fclr As Color

    ' get the colors to use for this item for this
    Bclr = Color.FromName(_Colors(e.Index).ToString)
    Fclr = GetContrastingColor(Bclr)     ' see below

    With e.Graphics
        Using br As New SolidBrush(Bclr)
            .FillRectangle(br, e.Bounds)
        End Using
        Using br As New SolidBrush(Fclr)
            .DrawString(cboSheetBackColor.Items(e.Index).ToString,
             cboSheetBackColor.Font, br, 
             e.Bounds.X, e.Bounds.Y)
        End Using

    End With
    e.DrawFocusRectangle()

End Sub

You can just draw a swatch like Windows/VS does by defining a rectangle to fill. Generally, thats swell, but in the case where you are doing something like defining a background color it rather helps to show how it looks with text on it and more of the color than the little bitty swatch - hence the filled CBO Item rect.

The standard window Text color will not show up on all of them. For a "light" theme, Violet and Black etc will hide/make the color name impossible to read. GetContrastingColor is a function which evaluates the Brightness of the current color and then returns either White or Black:

Public Function GetContrastingColor(ByVal clrBase As Color) As Color
    ' Y is the "brightness"
    Dim Y As Double = (0.299 * clrBase.R) _
            + (0.587 * clrBase.G) _
            + (0.114 * clrBase.B)

    If (Y < 140) Then
        Return Color.White
    Else
        Return Color.Black
    End If
End Function

You can then use all this in a Class which inherits from ComboBox, or build a UserControlif you like distinct controls. You can also leave it as code in a DLL which is called on those occasions. I should mention there are also perhaps a dozen such critters on CodeProject.

查看更多
登录 后发表回答