如何通过16进制判断是暗色还是亮色
想实现类似QQ面板的功能,当背景是暗色时,文字自动为白色,背景亮色,文字为黑色。如何通过一颜色代码来判断呢? 颜色 --------------------编程问答-------------------- 将颜色转换成HSV模型,看V值吧,具体可不可行,看楼主的需求…… --------------------编程问答--------------------
Private Sub c2hsb(ByVal clr As Long)--------------------编程问答-------------------- Dim hsbH, hsbS, hsbB As Single
Dim MyR As Single, MyG As Single, MyB As Single
Dim Max As Single, Min As Single
Dim MyS As Single
Dim Delta As Single, MyVal As Single
Dim cc As String * 6
Dim r1, g1, b1 As Byte
On Error Resume Next
cc = Right("000000" + Hex$(clr), 6)
b1 = Val("&H" + Left(cc, 2))
g1 = Val("&H" + Mid(cc, 3, 2))
r1 = Val("&H" + Right(cc, 2))
MyR = r1 / 255: MyG = g1 / 255: MyB = b1 / 255
Max = Maximum(MyR, MyG, MyB)
Min = Minimum(MyR, MyG, MyB)
hsbB = Int(Max * 100)
If Max <> 0 Then
MyS = (Max - Min) / Max * 100
Else
MyS = 0
End If
hsbS = MyS
If hsbS = 0 Then
hsbH = 0
Else
Delta = Max - Min
Select Case Max
Case MyR
MyVal = (MyG - MyB) / Delta
Case MyG
MyVal = 2 + (MyB - MyR) / Delta
Case MyB
MyVal = 4 + (MyR - MyG) / Delta
End Select
MyVal = MyVal * 60
If MyVal < 0 Then MyVal = MyVal + 360
hsbH = MyVal
End If
' Debug.Print "hsb="; hsbH; " "; hsbS; " "; hsbB
End Sub
--------------------编程问答--------------------
楼上粗心了,没有提供Maximum,Minimum的函数,其实就是R,G,B分量中的最大值和最小值。 --------------------编程问答-------------------- 似乎是要把RGB颜色转换为HSV方式后,看一下V代表的明度来判断吧,你可搜索一下RGB转HSV的代码 --------------------编程问答-------------------- (r+g+b)/3 > 128黑,否则白! --------------------编程问答-------------------- 用R,G,B来做。 --------------------编程问答-------------------- 不知楼主对于"亮"和"暗"如何定义呢? 按照灰度值么? 灰度大于127算"亮"否则就是"暗"的么 --------------------编程问答--------------------
谢谢提醒!补上:
Private Function Minimum(ParamArray Vals())
Dim n As Integer, MinVal
On Error Resume Next
MinVal = Vals(0)
For n = 1 To UBound(Vals)
If Vals(n) < MinVal Then MinVal = Vals(n)
Next n
Minimum = MinVal
End Function
Private Function Maximum(ParamArray Vals())
Dim n As Integer, MaxVal
On Error Resume Next
MaxVal = Vals(0)
For n = 1 To UBound(Vals)
If Vals(n) > MaxVal Then MaxVal = Vals(n)
Next n
Maximum = MaxVal
End Function
补充:VB , 基础类