Syntactic Confectionery Delight PerlMonks

### comment on

 Need Help??
Hi, thanks, I took a look at Color::Calc and some old code by Ho-Sheng Hsiao dated 1998-07-07 and came up with the following which seems to work pretty well...
```# given a colour in the form "#ff00ff" i.e. hex rrggbb, return a contr
+asting colour
sub contrast_colour {
my \$col = shift;
# Need rgb values for colour...
if ( not \$col =~ /^#(..)(..)(..)\$/ ) {
warn "failed to convert colour '\$col' to rgb components\n";
return;
}
my ( \$r, \$g, \$b ) = ( hex(\$1), hex(\$2), hex(\$3) );
#~ print "RGB= \$r, \$g, \$b\n";
# here's an idea! simple contrast solution...
my %oppcolors = (
"00" => "FF",
"33" => "FF",
"66" => "FF",
"99" => "FF",
"CC" => "00",
"FF" => "00",
);
\$r = int( \$r / 51 ) * 51;
\$g = int( \$g / 51 ) * 51;
\$b = int( \$b / 51 ) * 51;
#~ print "RGB rounded = \$r, \$g, \$b\n";
\$r = \$oppcolors{ sprintf( "%02X", \$r ) };
\$g = \$oppcolors{ sprintf( "%02X", \$g ) };
\$b = \$oppcolors{ sprintf( "%02X", \$b ) };
#~ print "RGB inverted = \$r, \$g, \$b\n";
return "#\$r\$g\$b";
}

In reply to Re: Re: Contrasting Colours by msemtd
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":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2023-09-29 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?