I’m trying to emulate Photoshop’s “Color Overlay” using CSS filters, and while doing so, found out the CSS filters operate on colors as consistently as an epileptic seizure.
Consider the color #FF0000
. If we rotate its hue by 120deg
, we should get #00FF00
, and by 240deg
we should get #0000FF
. This is the realm of sanity. Now let’s enter CSS filters:
body { font: bold 99px Arial }
span { color: #F00; }
.daltonics-wont-notice {
-webkit-filter: hue-rotate(120deg);
filter: hue-rotate(120deg);
}
.precision-is-overrated {
-webkit-filter: hue-rotate(240deg);
filter: hue-rotate(240deg);
}
<span class="red">☺</span>
<span class="daltonics-wont-notice">☹</span>
<span class="precision-is-overrated">☹</span>
What should be #00FF00
is #007100
, and what should be #0000FF
is #0132FF
. By using hue-rotate
, the hue, saturation and brightness have been set to nonsensical levels, cross-browser.
I need to catch up with Cthulhu and figure out what logic He coded so I can work around it.
Is this a wierd color space unrelated to HSV or HSL? Is it possible to translate HSV, HSL or RGB coordinates into this whimsical dimension? Does it have a name? A standard? A cult following?
3
Answers
I still cannot believe this is cross-browser. I mean, I've been googling for color spaces and couldn't find any where their definition of "hue" makes sense. They pulled it completely out of their asses, as a big, spiky solid block of galvanized stupidity.
Either way, I have read the inscriptions, and after careful examination of the magic incantations, I've produced a javascript version of the horribly-broken
hue-rotate
algorithm browsers are currently suffering from.Here's a jsfiddle version and here's it as a code snippet:
Note that at some point they may find out that the algorithm was coded by an intern on April 1st that wanted to pull a prank on everyone. They may even find the dice used to choose coefficients.
One way or another, knowing the random logic employed helps me work around it, and that's why I did this. Hopefully someone will stuble upon it, and maybe some day we'll have fixed versions of
hue-rotate
and company shipped with browsers.As a bonus, in case it helps anyone: this is how Sepia is calculated:
I found that not only CSS but also other implementation are using the same algorithm.
Here is an reasonable explanation.
So the algorithm makes hue rotation while preserving luminance against just rotating hue channel.
That’s a luminance-preserving hue rotation. It’s supposed to work like that. If you hue-rotate an image before converting to monochrome, it should have the same perceived brightness. #00FF00 and #0000FF would not.