SRL Archive - Research & Development Lounge

function SimilarCol(col1, col2, rT, gT, bT, hT, sT, lT, xT, yT, zT: Integer): Boolean


mixster

2008-05-25 19:06:21


function SimilarRGB(col1, col2, rT, gT, bT: Integer): Boolean;
var
r1, g1, b1, r2, g2, b2: Integer;
begin
ColorToRGB(col1, r1, g1, b1);
ColorToRGB(col2, r2, g2, b2);
Result := InRange(r2, r1 - rT, r1 + rT) and InRange(g2, g1 - gT, g1 + gT) and InRange(b2, b1 - bT, b1 + bT);
end;

function SimilarHSL(col1, col2, hT, sT, lT: Integer): Boolean;
var
h1, s1, l1, h2, s2, l2: Extended;
begin
ColorToHSL(col1, h1, s1, l1);
ColorToHSL(col2, h2, s2, l2);
Result := (h2 > (h1 - hT)) and (h2 < (h1 + hT)) and (s2 > (s1 - sT)) and (s2 < (s1 + sT)) and (l2 > (l1 - lT)) and (l2 < (l1 + lT));
end;

function SimilarXYZ(col1, col2, xT, yT, zT: Integer): Boolean;
var
x1, y1, z1, x2, y2, z2: Extended;
begin
ColorToXYZ(col1, x1, y1, z1);
ColorToHSL(col2, x2, y2, z2);
Writeln(FloatToStr(x1) + ',' + FloatToStr(y1) + ',' + FloatToStr(z1));
Writeln(FloatToStr(x2) + ',' + FloatToStr(y2) + ',' + FloatToStr(z2));
Result := (x2 > (x1 - xT)) and (x2 < (x1 + xT)) and (y2 > (y1 - yT)) and (y2 < (y1 + yT)) and (z2 > (z1 - zT)) and (z2 < (z1 + zT));
end;

function SimilarCol(col1, col2, rT, gT, bT, hT, sT, lT, xT, yT, zT: Integer): Boolean;
begin
Result := SimilarRGB(col1, col2, rT, gT, bT) and SimilarHSL(col1, col2, hT, sT, lT) and SimilarXYZ(col1, col2, xT, yT, zT);
end;

rT, gT, bT = Red Tolerance, Green Tolerance, Blue Tolerance. The rest follow the same naming pattern with it going HSL then XYZ. It converts them into their respective states then compares them against the tolerances. Haven't tested it thoroughly, but it's pretty straight forward so don't see a reason to as initial tests showed it atleast worked roughly like it should.
Enjoy.

R0b0t1

2008-05-25 20:02:47

Kind of useless.


SimilarColors(XYZToColor({...}), XYZToColor({...}), GoodTol);

Since your probably using it for autocoloring, GoodTol could be


GoodTol := Abs(XYZToColor({.Some selected color.}) - XYZToColor({.Other selected color.}));


Same for any other colorspace. Same effect, also.


EDIT: Some selected color and its counterpart would be the min and max of your array you found, variable … and its counterpart would be the value you are comparing against and the new value you got from a findcolor or something.

mixster

2008-05-26 07:19:13

I just added it in as I was heavily using colour comparisons amongst the three and it made it much quicker than having the fairly long code typed out multiple times. I know it could feature much more, but I just needed basic comparisons between the various values and if anyone wishes to, they can feel free to expand it to incorporate other comparative methods etc.

R0b0t1

2008-05-26 16:13:30

If you want to avoid multiple lines of code, then just make a wrapper for the above example. It'll do the same thing, tbh.