How to compare colors

How do I make a function that compares two colors and return TRUE if the two colors are the same?

There has to be some kind of "tolerance-variabel", for example:

When Tolerance is 0 the function returns true if the two colors are exactly the same
When Tolerance is 10 the function returns true if the two colors are mostly the same
When Tolerance is 50 the function returns true if the two colors are slightly the same
and so on...

The function could be something like:

function SameColor(Color1,Color2:tColor; Tolerance:Word):boolean;

...but this does not work very well.

How do I make this function in a better way?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Better measure of difference is sum of squares.

Another alternative is recalculate RGB to more meaningfull coefficients like YUV and compare them

hello  hutelihut, , I beleive that to compare colors, it would be best to compare EACH color componenet (red, green, blue), instead of adding them together and compare the total, you might try this code, it seems to work for me - ->

function SameColor(Color1, Color2: TColorRef; Tolerance:Word): Boolean;
// I changed the the TColor to TColorRef because TColor uses a different method for color definition
Result := (Color1 = Color2) or
          ((abs(GetRValue(Color1)-GetRValue(Color2))<=Tolerance) and
          (abs(GetGValue(Color1)-GetGValue(Color2))<=Tolerance) and

procedure TForm1.sbut_ColorCompClick(Sender: TObject);
if SameColor($888888, $858192, $A) then
  Showmessage('Yes Close color');
  // if you have TColor as a variable then use the ColorToRGB( ) function to get the TColorRef for this function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.