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".


In reply to Re: Converting hex colors to color safe hex by kyle
in thread Converting hex colors to color safe hex by hushhush

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.