?
Solved

How to compare colors

Posted on 2006-04-26
2
Medium Priority
?
723 Views
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;
begin
  SameColor:=abs(GetRValue(Color1)-GetRValue(Color2))+
             abs(GetGValue(Color1)-GetGValue(Color2))+
             abs(GetBValue(Color1)-GetBValue(Color2))<=Tolerance;
end;

...but this does not work very well.

How do I make this function in a better way?
0
Comment
Question by:hutelihut
2 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 16547557
Better measure of difference is sum of squares.

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

0
 
LVL 35

Accepted Solution

by:
Slick812 earned 540 total points
ID: 16547747
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
begin
Result := (Color1 = Color2) or
          ((abs(GetRValue(Color1)-GetRValue(Color2))<=Tolerance) and
          (abs(GetGValue(Color1)-GetGValue(Color2))<=Tolerance) and
          (abs(GetBValue(Color1)-GetBValue(Color2))<=Tolerance));
end;

procedure TForm1.sbut_ColorCompClick(Sender: TObject);
begin
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
end;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

599 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