Skip to content

Commit

Permalink
UnicodePlots: allow passing arbitrary colors (RGBA, symbols, integers…
Browse files Browse the repository at this point in the history
…, ...) (JuliaPlots#3758)
  • Loading branch information
t-bltg authored Aug 19, 2021
1 parent f85f668 commit c67da7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Requires = "1"
Scratch = "1"
Showoff = "0.3.1, 1.0"
StatsBase = "0.32, 0.33"
UnicodePlots = "2"
julia = "1.5"

[extras]
Expand Down
40 changes: 17 additions & 23 deletions src/backends/unicodeplots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ warn_on_unsupported_args(::UnicodePlotsBackend, plotattributes::KW) = nothing

# --------------------------------------------------------------------------------------

function _canvas_map()
(
braille = UnicodePlots.BrailleCanvas,
ascii = UnicodePlots.AsciiCanvas,
block = UnicodePlots.BlockCanvas,
dot = UnicodePlots.DotCanvas,
density = UnicodePlots.DensityCanvas,
)
end
_canvas_map() = (
braille = UnicodePlots.BrailleCanvas,
ascii = UnicodePlots.AsciiCanvas,
block = UnicodePlots.BlockCanvas,
dot = UnicodePlots.DotCanvas,
density = UnicodePlots.DensityCanvas,
)

# do all the magic here... build it all at once, since we need to know about all the series at the very beginning
function rebuildUnicodePlot!(plt::Plot, width, height)
Expand Down Expand Up @@ -93,14 +91,7 @@ function rebuildUnicodePlot!(plt::Plot, width, height)
end

# add a single series
function addUnicodeSeries!(
o,
plotattributes,
addlegend::Bool,
xlim,
ylim;
rev_color_names = Dict(v => k for (k, v) in Colors.color_names),
)
function addUnicodeSeries!(o, plotattributes, addlegend::Bool, xlim, ylim)
# get the function, or special handling for step/bar/hist
st = plotattributes[:seriestype]
if st == :histogram2d
Expand Down Expand Up @@ -130,12 +121,15 @@ function addUnicodeSeries!(
end
label = addlegend ? plotattributes[:label] : ""

# if we happen to pass in allowed color symbols, great... otherwise let UnicodePlots decide
lc = convert(ARGB32, plotattributes[:linecolor])
sym = Symbol(
get(rev_color_names, map(Int, (red(lc).i, green(lc).i, blue(lc).i)), nothing),
)
color = sym in UnicodePlots.color_cycle ? sym : :auto
lc = plotattributes[:linecolor]
if typeof(lc) <: UnicodePlots.UserColorType
color = lc
elseif lc isa RGBA{Float64}
lc = convert(ARGB32, lc)
color = map(Int, (red(lc).i, green(lc).i, blue(lc).i))
else
color = :auto
end

# add the series
x, y = RecipesPipeline.unzip(
Expand Down

0 comments on commit c67da7d

Please sign in to comment.