
March 18th, 2002, 06:18 PM
#1
Color Matching Algorithm Needed
Does anyone have (or know of) a good algorithm for finding the closest color available in a palette? I am using a 256color palette, with 8 bits each for the r,g, and b components. Allegro uses a weighted squares method, which works OK, but it does not seem to be as accurate as the CPalette::GetNearestColorIndex() function in VC++. I am writing an app for DOS, so I can't use the VC++ function. Any help would really be appreciated.
Jesus is Lord!

March 18th, 2002, 06:36 PM
#2
Re: Color Matching Algorithm Needed
Hello, These two functions together work really well. We have a math wiz on our team who gave me this to use once, and it is great.
double CYourClass::ComputeColorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
{
/*double rd = 77*(r1r2)/255;
double gd = 150*(g1g2)/255;
double bd = 29*(b1b2)/255;
double d = sqrt(rd*rd+gd*gd+bd*bd);
return d;*/
double x1 = .412453*r1/255+.357580*g1/255+.180423*b1/255;
double y1 = .212671*r1/255+.715160*g1/255+.072169*b1/255;
double z1 = .019334*r1/255+.119193*g1/255+.950227*b1/255;
double x2 = .412453*r2/255+.357580*g2/255+.180423*b2/255;
double y2 = .212671*r2/255+.715160*g2/255+.072169*b2/255;
double z2 = .019334*r2/255+.119193*g2/255+.950227*b2/255;
double cl1 = 116*pow(y1,1.0/3)16;
if (y1 <= .008856) cl1 = 903.3*y1;
double ca1 = 500*(Fcn(x1)Fcn(y1));
double cb1 = 200*(Fcn(y1)Fcn(z1));
double cl2 = 116*pow(y2,1.0/3)16;
if (y2 <= .008856) cl2 = 903.3*y2;
double ca2 = 500*(Fcn(x2)Fcn(y2));
double cb2 = 200*(Fcn(y2)Fcn(z2));
double ld = (cl1cl2);
double ad = (ca1ca2);
double bd = (cb1cb2);
double dist = ld*ld+ad*ad+bd*bd;
dist = dist / 10100; //4 .05
return dist;
}
double CYourClass::Fcn(double x)
{
if (x > .008856)
return pow(x,1.0/3);
else
return 7.787*x+16.0/116;
}
Good luck!
Wade

March 19th, 2002, 03:36 PM
#3
Re: Color Matching Algorithm Needed
Thanks, that is a lot better than the standard weighted squares method (which I noticed you had commented out in your code). This is good enough for me to use, but STILL not quite as good as the CPalette::GetNearestPaletteIndex() function.
Thanks for your help.
Jesus is Lord!

May 13th, 2012, 03:35 AM
#4
Re: Color Matching Algorithm Needed
Originally Posted by Wade
Hello, These two functions together work really well. We have a math wiz on our team who gave me this to use once, and it is great.
double CYourClass::ComputeColorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
{
/*double rd = 77*(r1r2)/255;
double gd = 150*(g1g2)/255;
double bd = 29*(b1b2)/255;
double d = sqrt(rd*rd+gd*gd+bd*bd);
return d;*/
double x1 = .412453*r1/255+.357580*g1/255+.180423*b1/255;
double y1 = .212671*r1/255+.715160*g1/255+.072169*b1/255;
double z1 = .019334*r1/255+.119193*g1/255+.950227*b1/255;
double x2 = .412453*r2/255+.357580*g2/255+.180423*b2/255;
double y2 = .212671*r2/255+.715160*g2/255+.072169*b2/255;
double z2 = .019334*r2/255+.119193*g2/255+.950227*b2/255;
double cl1 = 116*pow(y1,1.0/3)16;
if (y1 <= .008856) cl1 = 903.3*y1;
double ca1 = 500*(Fcn(x1)Fcn(y1));
double cb1 = 200*(Fcn(y1)Fcn(z1));
double cl2 = 116*pow(y2,1.0/3)16;
if (y2 <= .008856) cl2 = 903.3*y2;
double ca2 = 500*(Fcn(x2)Fcn(y2));
double cb2 = 200*(Fcn(y2)Fcn(z2));
double ld = (cl1cl2);
double ad = (ca1ca2);
double bd = (cb1cb2);
double dist = ld*ld+ad*ad+bd*bd;
dist = dist / 10100; //4 .05
return dist;
}
double CYourClass::Fcn(double x)
{
if (x > .008856)
return pow(x,1.0/3);
else
return 7.787*x+16.0/116;
}
Good luck!
Wade
Hello Wade,
you computed color difference in this function .
Well this is actually color matching or similarity . but my question is that what the difference mean actually regarding matching .
I want to show color similarity image .do't want to compute the color differences.
look : http://i1096.photobucket.com/albums/...similarity.jpg
This is color difference formula that's what you did in your function and this is actually color matching.
Just tell what this difference mean ?
Also how can i show color similarity image ?

May 13th, 2012, 07:19 AM
#5
Re: Color Matching Algorithm Needed
This thread is 10 years old.

May 14th, 2012, 08:02 PM
#6
Re: Color Matching Algorithm Needed
Originally Posted by mili123
Hello Wade,
How do you know that Wade is even still alive after 10 years?
Regards,
Paul McKenzie
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
