Skip to content

Serialization of color names #26

@raphlinus

Description

@raphlinus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions