# Parse string to color

**URL:** <https://discourse.julialang.org/t/parse-string-to-color/56934>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 11, 2021, 7:49am UTC](https://discourse.julialang.org/t/parse-string-to-color/56934 "2021-03-11T07:49:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 11, 2021, 7:49am UTC](https://discourse.julialang.org/t/parse-string-to-color/56934/1 "2021-03-11T07:49:07Z")

</div>

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
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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 11, 2021, 8:16am UTC](https://discourse.julialang.org/t/parse-string-to-color/56934/2 "2021-03-11T08:16:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 11, 2021, 8:19am UTC](https://discourse.julialang.org/t/parse-string-to-color/56934/3 "2021-03-11T08:19:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [March 13, 2021, 9:24am UTC](https://discourse.julialang.org/t/parse-string-to-color/56934/4 "2021-03-13T09:24:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [March 13, 2021, 9:36am UTC](https://discourse.julialang.org/t/parse-string-to-color/56934/5 "2021-03-13T09:36:18Z")

</div>

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!
