Skip to content

[css-color-6] color-contrast() with automatic continuous lightness adjustment #5153

Open
@LeaVerou

Description

@LeaVerou

Current definition: https://drafts.csswg.org/css-color-5/#colorcontrast

While providing a list of colors and picking the one that satisfies a certain contrast constraint is useful for some use cases, it is quite a hassle for others. In virtually every case I've used a contrast calculator, I had a fairly good idea of what colors I wanted, and I just adjusted the lightness of one of the two until it passed WCAG. To use the color-contrast() in its current form would basically mean precomputing a list of various lightnesses, which is suboptimal.

I would find it far more useful for my use cases, if in addition to a list of colors, I could provide just a background and foreground color, optionally a min contrast ratio (would default to 4.5), and optionally a direction to adjust the foreground color (would default to closest).

I.e. something like this:

color-contrast() = color-contrast([lighten | darken]? <color> on <color> [to <number>:1]? )

E.g.

color-contrast(lighten rebeccapurple on wheat to 3:1);

If lighten or darken are provided and white/black respectively don't pass the minimum contrast, we could either go the other direction, or just return white/black.

Of course lightness would be LCH lightness, not HSL lightness. And before you point out that contrast is relative luminance (Y) based and not lightness based: Yes, I'm aware, but:

  • Two colors with the same Y also have the same L
  • Y and L are comonotone (they increase/decrease together, albeit at different rates). I.e. For any colors C1, C2, if Y1 > Y2 then L1 > L2 and vice versa.

So, if there is a lighter/darker color that will pass a given contrast ratio, increasing/decreasing LCH lightness will find it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closed Deferreda11y-trackerGroup bringing to attention of a11y, or tracked by the a11y Group but not needing response.css-color-6

    Type

    No type

    Projects

    Status

    Tuesday

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions