1. Introduction
This section is not normative.
Web developers, design tools and design system developers often use color functions to assist in scaling the design of their component color relations. With the increasing usage of design systems that support multiple platforms and multiple user preferences, like the increased capability of Dark Mode in UI, this becomes even more useful to not need to manually set color, and to instead have a single source from which schemes are calculated.
Currently Sass, calc() on HSL values, or PostCSS is used to do this. However, preprocessors are unable to work on dynamically adjusted colors; all current solutions are restricted to the sRGB gamut and to the perceptual limitations of HSL (colors are bunched up in the color wheel, and two colors with visually different lightness, like yellow and blue, can have the same HSL lightness).
This module adds three functions: color-mix(), color-contrast(), and a way to modify colors.
The perceptually uniform ``lch()`` color space is used for mixing by default, as this has no gamut restrictions and colors are evenly distributed. However, other color spaces can be specified, including ``hsl()`` or ``srgb`` if desired.
2. Color Spaces
Mixing or otherwise combining colors has different results depending on the color space used. In some cases, the result of physically mixing two colored lights is desired (in that case, the CIE XYZ color space is appropriate, because it is linear in light intensity). In other cases, colors which are evenly spaced perceptually are desired (in which case, the CIE Lab color space is designed to be perceptually uniform). Alternatively, maximizing chroma so that color mixtures follow along the hue wheel is wanted (CIE LCH works well for this). Lastly, compatibility with legacy Web content may be the most important consideration. (the sRGB color space, which is neither linear-light nor perceptually uniform, is the choice here).
<color-space> = srgb | hsl | hwb | xyz | lab | lch
The srgb, hsl, hwb, xyz, lab, and lch keywords each refer to their corresponding color space.
When no <color-space> is specified, the mixing is done in the lch color space. The xyz color space is CIE XYZ, with a D50 whitepoint, and allows computation to be done in a linear-light-intensity space.
3. Mixing colors: the color-mix() function
This function takes two <color> specifications and returns the result of mixing them, in a given <color-space>, by a specified amount.
color-mix() = color-mix( in <color-space> , [ <color> && <percentage [0,100]>? ]#{2} <hue-adjuster>?) <hue-adjuster> = shorter | longer | increasing | decreasing | specified
Percentages are required to be in the range 0% to 100%. Negative percentages are specifically disallowed. The percentages are normalized as follows:
-
Let p1 be the first percentage and p2 the second one.
-
If both percentages are omitted, they each default to 50% (an equal mix of the two colors).
-
Otherwise, if p2 is omitted, it becomes 100% - p1
-
Otherwise, if p1 is omitted, it becomes 100% - p2
-
If the percentages sum to zero, the function is invalid.
-
Otherwise, if both are provided and add up to greater than 100%, they are scaled accordingly so that they add up to 100%.
-
Otherwise, if both are provided and add up to less than 100%, the sum is saved as an alpha multiplier. They are then scaled accordingly so that they add up to 100%.
This means that p1 becomes p1 / (p1 + p2) and p2 becomes p2 / (p1 + p2).
color-mix ( in lch, purple50 % , plum50 % ) color-mix ( in lch, purple50 % , plum) color-mix ( in lch, purple, plum50 % ) color-mix ( in lch, purple, plum) color-mix ( in lch, plum, purple) color-mix ( in lch, purple80 % , plum80 % )
All produce a 50-50 mix of purple and plum, in lch: lch(51.51% 52.21 325.8) which is rgb(68.51% 36.01% 68.29%).
However, this form is not the same, as the alpha is less than one:
color-mix ( in lch, purple30 % , plum30 % )
This produces lch(51.51% 52.21 325.8 / 0.6) which is rgb(68.51% 36.01% 68.29% / 0.6).
After normalizing both percentages, the result is produced via the following algorithm:
-
Both colors are converted to the specified <color-space>. If the specified color space has a smaller gamut than the one in which the color to be adjusted is specified, gamut mapping will occur.
-
Colors are then interpolated in the specified color space, as described in CSS Color 4 §13 Interpolation. If the specified color space is a cylindrical-polar-color space, then the <hue-adjuster> controls the interpolation of hue, as described in CSS Color 4 §13.3 Hue interpolation. If no <hue-adjuster> is specified, it is as if shorter had been specified. If the specified colorspace is a rectangular-orthogonal-color space, then specifying a <hue-adjuster> is not an error, but has no effect.
-
If an alpha multiplier was produced during percentage normalization, the alpha component of the interpolated result is multiplied by the alpha multiplier.
The result of mixing is the color at the specified percentage along the progression of the second color to the first color.
Note: As a corrollary, a percentage of 0% just returns the other color converted to the specified color space, and a percentage of 100% returns the same color converted to the specified color space.
color-mix ( in lch, peru40 % , palegoldenrod)
The mixing is done in lch() color space. Here is a top-down view, looking along the neutral L axis:
The calculation is as follows:
-
peru is lch(62.253% 54.011 63.677)
-
palegoldenrod is lch(91.374% 31.406 98.834)
-
the mixed lightness is 62.253 * 40/100 + 91.374 * (100-40)/100 = 79.7256
-
the mixed chroma is 54.011 * 40/100 + 31.406 * (100-40)/100 = 40.448
-
the mixed hue is 63.677 * 40/100 + 98.834 * (100-40)/100 = 84.771
-
the mixed result is lch(79.7256% 40.448 84.771)
Note: interpolating on hue and chroma keeps the intermediate colors as saturated as the endpoint colors.
color-mix ( in lch, teal65 % , olive);
The calculation is as follows:
-
sRGB teal (#008080) is lch(47.9855% 31.6903 196.4524)
-
sRGB olive (#808000) is lch(52.1496% 56.8124 99.5746)
-
mixed lightness is 47.9855 * 0.65 + 52.1496 * 0.35 = 49.4429
-
mixed chroma is 31.6903 * 0.65 + 56.8124 * 0.35 = 40.4830
-
mixed hue is 196.4524 * 0.65 + 99.5746 * 0.35 = 162.5452
-
mixed result is lch(49.4429% 40.4830 162.5452)
-
which is a slightly-blueish green: rgb(7.7377% 52.5730% 37.3213%)
The choice of mixing colorspace can have a large effect on the end result.
color-mix ( in lch, white, black); color-mix ( in xyz, white, black); color-mix ( in srgb, white, black);
The calculation is as follows:
-
sRGB white (#008080) is lch(100% 0 0)
-
sRGB black (#808000) is lch(0% 0 0)
-
The mix in LCH is lch(50% 0 0)
-
The mix in XYZ is lch(76% 0 0)
-
The mix in sRGB is lch(53.4% 0 0)
The mix in LCH gives an L value of 50%, a perfect mid gray, exactly as expected (mixing in Lab would do the same, as the Lightness axis is the same in LCH and Lab).
The mix in XYZ gives a result that is too light; XYZ is linear-light but is not perceptually uniform. The mix in sRGB gives a result that is a bit too light; sRGB is neither perceptually uniform nor linear-light.
4. Selecting the most contrasting color: the color-contrast() function
This function takes, firstly, a single color (typically a background, but not necessarily), secondly, a list of two or more colors, and thirdly, an optional target luminance contrast [WCAG21]. It selects from that list the first color color to meet or exceed the target contrast. If no target is specified, it selects the first color with the highest contrast to the single color.
The single color is separated from the list with the keyword vs and the target contrast, if present, is separated from the list with the keyword to.
color-contrast() = color-contrast( <color> vs <color>#{2,} [ to [<number> | AA | AA-large | AAA | AAA-large]]? )
The keyword AA is equivalent to 4.5, AA-large is equivalent to 3, AAA is equivalent to 7, and AAA-large is equivalent to 4.5 .
color-contrast ( wheat vs tan, sienna, var ( --myAccent), #d2691e)
The calculation is as follows:
-
wheat (#f5deb3), the background, has relative luminance 0.749
-
tan (#d2b48c) has relative luminance 0.482 and contrast ratio 1.501
-
sienna (#a0522d) has relative luminance 0.137 and contrast ratio 4.273
Suppose myAccent has the value #b22222:
-
#b22222 has relative luminance 0.107 and contrast ratio 5.081
-
#d2691e has relative luminance 0.305 and contrast ratio 2.249
color-contrast ( wheat vs bisque, darkgoldenrod, olive, sienna, darkgreen, maroon to AA)
The calculation is as follows:
-
wheat (#f5deb3), the background, has relative luminance 0.749
-
bisque (#ffe4c4) has relative luminance 0.807 and contrast ratio 1.073
-
darkgoldenrod (#b8860b) has relative luminance 0.273 and contrast ratio 2.477
-
olive (#808000 ) has relative luminance 0.200 and contrast ratio 3.193
-
sienna (#a0522d) has relative luminance 0.137 and contrast ratio 4.274
-
darkgreen (#006400 ) has relative luminance 0.091 and contrast ratio 5.662
-
maroon (#800000 ) has relative luminance 0.046 and contrast ratio 8.333
The first color in the list which meets the desired contrast ratio of 4.5 is darkgreen.
color-contrast ( wheat vs bisque, darkgoldenrod, olive, sienna, darkgreen, maroon to5.8 )
The calculation is as follows:
-
the relative luminances and contrast ratios are the same as the previous example.
The first color in the list which meets the desired contrast ratio of 5.8 is maroon.
The colors in the list (after the keyword vs) are tested sequentially, from left to right; a color is the temporary winner if it has the highest contrast of all those tested so far.
List traversal is terminated once the target contrast has been met or exceeded.
Once the end of the list is reached, if there is no target contrast, the current temporary winner is the overall winner. Thus, if two colors in the list happen to have the same contrast, the earlier in the list wins because the later one has the same contrast, not higher.
If there is a target contrast, and the end of the list is reached without meeting that target, either white or black is returned, whichever has the higher contrast.
color-contrast ( wheat vs bisque, darkgoldenrod, olive to AA)
The calculation is as follows:
-
the relative luminances and contrast ratios are the same as the previous example.
No color in the list meets the desired contrast ratio of 4.5, so black is returned as the contrast (15.982) is higher than that of white (1.314).
foo{ --bg : hsl ( 200 50 % 80 % ); --purple-in-hsl : hsl ( 300 100 % 25 % ); color : color-contrast ( var ( --bg) vshsl ( 200 83 % 23 % ), purple, var ( --purple-in-hsl)); }
The calculation is as follows:
-
--bg is rgb(179 213 230) which has relative luminance 0.628835
-
hsl(200 83% 23%) is rgb(10 75 107) which has relative luminance 0.061575 and contrast ratio 6.08409
-
purple is rgb(128 0 128) which has relative luminance 0.061487 and contrast ratio 6.08889
-
--purple-in-hsl is also rgb(128 0 128) which has relative luminance 0.061487 and contrast ratio 6.08889. This is not greater than the contrast for purple, so purple wins.
The calculated values here are shown to six significant figures, to demonstrate that early rounding to a lower precision would have given the wrong result (0.061575 is very close to 0.061487; 6.08409 is very close to 6.08889).
5. Modifying colors
Note: There are currently two proposals for modifying colors: color-adjust and Relative color syntax.
there are two proposals for color modification (proposal 1, proposal 2). The CSS WG expects that the best aspects of each will be chosen to produce a single eventual solution. <https://github.com/w3c/csswg-drafts/issues/3187>
5.1. Adjusting colors: the color-adjust function
This function takes one <color> specification and returns the result of adjusting that color, in a given color space, by a specified color-adjuster.
Unless otherwise specified, the adjustment is done in the lch color space.
Multiple color functions can be specified.
color-adjust() = color-adjust( <color> <color-adjuster> && [ in <color-space> ]? )
<color-adjuster> = [ [ <srgb-adjuster> | <hsl-adjuster> | <hwb-adjuster> | <xyz-adjuster> | <lab-adjuster> | <lch-adjuster> ] | alpha ] <percentage> <srgb-adjuster> = red | green | blue <hsl-adjuster> = hue | saturation | lightness <hwb-adjuster> = hue | whiteness | blackness <xyz-adjuster> = x | y | z <lab-adjuster> = lightness | a | b <lch-adjuster> = lightness | chroma | hue
The meaning of the adjusters is defined by color space. For example, if the <color-space> is hsl, hue means the HSL hue, which is not the same as the LCH hue; if the color space is lch, lightness means the LCH Lightness, which is the same as Lab lightness, but different to the HSL Lightness.
Only the <color-adjuster>s defined for a given <color-space> are available. For example, it is not possible to use the HWB whiteness adjuster unless the color space is hwb. The alpha adjuster is available on any <color-space>.
Note: not all <color-adjuster>s are equally useful. Adjustments in LCH are the most useful, which is why it is the default. Adjustments on the a and b axes are rarely needed. Adjustments in the srgb-related spaces (srgb itself, hsl, hwb) are provided mainly for backward compatibility with the sorts of adjustments currently done in CSS preprocessors. Adjusting the individual x, y and z channels will produce significant hue shifts; however, adjusting all three channels together is useful and will lighten or darken the color.
color-adjust ( peru lightness-20 % );
The calculation is as follows:
-
peru (#CD853F) is lch(62.2532% 54.0114 63.6769)
-
adjusted lightness is 62.2532% - 20% = 42.2532%
-
adjusted result is lch(42.2532% 54.0114 63.6769)
-
which is rgb(57.58% 32.47% 3.82%)
5.2. Relative color syntax
In previous levels of this specification, the color functions could only specify colors in an absolute manner, by directly specifying all of the color channels.
The new relative color syntax allows existing colors to be modified using the color functions: if an origin color is specified, then each color channel can either be directly specified, or taken from the origin color (and possibly modified with math functions).
The precise details of each function’s changes to accomodate relative colors are listed below, but they all follow a common structure:
-
An origin color can be specified with a ''from <color>'' value at the start of the function.
-
If an origin color is specified, the remaining arguments can either be specified directly, as normal, be specified as a channel keyword referring to one of the channels of the origin color. Math functions can also use these keywords to do dynamic modifications of the origin color’s channels.
-
Relative color syntax doesn’t change whether an argument is required or optional. If the alpha value is omitted, however, it defaults to taking from the origin color (rather than defaulting to 100%, as it does in the absolute syntax).
If the origin color was originally specified with a different color function, it’s first converted into the chosen color function, so it has meaningful values for the channels.
html{ --bg-color : blue; } .overlay{ background : rgb ( fromvar ( --bg-color) r g b /80 % ); }
In this example, the r, g, and b channels of the origin color are unchanged, indicated by specifying them with the keywords drawing their values from the origin color, but the opacity is set to 80% to make it slightly transparent, regardless of what the origin color’s opacity was.
html{ --color : green; } .foo{ --darker-accent : lch ( fromvar ( --color) calc ( l /2 ) c h); }
In this example, the origin color is darkened by cutting its lightness in half, without changing any other aspect of the color.
Note as well that the origin color is a color keyword (effectively RGB), but it’s automatically interpreted as an LCH color due to being used in the lch() function.
For example, to do a rough approximation of grayscaling a color:
--blue-into-gray : rgb ( fromvar ( --color) calc ( r *.3 + g *.59 + b *.11 ) calc ( r *.3 + g *.59 + b *.11 ) calc ( r *.3 + g *.59 + b *.11 ));
Using this, red would become rgb(30% 30% 30%), green would become rgb(59% 59% 59%), and blue would become rgb(11% 11% 11%). A more moderate color, like darkolivegreen, which has RGB values rgb(85 107 47), would become approximately rgb(37% 37% 37%).
(Note, tho, that an easier and more accurate way to grayscale a color is to use the lch() function, as that color space is more accurate to human perception: lch(from var(--color) l 0 h) preserves the lightness, but zeroes out the chroma, which determines how "colorful" the color is.)
5.2.1. Relative RGB colors
The grammar of the rgb() function is extended as follows:
rgb() = rgb( <percentage>{3} [ / <alpha-value> ]? ) | rgb( <number>{3} [ / <alpha-value> ]? ) | rgb( [ from <color> ]? [ <number> | <percentage> ]{3} [ / <alpha-value> ]? )
Within a relative color syntax rgb() function, the allowed channel keywords are:
-
r, g, and b are all <percentage>s that correspond to the origin color’s red, green, and blue channels after its conversion to sRGB
-
alpha is a <percentage> that corresponds to the origin color’s alpha transparency
rgb(from indianred 255 g b)
This takes the sRGB value of indianred (205 92 92) and replaces the red channel with 255 to give rgb(255 92 92).
5.2.2. Relative HSL colors
The grammar of the hsl() function is extended as follows:
hsl() = hsl([from <color>]? <hue> <percentage> <percentage> [ / <alpha-value> ]? )
Within a relative color syntax hsl() function, the allowed channel keywords are:
-
h is an <angle> that corresponds to the origin color’s HSL hue after its conversion to sRGB, normalized to a [0deg, 360deg) range
-
s and l are <percentage>s that correspond to the origin color’s HSL saturation and lightness after its conversion to sRGB
-
alpha is a <percentage> that corresponds to the origin color’s alpha transparency
--accent: lightseagreen; --complement: hsl(from var(--accent) calc(h + 180deg) s l);
lightseagreen is hsl(177deg 70% 41%), so --complement is hsl(357deg 70% 41%)
5.2.3. Relative HWB colors
The grammar of the hwb() function is extended as follows:
hwb() = hwb([from <color>]? <hue> <percentage> <percentage> [ / <alpha-value> ]? )
Within a relative color syntax hwb() function, the allowed channel keywords are:
-
h is an <angle> that corresponds to the origin color’s HWB hue after its conversion to sRGB, normalized to a [0deg, 360deg) range
-
w and b are <percentage>s that correspond to the origin color’s HWB whiteness and blackness after its conversion to sRGB
-
alpha is a <percentage> that corresponds to the origin color’s alpha transparency
5.2.4. Relative Lab colors
The grammar of the lab() function is extended as follows:
lab() = lab([from <color>]? <percentage> <number> <number> [ / <alpha-value> ]? )
Within a relative color syntax lab() function, the allowed channel keywords are:
-
l is a <percentage> that corresponds to the origin color’s CIE Lightness
-
a and b are <number>s that correspond to the origin color’s CIELab a and b axises
-
alpha is a <percentage> that corresponds to the origin color’s alpha transparency
-
lab(from var(--mycolor) l a b / 100%) sets the alpha of var(--mycolor) to 100% regardless of what it originally was.
-
lab(from var(--mycolor) l a b / calc(alpha * 0.8)) reduces the alpha of var(--mycolor) by 20% of its original value.
-
lab(from var(--mycolor) l a b / calc(alpha - 20%)) reduces the alpha of var(--mycolor) by 20% of 100%.
Note that all the adjustments are lossless in the sense that no gamut clipping occurs, since lab() encompasses all visible color. This is not true for the alpha adjustments in the sRGB based functions (such as 'rgb()', 'hsl()', or 'hwb()'), which would also convert to sRGB in addition to adjusting the alpha transparency.
--mycolor: orchid; // orchid is lab(62.753% 52.460 -34.103) --mygray: lab(from var(--mycolor) l 0 0) // mygray is lab(62.753% 0 0) which is rgb(59.515% 59.515% 59.515%)
5.2.5. Relative LCH colors
The grammar of the lch() function is extended as follows:
lch() = lch([from <color>]? <percentage> <number> <hue> [ / <alpha-value> ]? )
Within a relative color syntax lch() function, the allowed channel keywords are:
-
l is a <percentage> that corresponds to the origin color’s CIE Lightness
-
c is a <number> that corresponds to the origin color’s LCH chroma
-
h is an <angle> that corresponds to the origin color’s LCH hue, normalized to a [0deg, 360deg) range.
-
alpha is a <percentage> that corresponds to the origin color’s alpha transparency
--accent: lightseagreen; --complement: LCH(from var(--accent) l c calc(h + 180deg));
lightseagreen is LCH(65.4937% 39.4484 190.1013), so --complement is LCH(65.4937% 39.4484 370.1013)
--mycolor: orchid; // orchid is lch(62.753% 62.571 326.973) --mygray: lch(from var(--mycolor) l 0 h) // mygray is lch(62.753% 0 326.973) which is rgb(59.515% 59.515% 59.515%)
But now (since the hue was preserved) re-saturating again
--mymuted: lch(from var(--mygray) l 30 h); // mymuted is lch(62.753% 30 326.973) which is rgb(72.710% 53.293% 71.224%)
5.2.6. Relative color-function colors
The grammar of the color() function is extended as follows:
color() = color( [from <color>]? <colorspace-params> [ / <alpha-value> ]? ) colorspace-params = [<custom-params> | <predefined-rgb-params> | <xyz-params>] custom-params = <dashed-ident> [ <number> | <percentage> ]# predefined-rgb-params = <predefined-rgb> [ <number> | <percentage> ]{3} predefined-rgb = srgb | display-p3 | a98-rgb | prophoto-rgb | rec2020 xyz-params = xyz <number>{3}
Within a relative color syntax color() function using <custom-params>, the number and name of the allowed channel keywords are:
-
defined by the components descriptor on the corresponding @color-profile, if present; otherwise, no relative color manipulation is valid. They are <percentage>s that correspond to the origin color’s channels after its conversion to the color space of the color profile.
Within a relative color syntax color() function using <predefined-rgb-params>, the allowed channel keywords are:
-
r, g, and b are all <percentage>s that correspond to the origin color’s red, green, and blue channels after its conversion to the predefined RGB color space.
Within a relative color syntax color() function using <xyz-params>, the allowed channel keywords are:
-
x, y, z are all <number>s that correspond to the origin color’s X, Y and Z channels after its conversion to relative, D50-adapted CIE XYZ color space.
6. Security and Privacy Considerations
No new security or privacy considerations have been reported on this specification.
7. Accessibility Considerations
This specification introduces a new feature to help stylesheet authors write stylesheets which conform to WCAG 2.1 section 1.4.3 Contrast (Minimum).
8. Changes
8.1. Since the FPWD of 10 June 2020
- Added relative color syntax for the color() function
- Clarified that the color-adjuster is not optional
- Clarified that the percentage in color-mix is mandatory
- Moved hue-adjuster back to color-mix whee it belongs
- Added example with different mxing color spaces
- Added examples of percentage normalization in color-mix()
- Explicitly excluded negative percentages in color-mix()
- Percentages in color-mix() summing to less than 100% produce an alpha transparency less than 100%
- Consistently used the term color space rather than colorspace, defined <color-space> token
- Corrected color-contrast grammar
- Added an optional target contrast ratio to color-contrast()
- Corrected adjuster grammar
- Noted that the corner case of percentages summing to zero needs to be handled
- Clarified order of operations in color-mix()
- Updated examples to match current grammar
- Defined how percentages are normalized
- Clarify meaning of 0% and 100% in color-mix()
- Definition of adjusters moved from color-mix() to color-adjust()
- Allow arguments to color-mix() to be in any order
- Mandatory color space for color-mix()
- Allowed the percentage in color-mix() to come before the color
- Added explicit algorithm for color-mix()
- Removed adjusters from color-mix() and simplified the grammar
- Added the "in" keyword to specify the color space used for mixing
- Required color-contrast() list to have at least two items
- Improved explanation of the relative color syntax
- Link to CSS 4 definition of color serialization
- Added separate section for color spaces
- Updated color-adjust example
- Added explanatory diagrams
- Deal with unresolved percentages
- Normalize arguments to color-mix
- Allow percentages for adjusters
- Link fixes
- Updated color-mix grammar, allowing adjusters, add alpha adjuster
- Corrections to some examples
- Updated Security and Privacy section
- added vs keyword to color-contrast
- added xyz adjuster to grammar
- added hue adjuster keywords
- add XYZ color space for mixing
- defined color-adjuster and color space
- allowed mix percent to default to 50%
- added worked examples and diagrams
- corrected minor spelling, syntax and formatting issues