Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support modern css colors #347

Merged
merged 12 commits into from
Aug 17, 2024
Merged

feat: support modern css colors #347

merged 12 commits into from
Aug 17, 2024

Conversation

gka
Copy link
Owner

@gka gka commented Aug 8, 2024

CSS now supports a modern syntax and the one currently implemented in chroma.js is rebranded as "legacy". So we should go with the times and change the default output of color.css() to the modern syntax.

Since this is a breaking change I think we should bump the version number to 3.

This branch will also add support for Lab, Lch, OkLab, and OkLch CSS exports, e.g.

chroma('hotpink').css('oklab');
  • support configuration of Lab white reference illuminant to match W3C implementation of Lab
  • document new methods getLabWhitePoint and setLabWhitePoint
  • support .css('lab')
  • support .css('lch')
  • support .css('oklab')
  • support .css('oklch')
  • support parsing of modern CSS colors in rgb() space
  • support parsing of modern CSS colors in hsl() space
  • document new CSS color exporting
  • support highlighting of modern CSS color in documentation
  • support parsing of modern CSS colors in lab() space
  • support parsing of modern CSS colors in lch() space
  • support parsing of modern CSS colors in oklab() space
  • support parsing of modern CSS colors in oklch() space

@gka gka marked this pull request as ready for review August 8, 2024 21:56
@gka gka marked this pull request as draft August 8, 2024 21:58
@gka
Copy link
Owner Author

gka commented Aug 12, 2024

cc @zyyv

Alright, changing of the CIELab white reference point can now be done using this API:

chroma('hotpink').lab(); // [65.49,64.24,-10.65]
chroma.setLabWhitePoint('D50');
chroma('hotpink').lab(); // [65.86,63.27,-9.64]

This will affect the CIELab color spaces Lab and Lch. I thought this makes it easier to just switch once and for all rather than having to pass the white point as additional argument in all the places where you can define an output mode.

More importantly, chroma.js will ensure that when you export a color as a CSS Lab string, it uses the D50 white point. Otherwise, the color would show up differently in a browser.

@gka
Copy link
Owner Author

gka commented Aug 12, 2024

I'll add the other CSS color spaces Lch, Oklab, and Oklch before merging this.

@gka gka mentioned this pull request Aug 14, 2024
@gka
Copy link
Owner Author

gka commented Aug 15, 2024

Dang, of course, OKLab is also dependent on a white reference point, and according to the official paper by Björn Ottosson, it's hard-coded to the D65 illuminant:

Oklab uses a D65 white point since this is what sRGB and other common color spaces use.

But as far as I can see, W3C has adapted their implementation of oklab() to also use the D50 illuminant. This means that you can't convert a hex color to an oklab CSS string using chroma.js and then expect a browser to show the same color.

I've solved this problem for the CIE Lab space, but I don't know if I can also do this for the Oklab space. We're using the official conversion matrices, which are hard-coded to D65.

The new matrices have been derived using a higher precision sRGB matrix and with exactly matching D65 values.

@gka
Copy link
Owner Author

gka commented Aug 15, 2024

or perhaps I'm misunderstanding something here. In any case, W3C seems to be using a more complicated method to convert from RGB to OKLab and back. Fortunately, they've published the JS code for their conversions, so we could try to re-use it: https://www.w3.org/TR/css-color-4/#color-conversion-code

@gka gka marked this pull request as ready for review August 17, 2024 09:29
@gka
Copy link
Owner Author

gka commented Aug 17, 2024

merging this now, and will fix the oklab issues in another PR.

@gka gka merged commit f9268c0 into main Aug 17, 2024
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.

1 participant