vb找到最近的已知颜色

时间:2013-11-28 18:51:30

标签: vba colors

我一直在努力找到最近的已知颜色,如

dim nearestcolor as color= bitmap.getpixel(point.x,point.y)
if nearestcolor.toknowncolor = color.red then 

end if

我需要这样的东西,我需要找到nearestcolor看起来像红色

2 个答案:

答案 0 :(得分:0)

不确定这是否是您的答案,但您可以将颜色的rgb值视为x,y,z平面中的点,并使用几何来计算已知颜色与像素颜色之间的距离。 http://freespace.virgin.net/hugo.elias/routines/r_dist.htm

答案 1 :(得分:0)

Sub HoldCol()
    Dim colC, CC&
' all thanks to Mr Chip Pearson
    colC = Array("UNNAMED", _
                 "Black", "White", "Red", "Bright Green", "Blue", "Yellow", "Pink", "Turquoise", _
                 "Dark Red", "Green", "Dark Blue", "Dark Yellow", "Violet", "Teal", "Gray 25%", "Gray 50%", _
                 "UNNAMED", "UNNAMED", "UNNAMED", "UNNAMED", "UNNAMED", "UNNAMED", "UNNAMED", "UNNAMED", _
                 "Dark Blue", "Pink", "Yellow", "Turquoise", "Violet", "Dark Red", "Teal", "Blue", _
                 "Sky Blue", "Light Turquoise", "Light Green", "Light Yellow", "Pale Blue", "Rose", "Lavender", "Tan", _
                 "Light Blue", "Aqua", "Lime", "Gold", "Light Orange", "Orange", "Blue Gray", "Gray 40%", _
                 "Dark Teal", "Sea Green", "Dark Green", "Olive Green", "Brown", "Plum", "Indigo", "Gray 80%")
    'cc  as collor to look at
    ' or use fill color to color cell

    CC = [m4].Interior.Color

    [m5].Interior.Color = CC    '[m4]  ' put color in cell   'out of need
    [m6] = [m5].Interior.ColorIndex    ' let vba find the nearest color index
    [n6] = CStr(colC([m6]))    ' get its name
    [m7].Interior.ColorIndex = [m6]
    ' may be offset 1 if you are using option base 1
    ' gets only close as you can see in cell "M7"
'use variable names   in reality

End Sub