Skip to content

feat: implementation of contrast-color() from the CSS Color 5#1160

Open
yisibl wants to merge 1 commit intoparcel-bundler:masterfrom
yisibl:contrast-color
Open

feat: implementation of contrast-color() from the CSS Color 5#1160
yisibl wants to merge 1 commit intoparcel-bundler:masterfrom
yisibl:contrast-color

Conversation

@yisibl
Copy link
Copy Markdown
Contributor

@yisibl yisibl commented Feb 25, 2026

The former name of contrast-color() was color-contrast(). In CSS Color 5, its syntax has been simplified to:
contrast-color() = contrast-color( <color> )

Spec: https://drafts.csswg.org/css-color-5/#contrast-color

This function is part of interop-2026 and is already supported in Firefox 146Safari 26 and Chrome 147.0.7707.0(experimental)

This implementation solely parses functions, enabling color values within contrast-color() to be compressed or simplified.

Part of #99

@yisibl yisibl marked this pull request as ready for review February 25, 2026 15:04
This implementation solely parses functions, enabling color values within `contrast-color()` to be compressed or simplified.

Spec: https://drafts.csswg.org/css-color-5/#contrast-color

`contrast-color() = contrast-color( <color> )`

Part of parcel-bundler#99
@yisibl
Copy link
Copy Markdown
Contributor Author

yisibl commented Mar 9, 2026

@devongovett PTAL

@devongovett
Copy link
Copy Markdown
Member

I think contrast-color with a known value should compute to either black or white at build time, similar to how we calculate the value of relative colors and color-mix. There was a previous PR that implemented this (#808), but it also supported parts of the level 6 spec which is not yet final. Perhaps we can scope that down a bit?

@yisibl
Copy link
Copy Markdown
Contributor Author

yisibl commented Mar 9, 2026

This can be done in the next PR. For this PR, I want to focus on implementing the syntax-level parsing.

However, I believe the algorithm currently defined for contrast-color in L5 is subpar. It might not be worth implementing this algorithm; it's better to leave it to the browser to handle.

See https://webkit.org/blog/16929/contrast-color/#:~:text=you%E2%80%99ll%20find%20many%20of%20the%20pairings%20with%20mid%2Dtone%20background%20colors%20don%E2%80%99t%20result%20in%20enough%20contrast.

@devongovett
Copy link
Copy Markdown
Member

This can be done in the next PR.

It would change what gets stored in the AST though right? We wouldn't need a CssColor::ContrastColor variant if we parsed it directly to a color.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants