hushhush has asked for the wisdom of the Perl Monks concerning the following question:
This has been driving me crazy, can anyone offer any advice, maybe a snippet, on how to convert a hex color code to it's nearest color safe counterpart?
I'm parsing css sheets and need to convert them all, the part I'm stuck on is given not color safe hex code, converting to nearest color safe.
This turned out to be surprisingly hard for me to do :/. I guess this means my math with perl sucks :(
thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting hex colors to color safe hex
by kyle (Abbot) on Apr 11, 2008 at 16:23 UTC | |
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". | [reply] [d/l] [select] |
by dhosek (Beadle) on Apr 11, 2008 at 17:38 UTC | |
But are websafe colors even an issue any more? Who's using 256-color displays these days? This is one of those holdovers from the early days of the web which don't make sense. On the other hand, if you're going to a GIF, then you may want to think about using a proper clustering algorithm to get to the optimal color set. I'd suggest looking at Algorithm::Cluster for that task.
Donald Hosek, Tech Lead at oversee.net L.A. perl people, we're hiring. | [reply] |
by hushhush (Novice) on Apr 11, 2008 at 16:49 UTC | |
thanks for the replies yes, I meant web safe colors. I'd give some example code except I keep deleting it because it's horribly wrong. Getting stuck.
Basically I"m trying to do given hex color input as so: EEEEEE | [reply] |
|
Re: Converting hex colors to color safe hex
by radiantmatrix (Parson) on Apr 11, 2008 at 16:22 UTC | |
Do you perhaps mean "web safe" instead of "color safe"? If so, it's a pretty easy approach. Each element of the color is 1 byte (0-255 unsigned), and expressed in hex. All you have to do is round the values of each byte to the nearest in the "web safe" color chart. (with some bounds-checking, of course). Show us some code on what you've tried, perhaps trying to implement that kind of solution, and you'll get better help. Check out How (Not) To Ask A Question for tips on getting the best help here.
<–radiant.matrix–>
Ramblings and references The Code that can be seen is not the true Code I haven't found a problem yet that can't be solved by a well-placed trebuchet | [reply] |
|
Re: Converting hex colors to color safe hex
by dwm042 (Priest) on Apr 11, 2008 at 18:24 UTC | |
Some results are:
| [reply] [d/l] [select] |