Table of Contents
We've had color functions in preprocessors like Sass for a while now. This ability to adjust and modify colors should be coming soon to CSS with the CSS Color Module Level 4. We'll be able to apply a number of color adjusters to base colors. This becomes very handy when coupled with CSS variables, because it'll be possible to have our base colors defined as variables, and then apply adjusters where needed.
Note that <^>color-mod<^> was previously known as just the <^>color<^> function. The name was recently changed in the spec to color-mod.
Here's an example of how the color-mod function is used:
.box {
// Let's make it a little redder
color: color-mod(rgb(147,123,25) red(218));
}
Or with an HEX value as the base color:
.box {
color: color-mod(#937b19 contrast(25%);
}
Or even with computed properties (CSS variables):
:root {
--base-color: #937b19;
}
.box {
color: color-mod(var(--base-color) tint(59%));
}
The resulting color from the above snippets will be <^>rgb(218, 123, 25)<^>.
You can use multiple color adjuster in the same color function:
.box {
color: color-mod(purple lightness(62%) red(218) blue(202) whiteness(25%));
}
Color Adjusters
Here's a list of available color adjusters:
- <^>alpha<^>: A value for the alpha-transparency between 0% and 100%.
- <^>red<^>, <^>green<^> & <^>blue<^>: A value between 0 and 255. Given a starting color of <^>rgb(140, 254, 255)<^>, the starting red value would be 140, so anything higher than 140 increases the amount of red in the color and anything lower than 140 decreases the amount of red in color. <^>Green<^> and <^>blue<^> work the same way, with affecting their respective color.
- <^>blackness<^> & <^>whiteness<^>: A value between 0% and 100%.
- <^>contrast<^>: A value between 0% and 100%.
- <^>saturation<^>: A value between 0% and 100%. 0% is gray.
- <^>lightness<^>: A value between 0% and 100%. 0% is black, and 100% is white.
- <^>tint<^>: A value between 0% and 100%.
- <^>hue<^>: A value between 0 and 360.
- <^>shade<^>: A value between 0% and 100%. 100% is black.
- <^>blend<^>: Blend makes it easy to blend a color with another color. Here's an example if it's usage:
.box {
color: color-mod(hotpink blend(yellow 59%));
}
Browser Support
👉 Checkout ColorMe.io, a great tool to help you compose colors with the color function.