I have an icon that I would like to change color using CSS. This is in optimized SVG SVG, data optimized in CSS.
This was usually not possible. That is why icon fonts were invented; their main advantage over SVG is to get CSS color and text-shadow rules. Well, CSS filters are now capable of doing both things on arbitrary images, and now they work in all Blink, Webkit and Gecko browsers , and can be expected for the future IE / Spartan .
A text-shadow replacement is simple; just use drop-shadow filter .
Coloring an image in a specific color, however, turned out to be very difficult, despite all the necessary filters. My theory so far is as follows:
- Use
contrast(0) to turn the whole image solid gray while preserving the alpha channel (Mozilla wiki say it will turn black, but in all browsers it turns gray, it should be a typo). - Use
sepia(1) because we cannot work with hue / saturation in a gray image. This ensures that the entire image is composed of a reference color that we can do the math on; in particular, #AC9977 .
At this point, we should be able to turn the entire image from solid #AC9977 into any color we #AC9977 using hue-rotate , saturate and brightness .
First, what color coordinates are used by browsers? I could not find the meaning of spec to make sure that if it uses HSL (Lightness) or HSV (value) , but since HSB (Brightness) is a different name for HSV, I assume that it uses HSV. Also, using something like brightness(999) saturates colors (instead of making them white), which will happen in HSV, but not in HSL.
Based on this assumption, we will act as follows:
- Calculate the hue difference between
#AC9977 and the color we want, and use hue-rotate . - Calculate the saturation difference between them and use
saturate . - Calculate the difference in brightness between the two, and use
brightness .
Since this is not material that needs to be done manually, we will use the LESS preprocessor :
.colorize(@color) { @sepiaGrey:
This, in my opinion, should work. But it is not . Why and how to make it work?
An example of what I'm trying to achieve
Consider the icon as an image or element (background image, CSS-based form, etc.), with any color and shape defined by transparency (rather than a rectangular image that you can simply overlay). I want to make it completely composed of a certain color with CSS (presumably using filters ).

I plan to implement this as a LESS mixin that takes a color argument, but just a guide to the HSB function logic is enough.