Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Contrasting Colours

by Anonymous Monk
on May 29, 2003 at 20:53 UTC ( [id://261689]=note: print w/replies, xml ) Need Help??


in reply to Contrasting Colours

OK, I've tidied up my first draft and added a little Tk testing toy. The contrasting colours returned tend toward the lighter but the hash is easily tweaked.
#! perl use strict; use warnings; use Tk; my $title = "colour contrast"; my $mw = new MainWindow( -title => $title ); my $fr = $mw->Frame()->pack; for ( 1 .. 5 ) { my $b = $fr->Button( -text => "Colour $_", )->pack; $b->configure( -command => [ \&pick_n_mix, $b ] ); } MainLoop(); sub pick_n_mix { my ($btn) = @_; my $old = $btn->cget( -bg ); my $col = $mw->chooseColor( -title => "Choose a nice colour...", -initialcolor => $old ); return unless $col; $btn->configure( -bg => $col, -activebackground => $col ); # Choose some contrasting colours... my $fg = contrast_colour($col); $btn->configure( -bg => $col, -activebackground => $col, -fg => $fg, -activeforeground => $fg ); } # given a colour in the form "#ff00ff" i.e. hex rrggbb, return a # contrasting colour sub contrast_colour { my $col = shift; # Need rgb values for colour... if ( not $col =~ /^#(\w\w)(\w\w)(\w\w)$/ ) { warn "failed to convert colour '$col' to rgb components\n"; return; } my ( $r, $g, $b ) = map hex, ( $1, $2, $3 ); # Use a hash of contrasting values for the "websafe pallette" # with emphasis on lighter colours... my %opp = qw(00 FF 33 FF 66 FF 99 FF CC 00 FF 00 ); # Round each value down to a multiple of hex 33 (i.e. 51)... map { $_ = int( $_ / 51 ) * 51 } ( $r, $g, $b ); # Replace each value with a contrasting value from the hash... map { $_ = $opp{ sprintf "%02X", $_ } } ( $r, $g, $b ); return "#$r$g$b"; }

Replies are listed 'Best First'.
Re: Re: Contrasting Colours
by msemtd (Scribe) on May 29, 2003 at 21:03 UTC
    BTW: that was me posting without logging in - oops!

    I seem to have added an extra configure in there - the pick_n_mix callback now reads...

    sub pick_n_mix { my ($btn) = @_; my $old = $btn->cget( -bg ); my $col = $mw->chooseColor( -title => "Choose a nice colour...", -initialcolor => $old ); return unless $col; # Choose a contrasting colour for text... my $fg = contrast_colour($col); $btn->configure( -bg => $col, -activebackground => $col, -fg => $fg, -activeforeground => $fg ); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found