Skip to content

Commit

Permalink
Merge pull request #233 from tbreloff/tom_fixparse
Browse files Browse the repository at this point in the history
fixed parse StackOverflow and added Symbol/Colorant versions; closes …
  • Loading branch information
timholy committed Dec 6, 2015
2 parents a3b9f0c + 3952411 commit f6fc42c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,7 @@ function parse_alpha_num(num::AbstractString)
end


"""
parse(Colorant, desc)
Parse a color description.
This parses subset of HTML/CSS color specifications. In particular, everything
is supported but: "currentColor".
It does support named colors (though it uses X11 named colors, which are
slightly different than W3C named colors in some cases), "rgb()", "hsl()",
"#RGB", and "#RRGGBB' syntax.
Args:
- `Colorant`: literal "Colorant" will parse according to the `desc`
string (usually returning an `RGB`); any more specific choice will
return a color of the specified type.
- `desc`: A color name or description.
Returns:
An `RGB{U8}` color, unless:
- "hsl(h,s,l)" was used, in which case an `HSL` color;
- "rgba(r,g,b,a)" was used, in which case an `RGBA` color;
- "hsla(h,s,l,a)" was used, in which case an `HSLA` color;
- a specific `Colorant` type was specified in the first argument
"""
function Base.parse(::Type{Colorant}, desc::AbstractString)
function _parse_colorant(desc::AbstractString)
desc_ = replace(desc, " ", "")
mat = match(col_pat_hex2, desc_)
if mat != nothing
Expand Down Expand Up @@ -133,7 +108,39 @@ function Base.parse(::Type{Colorant}, desc::AbstractString)
return RGB{U8}(c[1] / 255, c[2] / 255, c[3] / 255)
end

Base.parse{C<:Colorant}(::Type{C}, desc) = convert(C, parse(Colorant, desc))::C
# note: these exist to enable proper dispatch, since super(Colorant) == Any
_parse_colorant{C<:Colorant,SUP<:Any}(::Type{C}, ::Type{SUP}, desc::AbstractString) = _parse_colorant(desc)
_parse_colorant{C<:Colorant,SUP<:Colorant}(::Type{C}, ::Type{SUP}, desc::AbstractString) = convert(C, _parse_colorant(desc))::C

"""
parse(Colorant, desc)
Parse a color description.
This parses subset of HTML/CSS color specifications. In particular, everything
is supported but: "currentColor".
It does support named colors (though it uses X11 named colors, which are
slightly different than W3C named colors in some cases), "rgb()", "hsl()",
"#RGB", and "#RRGGBB' syntax.
Args:
- `Colorant`: literal "Colorant" will parse according to the `desc`
string (usually returning an `RGB`); any more specific choice will
return a color of the specified type.
- `desc`: A color name or description.
Returns:
An `RGB{U8}` color, unless:
- "hsl(h,s,l)" was used, in which case an `HSL` color;
- "rgba(r,g,b,a)" was used, in which case an `RGBA` color;
- "hsla(h,s,l,a)" was used, in which case an `HSLA` color;
- a specific `Colorant` type was specified in the first argument
"""
Base.parse{C<:Colorant}(::Type{C}, desc::AbstractString) = _parse_colorant(C, super(C), desc)
Base.parse{C<:Colorant}(::Type{C}, desc::Symbol) = parse(C, string(desc))
Base.parse{C<:Colorant}(::Type{C}, c::Colorant) = c


macro colorant_str(ex)
isa(ex, AbstractString) || error("colorant requires literal strings")
Expand Down
3 changes: 3 additions & 0 deletions test/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const redF64 = convert(RGB{Float64}, redU8)
@test_throws ErrorException parse(Colorant, "hsl(120, 100, 50)")
@test parse(Colorant, "#D0FF58") === RGB(0xD0uf8,0xFFuf8,0x58uf8)

@test parse(Colorant, :red) === colorant"red"
@test parse(Colorant, colorant"red") === colorant"red"

@test hex(RGB(1,0.5,0)) == "FF8000"
@test hex(RGBA(1,0.5,0,0.25)) == "40FF8000"

Expand Down

0 comments on commit f6fc42c

Please sign in to comment.