The
really correct way to do this is to subtract the given value from the largest cofficient, then multiply the smaller cofficients by the quotient of its difference. This way, all coefficients are decreased proportionally, so that unlike with
BrowserUK's method the hue never changes, only the luminance. (Actually, it's not 100% correct either, but very close.)
sub darken_hexrgb {
my ($hexrgb, $darken) = @_;
my @rgb = map hex, unpack "A2"x3, $hexrgb;
my $max = (sort {$a <=> $b} @rgb)[-1];
return "000000" if $max <= $darken;
my $fact = ($max - $darken) / $max;
return sprintf "%02X"x3, map { int($_ * $fact) } @rgb
}
That's what it looks like:
| darken_hexrgb("FF9944", 8) = F79441 |
| darken_hexrgb("FF9944", 16) = EF8F3F |
| darken_hexrgb("FF9944", 32) = DF853B |
| darken_hexrgb("FF9944", 48) = CF7C37 |
| darken_hexrgb("FF9944", 64) = BF7232 |
| darken_hexrgb("FF9944", 80) = AF692E |
| darken_hexrgb("FF9944", 96) = 9F5F2A |
| darken_hexrgb("FF9944", 112) = 8F5526 |
| darken_hexrgb("FF9944", 128) = 7F4C21 |
| darken_hexrgb("FF9944", 160) = 5F3919 |
| darken_hexrgb("FF9944", 176) = 4F2F15 |
| darken_hexrgb("FF9944", 192) = 3F2510 |
| darken_hexrgb("FF9944", 208) = 2F1C0C |
| darken_hexrgb("FF9944", 224) = 1F1208 |
(Layout shamelessly stolen from BrowserUK.)
Makeshifts last the longest.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.