Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed parse StackOverflow and added Symbol/Colorant versions; closes … #233

Merged
merged 2 commits into from
Dec 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This attaches the docstring to _parse_colorant when it should be attached to Base.parse.

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