Option 1 - maximum contrast for black and white, fading to no contrast for middle gray: For each component, R, G, & B, subtract the current value from 255. reassamble into a colour - there's your complementary colour. If you haven't defined the format for a tuple, yet, I suggest using anonymous arrays. In any case, you'll need routines to do format conversions.

$c1 = [ $r, $g, $b ]; $c2 = contrasting_colour( @c1 ); sub contrasting_colour { my ( $c ) = @_; my ( $r, $g, $b ) = ( 255 - $c->[0], 255 - $c->[1], 255 - $c->[2] ); return [ $r, $g, $b ]; } sub rgb2hash { my ( $c ) = @_; my ( $r, $g, $b ) = ( 255 - $c->[0], 255 - $c->[1], 255 - $c->[2] ); return sprintf "#%02X%02X%02X", $r, $g, $b; }

Option 2 - less, but reasonable contrast for all values. Add half the range, 128, to each of R, G, B, rolling over values that go out of range. Black and white both go to middle grey, middle grey goes to white

sub contrasting_colour { my ( $c ) = @_; my ( $r, $g, $b ) = ( ($c->[0] + 128) % 256, ($c->[1] + 128) % 256, ($c->[2] + 128) % 256, ); return [ $r, $g, $b ]; }

Option 3 - Modify each of R, G, B by a varying amount, between 1/3 and 2/3 full range. As a consequence, looking for ten contrasting colours for black would generate ten different colours between dark grey and light grey, providing some degree of actual colour as well as monochrome. This provides fair contrast, but no repeatability.


In reply to Re: Contrasting Colours by TomDLux
in thread Contrasting Colours by msemtd

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.