Implementation of Color Matching (source code)

Source: Internet
Author: User

This article implements three color matching methods,
1. First, convert the color of the color structure to the HSB color, and then compare the color matching. This is a relatively accurate method so far, and the color approximation is very high.
2. Compare the absolute difference between the RGB components of two color structures, set a threshold value, and select the closest one. This method is of low precision and has obvious distortion in some colors, but fast
3. Compare the sum of the squares of the differences between the RGB components of two color structures to obtain the tolerance. It is best to select the least tolerance. This method is of higher precision than (2) and higher speed than (1) fast
The first method used by the author is very good for 256 color matching.Code(VB. NET) as follows:

# Region "first convert to HSB color before matching, the most accurate color matching method"

'''


''' HSB color class, equivalent to a structure
'''

'''
class HSB
public hue as double
Public saturation as double
Public brightness as double
sub new (byval h as double, byval s as double, byval B as double)
hue = H
saturation = S
brightness = B
end sub
end class
'''
'''rgb to HSB
'''
'''
'' '
'''
'''
'''
Private function rgbtohsb (byval red as integer, byval green as integer, byval blue as integer) as HSB
Dim R as double = red/ 255.0
Dim G as double = green/255.0
Dim B as double = blue/255.0.
Dim Max as double = math. Max (R, math. Max (G, B ))
Dim min as double = math. Min (R, math. Min (G, B ))
Dim h as double = 0.0
If (max = R and G> = B) then
H = 60 * (G-B)/(max-min)
Elseif (max = R and G <B) then
H = 60 * (G-B)/(max-min) + 360
Elseif (max = g) then
H = 60 * (B-r)/(max-min) + 120
Elseif (max = B) then
H = 60 * (r-g)/(max-min) + 240
End if
Dim s as double
If max = 0 then
S = 0.0
Else
S = 1.0-(min/max)
End if
Return new HSB (H, S, max)
End Function
 
''' <Summary>
''' Gets the closest index color
''' </Summary>
''' <Param name = "color"> color </param>
''' <Param name = "labelcolorshsb"> color list </param>
''' <Returns> returns the index value of the color closest to the compared color in the color list. </returns>
''' <Remarks> </remarks>
Private function getnearestcolorlabelindex (byval color as color, byval labelcolorshsb as List (of HSB) as integer
Const weighthue as double = 0.8
Const weightsaturation as double = 0.1
Const weightvalue as double = 0.1
Dim mindistance as double = double. maxvalue
Dim minindex as integer = 255
Dim targethsb as HSB = rgbtohsb (color. R, color. G, color. B)
For I as integer = 0 to labelcolorshsb. Count-1
Dim DH as double = labelcolorshsb (I). Hue-targethsb. Hue
Dim ds as double = labelcolorshsb (I). saturation-targethsb. saturation
Dim DV as double = labelcolorshsb (I). brightness-targethsb. brightness
Dim curdistance as double = math. SQRT (weighthue * Math. pow (DH, 2) + weightsaturation * Math. pow (DS, 2) + weightvalue * Math. pow (DV, 2 ))
If (curdistance <mindistance) then
Mindistance = curdistance
Minindex = I + 1
End if
Next
Return minindex
End Function

# End Region

# Region "absolute value tolerance, set the threshold to get the matching color"
''' <Summary>
''' To obtain a similar color.
''' </Summary>
''' <Param name = "pcolor"> matched color </param>
''' <Returns> </returns>
''' <Remarks> </remarks>

Public Function getclosecolor (byval pcolor as color, byavl colorlist as List (of color) as integer
Dim pcolordis as integer = const_colordistance
Dim DIF as integer
Dim pacicolor as integer = const_defacolorcolorindex
For I as integer = 0 to colorlist. Count-1
''Absolute value Tolerance Method
DIF = math. ABS (colorlist (I ). r-pcolor. r) + math. ABS (colorlist (I ). g-pcolor. g) + math. ABS (colorlist (I ). b-pcolor. b)
If DIF <pcolordis then
Pcolordis = dif
Pacicolor = I + 1
End if
Next
Return pacicolor
End Function
# End Region

# Region "select the minimum value of the square tolerance to obtain the matching color"

''' <Summary>
'''Square tolerance: select the minimum value to obtain the matching color.
''' </Summary>
''' <Param name = "pcolor"> matched color </param>
''' <Returns> </returns>
''' <Remarks> </remarks>
Private function getclosecolor (byval PDC as color, byavl colorlist as List (of color) as integer
Dim pacicolor as integer = const_defacolorcolorindex
Dim pdifs as new list (of Long)
Dim phash as new hashtable
For I as integer = 0 to colorlist. Count-1
Dim tcompvalue as long
Tcompvalue = colorvaluecomp (RGB (PDC. r, PDC. g, PDC. b), RGB (colorlist (I ). r, colorlist (I ). g, colorlist (I ). B ))
Pdifs. Add (tcompvalue)
If not phash. Contains (tcompvalue) Then phash. Add (tcompvalue, I + 1)
Next
Pdifs. Sort ()
Pacicolor = phash (pdifs (0 ))
Return pacicolor
End Function

''' <Summary>
''' Determines whether the two colors are similar within the tolerance range.
''' </Summary>
''' <Param name = "pcolora"> color a </param>
''' <Param name = "pcolorb"> color B </param>
''' <Param name = "pvalve"> tolerances </param>
''' <Returns> </returns>
''' <Remarks> </remarks>

Private function colorvalueisborder (byval pcolora as long, byval pcolorb as long, byval pvalve as long) as Boolean
'Compare the two colors within the range of the pvalve tolerance.
Dim toutvalue as Boolean
Dim tcompvalue as long
Tcompvalue = colorvaluecomp (pcolora, pcolorb)
If tcompvalue <= pvalve then
Toutvalue = true
Else
Toutvalue = false
End if
Return toutvalue
End Function
''' <Summary>
''' Get color by value
''' </Summary>
''' <Param name = "pcolorvalue"> value </param>
''' <Returns> </returns>
''' <Remarks> </remarks>

Private function colorgetbyvalue (byval pcolorvalue as long) as color
'Obtain A colorrgb color from a long color data.
Dim toutcolor as color = color. fromargb (255, pcolorvalue mod 2 ^ 8, (pcolorvalue \ 2 ^ 8) mod 2 ^ 8, (pcolorvalue \ 2 ^ 16) mod 2 ^ 8)
Return toutcolor
End Function
''' <Summary>
''' Get the tolerance of two long colors
''' </Summary>
''' <Param name = "pcolora"> color a </param>
''' <Param name = "pcolorb"> color B </param>
''' <Returns> </returns>
''' <Remarks> </remarks>

Public Function colorvaluecomp (byval pcolora as long, byval pcolorb as long) as long
'Get the color difference of two long colors.
Dim toutvalue as long
Dim tcolora as color
Dim tcolorb as color
Tcolora = colorgetbyvalue (pcolora)
Tcolorb = colorgetbyvalue (pcolorb)
Toutvalue = colorrgbcomp (tcolora, tcolorb)
Return toutvalue
End Function
''' <Summary>
''' Get the tolerance between the two colors
''' </Summary>
''' <Param name = "pcolora"> color a </param>
''' <Param name = "pcolorb"> color B </param>
''' <Returns> </returns>
''' <Remarks> Square tolerant method </remarks>

Public Function colorrgbcomp (byref pcolora as color, byref pcolorb as color) as long
'Obtain the color difference between two colorrgb values.
Dim toutvalue as long
Dim tabsr as long
Dim tabsg as long
Dim tabsb as long
Tabsr = math. Abs (clng (pcolora. R)-clng (pcolorb. R ))
Tabsg = math. Abs (clng (pcolora. g)-clng (pcolorb. g ))
Tabsb = math. Abs (clng (pcolora. B)-clng (pcolorb. B ))
Toutvalue = math. SQRT (tabsr ^ 2 + tabsg ^ 2 + tabsb ^ 2)
Return toutvalue
End Function
# End Region

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.