Find nearest color in an RGB list

Michael Doub mike at doub.com
Tue Jan 10 15:31:00 EST 2012


This works like a charm!   

Thanks,
   Mike



On 2012-01-10, at 3:03 PM, Nonsanity wrote:

> Both look like good suggestions for getting the closest color a human would
> think is a "match", but both involve converting Livecode's native RGB to
> another format. I may be wrong, but I don't think those conversions are
> available built-in. I'm sure functions have been written to do this many
> times, but it's not trivial to do from scratch. If you can get one, then
> the following code will still work - It'll just work better. (Replace the
> r, g, b variable names with the appropriate letters for clarity.)
> 
> Depending on your use case, you may not need to be that human-accurate, in
> which case you can do the same calculation in RGB space. The square root
> calculation in your two solutions is a way to get the distance between any
> two points in three dimensional space. All three color formats have three
> values, so they can all be pictured as a cube of space with colors as
> points within that space. Your starting color is in there too, and you just
> have to check the distance from it to all the other color points, and
> return the shortest.
> 
> I'm typing this in a mail program, so it is untested...
> 
> function Distance3D( x1, y1, z1, x2, y2, z2 )
>    return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2) )
> end Distance3D
> 
> function GetClosestColor( r, g, b )
>    put 99999 into ClosestDist
>    put r &"," g &","& b into ClosestColor
>    repeat for each line c of gColorList
>        get Distance3D( r, g, b, item 1 of c, item 2 of c, item 3 of c )
>        if it < ClosestDist then
>            put it into ClosestDist
>            put c into ClosestColor
>        end if
>    end repeat
>    return ClosestColor
> end GetClosestColor
> 
> 
> You'd have your colors in gColorList like this:
>    32,32,128
>    255,255,255
>    0,0,0
>    128,128,256
>    (etc)
> 
> 
> ~ Chris Innanen
> ~ Nonsanity
> 
> 
> 
> On Tue, Jan 10, 2012 at 1:13 PM, Michael Doub <mike at doub.com> wrote:
> 
>> 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
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list