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

Make it possible to serialize to the same format as parsed #515

Closed
LeaVerou opened this issue May 15, 2024 · 0 comments
Closed

Make it possible to serialize to the same format as parsed #515

LeaVerou opened this issue May 15, 2024 · 0 comments
Labels
Breaking change enhancement New feature or request
Milestone

Comments

@LeaVerou
Copy link
Member

LeaVerou commented May 15, 2024

Currently, Color.js supports parsing and serializing into a variety of formats, but it's exceedingly difficult to roundtrip in a way that doesn't switch to a different format.

This results in subpar user experiences where e.g. the user will input "oklch(0.6 20% 180deg)" and the UI will change their input to oklch(60% 0.08 180). For a demonstration, try editing the swatch in this <color-picker> component I’m working on.

However, parse() actually does keep track of metadata like what format was used and what was the coordinate type for each coordinate, it's just that serialize() doesn't really have a way to take advantage of it. It does not even expose a way to customize the output type of a specific component without having to completely redefine the format, a common user pain point.

I propose:

  • serialize() should accept an array or object for coordinate types (e.g. ["<percentage>", "<number>", "<angle>"] or {l: "<number>", c: "<percentage>"}). Perhaps coordTypes?
  • In the OOP API, Color objects that have been created by parsing strings, should keep track of the parsing metadata (e.g. in a symbol field). Folks using the procedural API can manage it on their own.
  • color.toString() should take advantage of any parsing metadata stored in color and serialize to the same format

Thoughts?


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking change enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant