I had this in my notes, so I'd calculate the luma and sort by luma

You probably want to shift hue, but not saturation or value. So, convert your RGB value into HSV, add 180 degrees to H (modulo 360 +degrees of course), and convert back to RGB. function contrastingColor(color) { return (luma(color) >= 165) ? '000' : 'fff'; } function luma(color) // color can be a hx string or an array of RGB va +lues 0-255 { var rgb = (typeof color === 'string') ? hexToRGBArray(color) : col +or; return (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); +// SMPTE C, Rec. 709 weightings } http://juicystudio.com/article/luminositycontrastratioalgorithm.php http://www.splitbrain.org/blog/2008-09/18-calculating_color_contrast_w +ith_php http://stackoverflow.com/questions/301869/how-to-find-good-looking-fon +t-color-if-background-color-is-known http://search.cpan.org/~ian/Color-Scheme-1.02/lib/Color/Scheme.pm http://colorschemedesigner.com/ http://stackoverflow.com/questions/97646/how-do-i-determine-darker +-or-lighter-color-variant-of-a-given-color http://stackoverflow.com/questions/635022/calculating-contrasting- +colours-in-javascript http://stackoverflow.com/questions/141855/programmatically-lighten +-a-color http://www.mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-mod +el-conversion-algorithms-in-javascript http://www.lighthouse.org/accessibility/effective-color-contrast http://weblogtoolscollection.com/archives/2009/04/10/how-to-highlight- +search-terms-with-jquery/ https://developer.mozilla.org/en/DOM/window.getComputedStyle

In reply to Re: Ordering colors for contrast (luma) by Anonymous Monk
in thread [OT] Ordering colors by BrowserUk

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.