Find nearest color in an RGB list
Michael Doub
mike at doub.com
Tue Jan 10 13:13:36 EST 2012
I have a list of RGB colors that represent different color samples. I need to create a function that will return the closest perceived RGB entry in the list when supplied a single RGB value. Is anyone aware of a library or some sample code that would help with this task?
From searching I found the follow two suggestions for solving this problem. Neither are not intuitively obvious to the casual observer...namely me. ;-)
1) "Convert RGB to CIE Lab color space, then compute the distance in that space ( deltaE = sqrt(deltaL^2 + deltaA^2 + deltaB^2). Colors with the lowest deltaE are the most perceptually similar to each other."
2) "Convert to RGB to HSL and consider an HSL color value a vector and define a weighted modulus function for the vector like this:
modulus = sqrt(a*H1*H1 + b*S1*S1 + c*L1*L1); where a,b,c are weights you should decide based on your visual definition of what creates a bigger difference in perceived color - a 1% change in Hue or a 1% change in Saturation. I would suggest you use a = b = 0.5 and c = 1 Finally, find out the range your modulus would take and define similar colors to be those which have their moduli very close to each other (say 5%)"
Regards,
Mike
More information about the use-livecode
mailing list