显示颜色名称的选色器 [英] Color-picker showing color names

查看:227
本文介绍了显示颜色名称的选色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道用于Visual Studio(Visual Basic)的颜色选择器显示标准颜色的名称?



例如,在Visual Studio中,您可以使用具有自定义选项卡的颜色选择器来更改控件的颜色WebSystem Web & 选项显示颜色名称的列表,而自定义供应(主要)RGB(这是VB ColorPicker

解决方案

有珍贵的到其中一个,直到你想做像VS,并提出系统颜色除了命名的颜色,使它成为一个弹出窗口等。使用颜色作为BackGround的示例:

 捕获名称
Private _Colors As String()

'获取名称
'添加限定符跳过SystemCOlors或
'根据需要透明
函数GetColorNames As String()
对于每个colorName作为字符串在KnownColor.GetNames如果
下一个

'将名称发布到CBO:
cboBackColor.Items。 AddRange(_Colors)



在表单CBO上,将DrawMode设置为 OwnerDrawFixed ,然后:

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

'获取此项目的颜色
Bclr = Color.FromName(_Colors(e.Index).ToString)
Fclr = GetContrastingColor(Bclr)'见下面

使用e.Graphics
使用br As New SolidBrush Bclr)
.FillRectangle(br,e.Bounds)
结束使用
使用br As New SolidBrush(Fclr)
.DrawString(cboSheetBackColor.Items(e.Index).ToString ,
cboSheetBackColor.Font,br,
e.Bounds.X,e.Bounds.Y)
结束使用

结束于
e.DrawFocusRectangle ()

End Sub

可以只是通过定义一个矩形来绘制像Windows / VS的样片。一般来说,它膨胀,但是在你正在做的事情,如定义背景颜色,它有助于显示它看起来与文本和更多的颜色比小点样本 - 因此填充的CBO项目rect。 / p>

标准窗口文本颜色不会显示在所有窗口上。对于光主题,紫色和黑色等将隐藏/使颜色名称不可能阅读。 GetContrastingColor 是一个函数,它计算当前颜色的亮度,然后返回白色或黑色:

 公共函数GetContrastingColor(ByVal clrBase As Color)As Color 
'Y是亮度
Dim Y As Double =(0.299 * clrBase.R)_
+(0.587 * clrBase.G)_
+(0.114 * clrBase.B)

如果(Y <140)则
返回Color.White
Else
返回Color.Black
结束如果
结束函数

然后可以在继承自ComboBox的类中使用所有这些,或者构建一个UserControlif你喜欢不同的控件。你也可以把它作为一个DLL中的代码,在这些场合调用。我应该提一下也有可能有十几个这样的小程序CodeProject。


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!

解决方案

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.

这篇关于显示颜色名称的选色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆