Reputation: 21
I am developing an app in which i have a periodic table in which each element in periodic table has a different color. Now i am showing a color picker to user, where user chooses a color from picker and i will mach the chosen color in periodic table and shows the element corresponding to it. i know how to choose color from picker and match two colors. Now my question is supposes a element in periodic table is of red color that is #FF0000 and if user selects a red color not exactly the same red code(#FF0000). Now in this case it is very unlikely that user chooses the exact color from the picker. He may chooses a similar red color with slightly different code. So how will i match in this case. Is there a way to match red color or any particular colr in all possible rage of RGB, so that if user selects any color within that range i can match it to any particuar color in table.
Upvotes: 1
Views: 76
Reputation: 27438
You can do something like this,
first you can convert that hexacode
to int
. then set some range say 20
for example. then compare both int
got from hexa for both color. if difference is more than range means 20 for example then there is not very close matching. if it is less then 20 than it is very close matching.
I am writing objective c code to convert number in hexa,
unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:@"#FF0000"];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];
NSLog(@"result :%d",result);
so you get integer number as result and then get integer for second color and compare both int numbers. if numbers are very close enough according to your setted range then they are matching colors otherwise not.
"hexa code will always unique for every color if it have negligible difference then also"
hope this will help :)
Upvotes: 0
Reputation: 5287
Step 1 : Extract the 3 RGB values of the color the user chose.
int colorSelected = Color.RED; //#FF0000
int r = (colorSelected >> 16) & 0xFF; //255
int g = (colorSelected >> 8) & 0xFF; //0
int b = (colorSelected >> 0) & 0xFF; //0
Step 2 : Define your threshold
int threshold = 15
Step 3 : For each color in your periodic table, repeat step 1, and match that each value is within the range
int currentCellColor = getCellColor(); //Let's say #FF0100
int currentRed = (currentCellColor >> 16) & 0xFF; //255
int currentGreen = (currentCellColor >> 8) & 0xFF; //0
int currentBlue = (currentCellColor >> 0) & 0xFF; //0
if (((currentRed >= (r - threshold)) && (currentRed <= (r + threshold))
&& ((currentGreen >= (g - threshold)) && (currentGreen <= (g + threshold))
&& ((currentBlue >= (b - threshold)) && (currentBlue <= (b + threshold))) {
//CurrentColor is within the desired threshold
} else {
//CurrentColor is not within the desired threshold
}
Technically the color spectrum is not supposed to be linear, but you should get a good approximation this way. For further research, you can browse https://en.wikipedia.org/wiki/Color_difference
Upvotes: 1