How to compare colors

Posted on 2006-04-26
Last Modified: 2010-08-05
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?
Question by:hutelihut
    LVL 17

    Expert Comment

    Better measure of difference is sum of squares.

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

    LVL 33

    Accepted Solution

    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

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now