Web developers and design systems developers often use functions to design components. With the increasing usage of design systems that support multiple platforms, and 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 layouts are calculated. Currently Sass, calc() on HSL values, or PostCSS is used to do this.
- Components with color variations based on calculations from a parent. (i.e. Button with outline that uses the primary button color to adjust the size)
- Theming - Palletes based on a color or set of colors for themes. Especially when a single base system is used with multiple themes
- Uniformity among transformations between components with different primary colors
- 3 functions:
adjust-color
,mix-color
, andcontrast-color
- LCH is the default color space. You can specify transformations in another color space (ie. HSL lightness instead of the default LCH lightness -
lightness(red, lch)
). If the argument is non-compliant with the color space (i.e.lightness(red, cmyk)
), the transformation will be clipped to CMYK after an LCH transformation. - In this example, like
filter
, > 100% or 1 = positive change (i.e. lighter), and < 100% or 1 = negative (i.e. makes it darker) and/or we can do negative and positive values based on 0 (- 30% || + 30%)
- Adjusts color via:
- lightness
- chroma
- hue
adjust-color(<color>, <colorFunctions(<amount>,<optional color space>)>)
- ex.
adjust-color(red, lightness(130%) chroma(0.4));
- ex.
adjust-color(red, lightness(130%, hsl) chroma(0.4, lch));
← with color space argument
- Mix two colors by an specified ammount of the first value. See examples:
mix-color(<color1>, <color2>, <colorFunctions(<amount>,<optional color space>)>)
- ex.
mix-color(red, yellow, lightness(30%) chroma(0.8));
← 30% of red lightness and 70% of yellow lightness, 80% of red alpha and 20% of yellow alpha - ex.
mix-color(red, yellow, lightness(30%, rgb) chroma(0.8, lch));
← specifying color spaces
- Determine which of the list of colors (second input, space-separated list of values) has the highest contrast with the background (first input)
contrast-color(<bgcolor || currentBackground>, <color>{2,})
← which of these (2+) colors is the most contrasted
@lunelson wrote:
I agree that directly calculating on gamma-corrected values is depressingly common. CSS Color 4 does this correctly, and even gives sample code for the color conversions. CSS Color 5 depends on CSS Color 4 and so, also does it correctly.
Unfortunately, Compositing does it the wrong way, mainly for compatibility with Photoshop blending modes which work on gamma-corrected RGB colors (and even use the NTSC values for luminance calculations!)
For HSL, are you applying the HSL equations to the un-gamma-corrected RGB values?