-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnamelookup.jl
72 lines (57 loc) · 1.95 KB
/
namelookup.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Returns list of similarly named colors, from the color_list
"""
function similarly_named_colors(
name::AbstractString,
color_list::AbstractDict{S} = ALL_COLORS) where {S<:AbstractString}
levsort(name, collect(keys(color_list)))
end
struct UnknownColorError{S<:AbstractString} <:Exception
name::S
msg::String
end
function UnknownColorError(
name::AbstractString,
color_list::AbstractDict{S}) where {S<:AbstractString}
similars = similarly_named_colors(name, color_list)
msg=if length(similars)==0
unescape_string("\"$name\" is not a known color name.")
else
suggestions = join(repr.(similars), ", ", " or ")
unescape_string("\"$name\" is not a known color name. Perhaps you meant: $suggestions")
end
UnknownColorError(name, msg)
end
Base.showerror(io::IO, ex::UnknownColorError) = print(io, "UnknownColorError: $(ex.msg)")
"""
Returns a color with the given name.
If not found,a list of suggestions is provided.
"""
function named_color(name::AbstractString, color_list::AbstractDict{S} = ALL_COLORS) where {S<:AbstractString}
if haskey(color_list, name)
color_list[name]
else
throw(UnknownColorError(name, color_list))
end
end
"""
Attempts to find a color to match any name you give it.
First by using the methods from Color.jl's @colorant_str macro,
to handle things like `colorant"#cd32cd"`
Then if that fails, does a lookup in the big list of named colors.
If that fails, then it provides suggestions for what color name you may have meant.
As an user, you basically should just use this, without worrying if a color is defined.
There are almost 3500 defined colors, and if you miss one,
then the suggestions will help you out.
(this is easier than trying to workout the name yourself)
"""
macro colorant_str(name::AbstractString)
local col
try
col = parse(Colorant, name)
catch ex
col = named_color(name)
:($col)
end
:($col)
end