Parse string to color

Say I have a string "HSV(204,0.7,0.7)", how can I parse it to an instance of one of the ColorTypes types?

MWE (I’m looking for function f):

julia> txt = "HSV(204,0.7,0.7)"
"HSV(204,0.7,0.7)"

julia> f(txt) == HSV{Float64}(204.0,0.7,0.7)
true

Keep in mind that the color could be any subtype of ColorTypes.Colorant (e.g. RGB, Lab, Gray, etc).

Is there anything in the Colors.jl / Parsers.jl - verses for that?

I don’t think these types have parsers defined, but you could split on the ( and ,s (with split or a regexp), and parse the results. Maybe map strings like "HSV" to a constructor HSV with a Dict you generate, that should provide some validation.

Yea, I hoped some implementation would have already existed, so I wouldn’t need to do it myself… But, that’s fine. Cool, thanks!

As a side note, parse in Colors.jl supports the CSS notations. In order to avoid conflicts with the CSS specification (in the future), I am reluctant to add support for other notations.

In the not-too-distant future, the “lab()” and “gray()” function notations will be supported, but they are not compatible with the Lab and Gray constructors.

1 Like

I see, thanks.
I always thought your icon was a new age version of Yoda… I now see it’s a flower, ha ha ha!

2 Likes