Mail Archives: djgpp/1999/10/22/02:18:07
Heya
> Well, sure pardner! A nice yun feller like yerself deserves all th' help it
> can git!
Sure gramps ;) And who said I was young anyway?
> To find the existing colour that is closest to the colour that you want, you
> have to calculate the distance between the colour that you want and each
> colour that you have.
> Here's what I mean with "the distance between": look at the r, g and b
> values as the x, y and z components of a point in 3d space. Now you can
> calculate how close two colours are to each other, just like you would
> calculate how close two 3d points are to each other. How do you calculate
> the distance between two 3d points? The distance is sqrt((x1-x2)^2 +
> (y1-y2)^2 + (z1-z2)^2). (The squareroot of the sum of the squares of the
> difference between the x, y and z components). So you would look at each
> colour you have and calculate the distance between it and the one you want,
> and so find the closest colour.
No, no, no, no, no... That method was proved to be horribly inaccurate... If
you're going to use RGB for closest colours you'll probably end up with horribly
distant colours compared to a couple of other methods (which I don't have on
hand but I'll post in a couple of days if you *really* want)...
And you still want to use ratios on the components because of how the eye finds
distances between colours...
> The example you gave:
> ,--r g b
> | | | |
> 0--0,0,0
> 1--1,7,9
> 2--2,8,9
> 3--5,1,2
> 4--7,8,3
> And you're looking for 1, 6, 2.
> Distance between your colour and
> 0:
> sqrt((1-0)^2 + (6-0)^2 + (2-0)^2) = sqrt(41)
> 1:
> sqrt((1-1)^2 + (6-7)^2 + (2-9)^2) = sqrt(50)
> 2:
> sqrt((1-2)^2 + (6-8)^2 + (2-9)^2) = sqrt(54)
> 3:
> sqrt((1-5)^2 + (6-1)^2 + (2-2)^2) = sqrt(41)
> 4:
> sqrt((1-7)^2 + (6-8)^2 + (2-3)^2) = sqrt(41)
>
> Mmm. So we have three potential colours that are equally close. A rather
> unlikely event, but which one you use doesn't really matter. Your algorith
> will probably return the first of the three as the closest, which is fine.
Dither the three colours, that's all... Much, much more accurate. For speed, use
the first...
> You will also notice that the sqrt operation is not really neccassary. You
> should drop it to speed up your operations a bit.
Duh!!! I've use this method way to many times, but the method I have in the post
before this one seems to work more effeciently (though it's off my head so it
may not be the method I'm using at home :)...
Round 2?
- frEk
- Raw text -