Support for non-gradient colors in surface-like plots (useful for contour)

This commit is contained in:
yharel 2019-03-28 02:28:19 +02:00
parent dd02a69e8f
commit 78059bf825
5 changed files with 21 additions and 16 deletions

View File

@ -1510,19 +1510,19 @@ function has_black_border_for_default(st::Symbol)
like_histogram(st) || st in (:hexbin, :bar, :shape) like_histogram(st) || st in (:hexbin, :bar, :shape)
end end
# converts a symbol or string into a Colorant or ColorGradient
# converts a symbol or string into a colorant (Colors.RGB), and assigns a color automatically # and assigns a color automatically
function getSeriesRGBColor(c, sp::Subplot, n::Int) function get_series_color(c, sp::Subplot, n::Int, seriestype)
if c == :auto if c == :auto
c = autopick(sp[:color_palette], n) c = like_surface(seriestype) ? cgrad() : autopick(sp[:color_palette], n)
elseif isa(c, Int) elseif isa(c, Int)
c = autopick(sp[:color_palette], c) c = autopick(sp[:color_palette], c)
end end
plot_color(c) plot_color(c)
end end
function getSeriesRGBColor(c::AbstractArray, sp::Subplot, n::Int) function get_series_color(c::AbstractArray, sp::Subplot, n::Int, seriestype)
map(x->getSeriesRGBColor(x, sp, n), c) map(x->get_series_color(x, sp, n), c, seriestype)
end end
function ensure_gradient!(plotattributes::KW, csym::Symbol, asym::Symbol) function ensure_gradient!(plotattributes::KW, csym::Symbol, asym::Symbol)
@ -1567,21 +1567,23 @@ function _update_series_attributes!(plotattributes::KW, plt::Plot, sp::Subplot)
end end
# update series color # update series color
plotattributes[:seriescolor] = getSeriesRGBColor(plotattributes[:seriescolor], sp, plotIndex) scolor = plotattributes[:seriescolor]
stype = plotattributes[:seriestype]
plotattributes[:seriescolor] = scolor = get_series_color(scolor, sp, plotIndex, stype)
# update other colors # update other colors
for s in (:line, :marker, :fill) for s in (:line, :marker, :fill)
csym, asym = Symbol(s,:color), Symbol(s,:alpha) csym, asym = Symbol(s,:color), Symbol(s,:alpha)
plotattributes[csym] = if plotattributes[csym] == :auto plotattributes[csym] = if plotattributes[csym] == :auto
plot_color(if has_black_border_for_default(plotattributes[:seriestype]) && s == :line plot_color(if has_black_border_for_default(stype) && s == :line
sp[:foreground_color_subplot] sp[:foreground_color_subplot]
else else
plotattributes[:seriescolor] scolor
end) end)
elseif plotattributes[csym] == :match elseif plotattributes[csym] == :match
plot_color(plotattributes[:seriescolor]) plot_color(scolor)
else else
getSeriesRGBColor(plotattributes[csym], sp, plotIndex) get_series_color(plotattributes[csym], sp, plotIndex, stype)
end end
end end
@ -1589,9 +1591,9 @@ function _update_series_attributes!(plotattributes::KW, plt::Plot, sp::Subplot)
plotattributes[:markerstrokecolor] = if plotattributes[:markerstrokecolor] == :match plotattributes[:markerstrokecolor] = if plotattributes[:markerstrokecolor] == :match
plot_color(sp[:foreground_color_subplot]) plot_color(sp[:foreground_color_subplot])
elseif plotattributes[:markerstrokecolor] == :auto elseif plotattributes[:markerstrokecolor] == :auto
getSeriesRGBColor(plotattributes[:markercolor], sp, plotIndex) get_series_color(plotattributes[:markercolor], sp, plotIndex, stype)
else else
getSeriesRGBColor(plotattributes[:markerstrokecolor], sp, plotIndex) get_series_color(plotattributes[:markerstrokecolor], sp, plotIndex, stype)
end end
# if marker_z, fill_z or line_z are set, ensure we have a gradient # if marker_z, fill_z or line_z are set, ensure we have a gradient

View File

@ -548,7 +548,7 @@ end
const _gr_gradient_alpha = ones(256) const _gr_gradient_alpha = ones(256)
function gr_set_gradient(c) function gr_set_gradient(c)
grad = c isa ColorGradient ? c : cgrad() grad = _as_gradient(c)
for (i,z) in enumerate(range(0, stop=1, length=256)) for (i,z) in enumerate(range(0, stop=1, length=256))
c = grad[z] c = grad[z]
GR.setcolorrep(999+i, red(c), green(c), blue(c)) GR.setcolorrep(999+i, red(c), green(c), blue(c))

View File

@ -354,7 +354,7 @@ end
function plotly_colorscale(grad::ColorGradient, α) function plotly_colorscale(grad::ColorGradient, α)
[[grad.values[i], rgba_string(plot_color(grad.colors[i], α))] for i in 1:length(grad.colors)] [[grad.values[i], rgba_string(plot_color(grad.colors[i], α))] for i in 1:length(grad.colors)]
end end
plotly_colorscale(c, α) = plotly_colorscale(cgrad(alpha=α), α) plotly_colorscale(c::Colorant,α) = plotly_colorscale(_as_gradient(c),α)
function plotly_colorscale(c::AbstractVector{<:RGBA}, α) function plotly_colorscale(c::AbstractVector{<:RGBA}, α)
if length(c) == 1 if length(c) == 1
return [[0.0, rgba_string(plot_color(c[1], α))], [1.0, rgba_string(plot_color(c[1], α))]] return [[0.0, rgba_string(plot_color(c[1], α))], [1.0, rgba_string(plot_color(c[1], α))]]

View File

@ -73,7 +73,7 @@ function py_colormap(grad::ColorGradient)
cm."set_bad"(color=(0,0,0,0.0), alpha=0.0) cm."set_bad"(color=(0,0,0,0.0), alpha=0.0)
cm cm
end end
py_colormap(c) = py_colormap(cgrad()) py_colormap(c::Colorant) = py_colormap(_as_gradient(c))
function py_shading(c, z) function py_shading(c, z)

View File

@ -273,6 +273,9 @@ _cycle(v, indices::AVec{Int}) = fill(v, length(indices))
_cycle(grad::ColorGradient, idx::Int) = _cycle(grad.colors, idx) _cycle(grad::ColorGradient, idx::Int) = _cycle(grad.colors, idx)
_cycle(grad::ColorGradient, indices::AVec{Int}) = _cycle(grad.colors, indices) _cycle(grad::ColorGradient, indices::AVec{Int}) = _cycle(grad.colors, indices)
_as_gradient(grad::ColorGradient) = grad
_as_gradient(c::Colorant) = ColorGradient([c,c])
makevec(v::AVec) = v makevec(v::AVec) = v
makevec(v::T) where {T} = T[v] makevec(v::T) where {T} = T[v]