color-js / color.js

Color conversion & manipulation library by the editors of the CSS Color specifications
https://colorjs.io
MIT License
1.81k stars 80 forks source link

Customize `serialize()` output types without having to specify a whole format #525

Closed LeaVerou closed 1 month ago

LeaVerou commented 1 month ago

First step for https://github.com/color-js/color.js/issues/515

Right now, the only way to customize the output types (e.g. to get a number for OKLCh L or a percentage for alpha) is to specify a whole new format that mirrors the original minus the changes. This is incredibly heavyweight.

I propose the following new serialize() settings:

Right now I’m leaning towards Option 1 as nested objects are both slower and annoying when you only need one.

LeaVerou commented 1 month ago

I ended up going with coords and alpha which contain all params for serializing coords and alpha respectively. Currently coords only takes an array of types, and alpha can take either a boolean (true = always, false = never) or a string ("<percentage>" or "<number>") or both as a {type, include} object.