in reply to Converting hex colors to color safe hex
I take it you have hex colors such as "ab1289" where "ab" is the red value, "12" is the blue value, and "89" is the green value. You want to match these up to the closest analog on some approved list (safe colors).
I'd probably start by writing something that would take such a color and break it into three decimal values using hex. I could store them in a hash mapping the original hex to an array reference with the numbers.
Then, given an unsafe color, compare it using the absolute value (abs) of the difference of the respective values (i.e., abs( $unsafe_red - $safe_red )). You'll get three differences (red, green, blue).
You might want the color that has the smallest combined difference (the minimum of $red_diff+$blue_diff+$green_diff), or you might want to pick a color where the individual differences are small (the minimum of max( $red_diff, $blue_diff, $green_diff )). You can find max() written for you in List::Util. I'm not sure which of those will give better results, but either one should get you "close".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting hex colors to color safe hex
by dhosek (Beadle) on Apr 11, 2008 at 17:38 UTC | |
|
Re^2: Converting hex colors to color safe hex
by hushhush (Novice) on Apr 11, 2008 at 16:49 UTC |