Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Contrasting Colours

by TomDLux (Vicar)
on May 29, 2003 at 16:04 UTC ( [id://261581]=note: print w/replies, xml ) Need Help??


in reply to Contrasting Colours

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://261581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found