Description
I'm writing the spec for color values right now, and the overall structure is straightforward: a CSSColorValue superclass, and subclasses for each type of color function.
I'm struggling a bit with the best design for the constructor arguments and/or the shorthand functions.
Some arguments, like hue angles, are easy - they're just a CSSNumericValue that needs to match <angle>
. But most of the arguments to color functions are percentages. Obviously I'll accept a CSSNumericValue that matches <percentage>
, but I'd like easier ways to invoke these - writing new CSSRGB(CSS.percent(10), CSS.percent(0), CSS.percent(100))
isn't great.
In particular, I'd like to allow them to accept JS numbers, interpreting the range 0-1 as being equivalent to a percent between 0% and 100%, so you could instead write the preceding function as new CSSRGB(.1, 0, 1)
.
This would generally be unproblematic and straightforward, except that rgb()
accepts <number>
as well, in the range 0-255. And elsewhere in TypedOM (such as new CSSScale()
), passing a raw JS number is equivalent to passing a CSSUnitValue(x, "number")
(see the "rectify a numberish value" algo). So this leaves me with several possibilities, none of which I find great:
-
Always allow JS numbers in the color functions in the 0-1 range. There's no constructor form that's a direct equivalent to the
rgb(0, 128, 255)
syntax. -
Allow JS numbers in the color functions in the 0-1 range, but also allow
CSSUnitValue(x, "number")
to be passed to CSSRGB, with the 0-255 range. -
Don't allow JS numbers at all, only CSSNumericValues. CSSRGB accepts
CSSUnitValue(x, "number")
in the 0-255 range. -
(Definitely bad, not doing this) Allow JS numbers in the 0-1 range for all the color functions except CSSRGB, allow them in the 0-255 range for CSSRGB (in addition to
CSSUnitValue(x, "number")
). -
Same as (3), but also have shorthand functions like
CSS.rgb()
(akin to theCSS.px()
family for numeric values) that act like (1).
I'm considering going with (1), but (5) would be fine as well. I don't like the others very much. Anyone else have opinions?