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

ColorUtils normalizeCssColor and parseRgba returns incorrect values for oklab #2270

Open
mattrunyon opened this issue Oct 29, 2024 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@mattrunyon
Copy link
Collaborator

In ThemeUtils.resolveCssVariablesInRecord, we convert the colors to hex so Monaco and plotly are ok with the colors; however, this calls ColorUtils.normalizeCssColor which incorrectly parses oklab colors as rgba and returns something like `#0.999999999999980.4ccccccccccccc0.4ff'

I think there are a few holes in the logic here:

  1. ColorUtils.asRgbOrRgbaString returns a computed color without checking if it is actually rgb/rgba. This is fine for hsl which resolves to rgb, but oklab colors do not compute into rgb in CSS. They resolve their internal values and you end up with oklab(L a b). 3 numbers in oklab

  2. ColorUtils.parseRgba parses oklab(0.5 0.5 0.5) because it doesn't actually check for anything about rgb, it just looks for 3 numbers. If the value does not start with color(srgb, it just assumes anything that contains 3 or 4 numbers is RGB(A).

    Further, there's a comment saying "We've already checked it's a valid color with CSS.supports". This is not always true because we depend on the caller to have verified the color. There's no verification in the method and the doc string mentions nothing about this (other than assumes already valid rgb/rgba.

  3. Since ColorUtils.normalizeCssColor calls ColorUtils.parseRgba with values that haven't been properly verified to be rgb/rgba, and ColorUtils.parseRgba does no validation it's operating on an rgb/rgba color, ColorUtils.normalizeCssColor passes the already bad parsed value into ColorUtils.rgbaToHex8. Since oklab colors use decimals (and values can be negative), we end up passing completely invalid r/g/b values into rgbaToHex8 and creating an even more incorrect string

Depending on how correct we want to be, this will also have trouble with any other color spaces as it only checks for srgb when using color. So other color spaces with 3 numbers will be treated as rgb and expect values of 0-255.

In addition to these issues, we have separate GridColorUtils which has a bunch of utils for converting oklab/srgb colors. IMO almost all of these should be moved into ColorUtils because they are not grid specific. The only one that is is darkenForDepth which has to deal with tree colors.

@mattrunyon mattrunyon added bug Something isn't working triage Issue requires triage labels Oct 29, 2024
@mofojed mofojed added this to the November 2024 milestone Oct 29, 2024
@mofojed mofojed removed the triage Issue requires triage label Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants