Re: difference between two colors
- Posted by Derek Parnell <ddparnell at bigpond.com> Jun 09, 2001
- 388 views
----- Original Message ----- From: rforno at tutopia.com Subject: Re: difference between two colors >I'd assume r, g, b are the axis of a 3-dimensional >space, and so their possible values span a cube. >Then I would compute the geometrical distance >between the 2 points in such a space, but I am >too lazy to write the function... This seems reasonable, especially when you factor in the way the eye perceives colors. Here's one attempt at this algorithm. ---------------------------- function RFORNO_difference(sequence rgb1, sequence rgb2) sequence colorfactors atom diff, brightness -- Set color band ranges -- ie. Red needs 16 values difference before the eye registers it, -- Green needs 12, and Blue needs 10 colorfactors = {16, 12, 10} -- Set brightness factor -- Tune this to your liking. brightness = 1.0 -- Calc relative luminosity difference of raw colors. lum = abs((rgb1[1] + rgb1[2] + rgb1[3])-(rgb2[1] + rgb2[2] + rgb2[3])) -- Adjust raw colors into color bands. rgb1 = floor(rgb1 / colorfactors) * colorfactors rgb2 = floor(rgb2 / colorfactors) * colorfactors -- Calc 3d-space distance. diff = power( power(rgb1[1] - rgb2[1], 2) + power(rgb1[2] - rgb2[2], 2) + power(rgb1[3] - rgb2[3], 2) , 0.5) -- Adjust dist with brightness, -- Convert to integer and return return floor(diff + (lum * brightness)) end function ---------------------------- Of course, one could also debate the color band factors, as each person sees color differently. There is an interesting discussion on this at http://www.firelily.com/opinions/color.html --- cheers, Derek Parnell Melbourne, Australia "To finish a job quickly, go slower."