Skip to content

Commit 07cca0a

Browse files
authored
Access color schemes through symbols (#3)
1 parent 8fdfae8 commit 07cca0a

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/TextHeatmaps.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module TextHeatmaps
33
using Crayons: Crayon
44
using FixedPointNumbers: N0f8
55
using Colors: Colorant, RGB, hex
6-
using ColorSchemes: ColorScheme, get, seismic
6+
using ColorSchemes: ColorScheme, colorschemes, get, seismic
77

88
include("heatmap.jl")
99

src/heatmap.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Create a heatmap of words where the background color of each word is determined
88
Arguments `values` and `words` (and optionally `colors`) must have the same size.
99
1010
## Keyword arguments
11-
- `colorscheme::ColorScheme`: color scheme from ColorSchemes.jl.
11+
- `colorscheme::Union{ColorScheme,Symbol}`: color scheme from ColorSchemes.jl.
1212
Defaults to `ColorSchemes.seismic`.
1313
- `rangescale::Symbol`: selects how the color channel reduced heatmap is normalized
1414
before the color scheme is applied. Can be either `:extrema` or `:centered`.
@@ -39,15 +39,19 @@ struct TextHeatmap{
3939
end
4040

4141
function TextHeatmap(
42-
val, words; colorscheme::ColorScheme=DEFAULT_COLORSCHEME, rangescale=DEFAULT_RANGESCALE
42+
val, words; colorscheme::Union{ColorScheme,Symbol}=DEFAULT_COLORSCHEME, rangescale=DEFAULT_RANGESCALE
4343
)
4444
if size(val) != size(words)
4545
throw(ArgumentError("Sizes of values and words don't match"))
4646
end
47+
colorscheme = get_colorscheme(colorscheme)
4748
colors = get(colorscheme, val, rangescale)
4849
return TextHeatmap(val, words, colors)
4950
end
5051

52+
get_colorscheme(c::ColorScheme) = c
53+
get_colorscheme(s::Symbol)::ColorScheme = colorschemes[s]
54+
5155
#==================#
5256
# Show in terminal #
5357
#==================#

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ using Aqua
3939
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:extrema)
4040
@test_reference "references/inferno_extrema.txt" repr("text/plain", h)
4141

42+
# Test colorscheme symbols
43+
colorscheme = :inferno
44+
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:centered)
45+
@test_reference "references/inferno_centered.txt" repr("text/plain", h)
46+
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:extrema)
47+
@test_reference "references/inferno_extrema.txt" repr("text/plain", h)
48+
4249
# Test errors
4350
@test_throws ArgumentError heatmap(val, ["Test", "Text", "Heatmaps"])
4451
# Test inner constructor

0 commit comments

Comments
 (0)