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

Named color parsing #15128

Merged
merged 1 commit into from Sep 14, 2023
Merged

Named color parsing #15128

merged 1 commit into from Sep 14, 2023

Conversation

tschaub
Copy link
Member

@tschaub tschaub commented Sep 14, 2023

This change updates the ol/color.js module so it doesn't depend on the DOM.

I added a dependency on the color-name package. This is a simple lookup of CSS named colors to RGB values. We could include this lookup directly in the ol/color.js module if we don't want the dependency. But it may eventually be useful to depend on https://github.com/colorjs/color-parse or other packages that depend on the same.

We didn't previously have tests that checked that fromString() worked with named colors. I added tests to cover this and moved all color tests to demonstrate that they pass without the DOM.

@github-actions
Copy link

📦 Preview the website for this branch here: https://deploy-preview-15128--ol-site.netlify.app/.

if (name === 'transparent') {
return 'rgba(0,0,0,0)';
}
return '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a good default value? It is a string but no valid rgb(a) string. most likely i would suspect this should not cause an error, because normally if colors are allowed, an empty strings is also ok.

Also this technically differs from the previous implementation because the browsers would actually accept any color name and just parse it randomly. I think this can be neglected as it is an unspecified case - also browsers handle this differently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as this is used in fromStringInternal_ and not exported the empty string makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous behavior in this case was not really predictable. If you el.style.color = 'oops', then getComputedStyle(el).color depends on what other CSS rules might apply. For example, on this page, that returns rgb(31, 35, 40). On about:blank, it returns 'rgb(0, 0, 0)'. I thought that I previously saw a case where the result was '', but cannot reproduce that now.

I think that the new behavior with the exported methods is probably more "correct" now. Calling asArray('oops') now throws new Error('Invalid color') whereas previously I think the behavior would depend on CSS rules that might be on the page.

Comment on lines +79 to +82
expect(() => {
fromString(c[0]);
}).to.throwException(expected);
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this throw an exception? I would expect an empty string.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, this calls fromString not fromNamed

Copy link
Member

@simonseyock simonseyock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had some questions but answered them myself :D looks good to me!

@tschaub
Copy link
Member Author

tschaub commented Sep 14, 2023

Good comments. Thanks for the review. Maybe we will want to revisit the behavior of fromNamed in the future (perhaps it should throw on invalid colors instead of returning ''). But for now I think the exported methods behave as we want them to.

@tschaub tschaub merged commit c1cb2dd into openlayers:main Sep 14, 2023
8 checks passed
@tschaub tschaub deleted the color-parsing branch September 14, 2023 10:25
@mike-000 mike-000 mentioned this pull request Sep 14, 2023
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.

None yet

2 participants