I also have got a couple of questions regarding this issue:
This is what I've come up with:
Should we support RGB/RGBA as stings? Something else I'm missing?
all those cases look good a few more we should probably accept:
It looks like you can also definite colours using
The other case is how does the parsed value
I think best that we return an instance of
Another question: should we always validate to the same thing or leave the output format as it came in? Eg. if someone enters
Genuinely not sure what's best here. I think we have to have:
Turns out this is much more complicated than I thought, but quite fun :-)
By the way, it seems that defining
Another thing that I've just realised is that if we do all these fancy conversions, we have to take care of alpha composition. We cannot just drop alpha from
I would certainly expect the output format match the input's one. I think a user can add conversion to the needed format at a post validation step.
May be also splitted as
Return a lower cased name of the colour passed
This brings us to the first question I've asked: if we only validate that a colour passed is in the set of predefined colours defined in CSS3/SVG specification? Or we just try to validate a colour returning it's predifined name if we can find it, otherwise we fall back to
I woud go for an alias of
Yes, we're checking it's in the list of predefined colours from css3.
We don't want to do any clever stuff with the alpha channel, if it's there (and not 1.0/255) the colour has to be defined as
We can happily ignore
Let's just call it
For clarity I think
class Color: __slots__ = '_rgba', '_original', '_color_match' def __init__(...): self._rgba: Tuple[int, int, int, Optional[int]] = ... self._original: str = ... self._color_match: Union[str, None, bool] = None # used to cache the named color or that there is none
Obviously do it how you think best, but I think the above makes sense as a starting point.
* Color validator MVP * Color Validator refactored * Small optimizations applied * Code coverage improved * hex processing improved, json encoder and repr added * Add documentation, update HISTORY (#504) * Increase test coverage (#504) * hex helpers refactored (#504) * Fixes after code review (#509) * Color Type section in docs reduced * Check for valid but unnamed colors * Minor fixes: typo in docs, default value for float comparison function * rewrite Color * fix color tests * tweaks and improve docs * tweaks and change Color.__str__ * add as_hsla and as_hsl * support more rgb(a) formats * add hsl parsing * parsing hex with alpha channel * fix hsl parsing * simpler failure/fallback rules for color display * storage as floats internally * tweak docs * fix type hints