linebender / linebender/color

Serialization of color names

Open
#26 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
110
Forks
14
Avg merge
16h 45m
Merged PRs (30d)
1

Description

There's a basic question that needs a decision (though it doesn't have to happen right now), and some more subtle considerations.

The serialization section of the CSS Color 4 spec requires that a parse of "red" serializes as "red", not "rgb(255, 0, 0)" or "#ff0000". I can see this as potentially useful in applications that process CSS for output to the web, improving human readability. We could do it, but it has some cost and complexity.

The outline of how I'd do it is reserve another bit in Missing (which I'd probably rename "Flags" or somesuch) indicating that it's a named color. Then, when serializing, if the bit is set, the color would be rounded to rgba8, the named color table would be searched, and, if there's a match, the name output. If there's not a match, then it would be serialized as if the bit were not set. The search could be implemented at least 3 ways: linear scan (slow-ish but simple and with no additional data burden), phf (tricky, estimated 284 bytes), or binary search using an additional 142 byte permutation table representing the sort by u32.

But this touches a deeper problem. There is some potentially some divergence between what we want for string output, and strict CSS serialization. Aside from the named colors, we need to serialize out-of-gamut srgb using the color function and srgb colorspace, rather than the rgb function, as the latter clips. Also, if we have color spaces not covered by CSS (#14, #15, possibly others), then those won't serialize to a valid CSS string.

One solution is that we do our own thing for Display on our color types that is not necessarily CSS compatible, and have another way to do strictly CSS compatible serialization. I'd lean toward doing that as Display impl on a CssFormat wrapper type, and also have that feature gated, but am open to other ideas.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the color types, their Display implementations, the Missing flags, and the CSS Color 4 serialization rules linked in the issue. The work first needs a decision between preserving named colors, adding a CssFormat wrapper, and defining CSS-compatible output; it is done when the chosen behavior handles named and out-of-gamut colors consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, rust
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.