Skip to content

Commit 256931e

Browse files
committed
More consistent legacy color conversion
When a given "legacy color" argument is not in fact a recognised color name, `legacy_color` should return nil, and indeed it does when the argument is a string. However, the color symbol method will just return the symbol argument if it is not recognised, creating an inconsistency in the behaviour. Instead, it should return nothing, as the string case does. This change is made, and a little of the surrounding code rearranged for clarity.
1 parent 26b5d2f commit 256931e

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/legacy.jl

+27-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ module Legacy
88

99
using ..StyledStrings: SimpleColor, Face, loadface!, face!
1010

11+
"""
12+
legacy_color(color::Union{String, Symbol, Int})
13+
14+
Attempt to obtain a `SimpleColor` for a "legacy" color value `color`.
15+
16+
When this is not possible, `nothing` is returned.
17+
"""
18+
function legacy_color end
19+
1120
"""
1221
A mapping from 256-color codes indicies to 8-bit colours.
1322
"""
@@ -51,6 +60,19 @@ const ANSI_256_COLORS =
5160
0x8a8a8a, 0x949494, 0x9e9e9e, 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6,
5261
0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee])
5362

63+
legacy_color(color256::Int) = get(ANSI_256_COLORS, color256+1, nothing)
64+
65+
"""
66+
A list of all named colors recognised, including both the old `light_*` and new
67+
`bright_*` named colors.
68+
"""
69+
const NAMED_COLORS =
70+
(:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white,
71+
:bright_black, :grey, :gray, :bright_red, :bright_green, :bright_yellow,
72+
:bright_blue, :bright_magenta, :bright_cyan, :bright_white, :light_black,
73+
:light_red, :light_green, :light_yellow, :light_blue, :light_magenta,
74+
:light_cyan, :light_white)
75+
5476
"""
5577
A mapping from old named colours to the new names, specifically from `light_*`
5678
to `bright_*`.
@@ -65,24 +87,13 @@ const RENAMED_COLORS = Dict{Symbol, Symbol}(
6587
:light_cyan => :bright_cyan,
6688
:light_white => :bright_white)
6789

68-
legacy_color(color::Symbol) = SimpleColor(get(RENAMED_COLORS, color, color))
69-
legacy_color(color256::Int) = get(ANSI_256_COLORS, color256+1, nothing)
70-
71-
"""
72-
legacy_color(color::Union{String, Symbol, Int})
73-
74-
Attempt to obtain a `SimpleColor` for a "legacy" color value `color`.
90+
legacy_color(color::Symbol) =
91+
if color in NAMED_COLORS
92+
SimpleColor(get(RENAMED_COLORS, color, color))
93+
end
7594

76-
When this is not possible, `nothing` is returned.
77-
"""
7895
function legacy_color(color::String)
79-
namedcolours = ("black", "red", "green", "yellow", "blue", "magenta",
80-
"cyan", "white", "bright_black", "grey", "gray",
81-
"bright_red", "bright_green", "bright_yellow",
82-
"bright_blue", "bright_magenta", "bright_cyan",
83-
"bright_white", "light_black", "light_red", "light_green",
84-
"light_yellow", "light_blue", "light_magenta", "light_cyan",
85-
"light_white")
96+
namedcolours = String.(namedcolours)
8697
if color in namedcolours
8798
legacy_color(Symbol(color))
8899
elseif 0 <= (color256 = something(tryparse(Int, color), -1)) <= 255

0 commit comments

Comments
 (0)