Skip to content

Commit

Permalink
Remove depwarn for X11 color names with spaces
Browse files Browse the repository at this point in the history
This also removes `color(::AbstractString)` and depreates `parse(::Type, ::Colorant)`.
  • Loading branch information
kimikage committed Mar 2, 2021
1 parent cf7b34b commit dd97849
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/namedcolorcharts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function ColorChartSVG(colorcategory)
write(io, """<tspan x="$cx" y="4.2">$name</tspan>""")
else
m = match(r"^(dark|pale|light|lemon|medium|blanched|(?:.+(?=white|blue|purple|blush)))(.+)$", name)
if m != nothing
if m !== nothing
write(io, """<tspan x="$cx" y="2.3">$(m.captures[1])</tspan>""")
write(io, """<tspan x="$cx" y="4.5">$(m.captures[2])</tspan>""")
else
Expand Down
9 changes: 0 additions & 9 deletions docs/src/constructionandconversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,6 @@ julia> parse(RGB{N0f16}, "indianred")
RGB{N0f16}(0.80392,0.36078,0.36078)
```

You can also pass a color (`Colorant` value) to the second argument of the
[`parse`](@ref) function, but in this case, `parse` does not apply any
conversion on the color.

```jldoctest example
julia> parse(RGB{N0f8}, HSL{Float64}(120, 1, 0.5)) # use `convert` instead
HSL{Float64}(120.0,1.0,0.5)
```


You can convert colors to hexadecimal strings using the [`hex`](@ref) function.
Note that the conversion result does not have the prefix `"#"`.
Expand Down
2 changes: 1 addition & 1 deletion src/names_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ const color_names = Dict(
"firebrick3" => (205, 38, 38),
"firebrick4" => (139, 26, 26),
"floralwhite" => (255, 250, 240),
"fuchsia" => (255, 0, 255),
"forestgreen" => ( 34, 139, 34),
"fuchsia" => (255, 0, 255),
"gainsboro" => (220, 220, 220),
"ghostwhite" => (248, 248, 255),
"gold" => (255, 215, 0),
Expand Down
18 changes: 9 additions & 9 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,9 @@ function _parse_colorant(desc::String)

ldesc == "transparent" && return RGBA{N0f8}(0,0,0,0)

wo_spaces = replace(ldesc, r"(?<=[^ ]{3}) (?=[^ ]{3})" => "")
wo_spaces = replace(ldesc, r"(?<=[^bfptuv ][^p ][adeghk-rtwy]) (?=[^efinq ][aeh-mo-rtuy][^kw \d][a-y]{0,7}\b)" => "")
c = get(color_names, wo_spaces, nothing)
if c !== nothing
camel = replace(titlecase(ldesc), " " => "")
Base.depwarn(
"""
The X11 color names with spaces are not recommended because they are not allowed in the SVG/CSS.
Use "$camel" or "$wo_spaces" instead.
""", :parse)
if c !== nothing && sizeof(wo_spaces) > 6
return RGB{N0f8}(n0f8(c[1]), n0f8(c[2]), n0f8(c[3]))
end

Expand Down Expand Up @@ -211,7 +205,13 @@ function Base.parse(::Type{C}, desc::AbstractString) where {C<:Colorant}
end

Base.parse(::Type{C}, desc::Symbol) where {C<:Colorant} = parse(C, string(desc))
Base.parse(::Type{C}, c::Colorant) where {C<:Colorant} = c

@noinline function Base.parse(::Type{C}, c::Colorant) where {C<:Colorant}
Base.depwarn("""
`parse(::Type, ::Coloarant)` is deprecated.
Do not call `parse` if the object does not need to be parsed.""", :parse)
c
end

"""
@colorant_str(ex)
Expand Down
10 changes: 4 additions & 6 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ using FixedPointNumbers
# Color parsing
# named-color
redN0f8 = parse(Colorant, "red")
@test colorant"red" == redN0f8
@test isa(redN0f8, RGB{N0f8})
@test redN0f8 == RGB(1,0,0)
@test colorant"red" === redN0f8
@test redN0f8 === RGB{N0f8}(1,0,0)
@test parse(RGB{Float64}, "red") === RGB{Float64}(1,0,0)
@test isa(parse(HSV, "blue"), HSV)
@test_throws ErrorException parse(Colorant, "p ink")
@test parse(Colorant, "transparent") === RGBA{N0f8}(0,0,0,0)
@test parse(Colorant, "\nSeaGreen ") === RGB{N0f8}(r8(0x2E),r8(0x8B),r8(0x57))
seagreen = @test_logs (:warn, r"Use \"SeaGreen\" or \"seagreen\"") parse(Colorant, "sea GREEN")
@test seagreen == colorant"seagreen"
@test parse(Colorant, "sea GREEN") === colorant"seagreen"

# hex-color
@test parse(Colorant, "#D0FF58") === RGB(r8(0xD0),r8(0xFF),r8(0x58))
Expand Down Expand Up @@ -50,6 +48,6 @@ using FixedPointNumbers
@test parse(Colorant, "hsl( 120 50% 7% / 1)") === HSLA{Float32}(120,.5,.07, 1) # CSS Color Module Level 4

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

end

0 comments on commit dd97849

Please sign in to comment.