Skip to main content
added 501 characters in body
Source Link
Mr. T
  • 13.1k
  • 5
  • 42
  • 49

I would first convert the RGB values into Lab space, then use the l,a,b values in the same 3d space distance calculation I have outlined above for the rgb values. This is known as the CIE76 algorithm (it's not the best, but probably one of the easier ones to implement... see my note below). That numeric difference value should get you close to understanding how similar two colors are to the human eye. Then based on some tests, I would pick a threshold value that you're comfortable with.

For converting RGB to Lab I found this page: http://cookbooks.adobe.com/post_Useful_color_equations__RGB_to_LAB_converter-14227.html

NOTE: This is basically the quickest (albeit, a bit dirty) way to get to color difference. I say dirty because there are more sophisticated algorithms out there that doesn't get distorted at the more saturated end of the color spectrum. If you want to see the more up-to-date algorithms, take a look at this wikipedia: http://en.wikipedia.org/wiki/Color_difference.

I would first convert the RGB values into Lab space, then use the l,a,b values in the same 3d space distance calculation I have outlined above for the rgb values. That numeric difference value should get you close to understanding how similar two colors are to the human eye. Then based on some tests, I would pick a threshold value that you're comfortable with.

For converting RGB to Lab I found this page: http://cookbooks.adobe.com/post_Useful_color_equations__RGB_to_LAB_converter-14227.html

I would first convert the RGB values into Lab space, then use the l,a,b values in the same 3d space distance calculation I have outlined above for the rgb values. This is known as the CIE76 algorithm (it's not the best, but probably one of the easier ones to implement... see my note below). That numeric difference value should get you close to understanding how similar two colors are to the human eye. Then based on some tests, I would pick a threshold value that you're comfortable with.

For converting RGB to Lab I found this page: http://cookbooks.adobe.com/post_Useful_color_equations__RGB_to_LAB_converter-14227.html

NOTE: This is basically the quickest (albeit, a bit dirty) way to get to color difference. I say dirty because there are more sophisticated algorithms out there that doesn't get distorted at the more saturated end of the color spectrum. If you want to see the more up-to-date algorithms, take a look at this wikipedia: http://en.wikipedia.org/wiki/Color_difference.

added 779 characters in body
Source Link
Mr. T
  • 13.1k
  • 5
  • 42
  • 49

EditedEdit 1: I read your question wrong. My fault.

One thing to note, this is assuming your colors are all in the RGB colorspace. CGColorGetComponents will return RGB only when your color is in the RGB colorspace. A grayscale color will return different values (and a different size array for rgb1 and rgb2).

Edit 2: Ok, I did some research from my old textbooks and the color data you want to be using is called Lab color space.

From http://en.wikipedia.org/wiki/Lab_color_space

Unlike the RGB and CMYK color models, Lab color is designed to approximate human vision.

I would first convert the RGB values into Lab space, then use the l,a,b values in the same 3d space distance calculation I have outlined above for the rgb values. That numeric difference value should get you close to understanding how similar two colors are to the human eye. Then based on some tests, I would pick a threshold value that you're comfortable with.

For converting RGB to Lab I found this page: http://cookbooks.adobe.com/post_Useful_color_equations__RGB_to_LAB_converter-14227.html

Edited: I read your question wrong. My fault.

One thing to note, this is assuming your colors are all in the RGB colorspace. CGColorGetComponents will return RGB only when your color is in the RGB colorspace. A grayscale color will return different values (and a different size array for rgb1 and rgb2).

Edit 1: I read your question wrong. My fault.

One thing to note, this is assuming your colors are all in the RGB colorspace. CGColorGetComponents will return RGB only when your color is in the RGB colorspace. A grayscale color will return different values (and a different size array for rgb1 and rgb2).

Edit 2: Ok, I did some research from my old textbooks and the color data you want to be using is called Lab color space.

From http://en.wikipedia.org/wiki/Lab_color_space

Unlike the RGB and CMYK color models, Lab color is designed to approximate human vision.

I would first convert the RGB values into Lab space, then use the l,a,b values in the same 3d space distance calculation I have outlined above for the rgb values. That numeric difference value should get you close to understanding how similar two colors are to the human eye. Then based on some tests, I would pick a threshold value that you're comfortable with.

For converting RGB to Lab I found this page: http://cookbooks.adobe.com/post_Useful_color_equations__RGB_to_LAB_converter-14227.html

added 768 characters in body
Source Link
Mr. T
  • 13.1k
  • 5
  • 42
  • 49

Edited: I read your question wrong. My fault.

I would start off with a set of defined colors that you want to use as your labels. For example:

#define COLOR_RED RGB(193.0f, 0.0f, 1.0f) 

Then I would use an rgb to rgb calculation to figure out how far the colors are in 3d space. So something like:

const float* rgb1 = CGColorGetComponents( color1.CGColor ); const float* rgb2 = CGColorGetComponents( color2.CGColor ); double diff = pow(pow(rgb2[0] - rgb1[0], 2) + pow(rgb2[1] - rgb1[1], 2) + pow(rgb2[2] - rgb1[2], 2), 0.5); 

Pick a threshold for the resultant, write this into a function, iterate over your labels (or arrays of labels and corresponding UIColors), and return the label where the diff is under a certain threshold.

One thing to note, this is assuming your colors are all in the RGB colorspace. CGColorGetComponents will return RGB only when your color is in the RGB colorspace. A grayscale color will return different values (and a different size array for rgb1 and rgb2).

Edited: I read your question wrong. My fault.

I would start off with a set of defined colors that you want to use as your labels. For example:

#define COLOR_RED RGB(193.0f, 0.0f, 1.0f) 

Then I would use an rgb to rgb calculation to figure out how far the colors are in 3d space. So something like:

const float* rgb1 = CGColorGetComponents( color1.CGColor ); const float* rgb2 = CGColorGetComponents( color2.CGColor ); double diff = pow(pow(rgb2[0] - rgb1[0], 2) + pow(rgb2[1] - rgb1[1], 2) + pow(rgb2[2] - rgb1[2], 2), 0.5); 

Pick a threshold for the resultant, write this into a function, iterate over your labels (or arrays of labels and corresponding UIColors), and return the label where the diff is under a certain threshold.

One thing to note, this is assuming your colors are all in the RGB colorspace. CGColorGetComponents will return RGB only when your color is in the RGB colorspace. A grayscale color will return different values (and a different size array for rgb1 and rgb2).

Source Link
Mr. T
  • 13.1k
  • 5
  • 42
  • 49
Loading