integrate colorpalettes in pgfplotsx

This commit is contained in:
Daniel Schwabeneder 2020-04-12 00:01:15 +02:00
parent becfa33ee8
commit f1e7e3a4de
4 changed files with 35 additions and 30 deletions

View File

@ -534,7 +534,7 @@ function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims)
GR.setwindow(xmin, xmax, zmin, zmax) GR.setwindow(xmin, xmax, zmin, zmax)
if !isempty(cbar.gradients) if !isempty(cbar.gradients)
series = cbar.gradients series = cbar.gradients
gr_set_gradient(_cbar_unique(gr_get_color.(series),"color")) gr_set_gradient(_cbar_unique(get_colorgradient.(series),"color"))
gr_set_transparency(_cbar_unique(get_fillalpha.(series), "fill alpha")) gr_set_transparency(_cbar_unique(get_fillalpha.(series), "fill alpha"))
GR.cellarray(xmin, xmax, zmax, zmin, 1, 256, 1000:1255) GR.cellarray(xmin, xmax, zmax, zmin, 1, 256, 1000:1255)
end end
@ -542,7 +542,7 @@ function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims)
if !isempty(cbar.fills) if !isempty(cbar.fills)
series = cbar.fills series = cbar.fills
GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillintstyle(GR.INTSTYLE_SOLID)
gr_set_gradient(_cbar_unique(gr_get_color.(series), "color")) gr_set_gradient(_cbar_unique(get_colorgradient.(series), "color"))
gr_set_transparency(_cbar_unique(get_fillalpha.(series), "fill alpha")) gr_set_transparency(_cbar_unique(get_fillalpha.(series), "fill alpha"))
levels = _cbar_unique(contour_levels.(series, Ref(clims)), "levels") levels = _cbar_unique(contour_levels.(series, Ref(clims)), "levels")
# GR implicitly uses the maximal z value as the highest level # GR implicitly uses the maximal z value as the highest level
@ -561,7 +561,7 @@ function gr_draw_colorbar(cbar::GRColorbar, sp::Subplot, clims)
if !isempty(cbar.lines) if !isempty(cbar.lines)
series = cbar.lines series = cbar.lines
gr_set_gradient(_cbar_unique(gr_get_color.(series),"color")) gr_set_gradient(_cbar_unique(get_colorgradient.(series),"color"))
gr_set_line(_cbar_unique(get_linewidth.(series), "line width"), gr_set_line(_cbar_unique(get_linewidth.(series), "line width"),
_cbar_unique(get_linestyle.(series), "line style"), _cbar_unique(get_linestyle.(series), "line style"),
_cbar_unique(get_linecolor.(series, Ref(clims)), "line color")) _cbar_unique(get_linecolor.(series, Ref(clims)), "line color"))
@ -657,25 +657,10 @@ function gr_set_gradient(c)
end end
function gr_set_gradient(series::Series) function gr_set_gradient(series::Series)
color = gr_get_color(series) color = get_colorgradient(series)
color !== nothing && gr_set_gradient(color) color !== nothing && gr_set_gradient(color)
end end
function gr_get_color(series::Series)
st = series[:seriestype]
if st in (:surface, :heatmap) || isfilledcontour(series)
series[:fillcolor]
elseif st in (:contour, :wireframe)
series[:linecolor]
elseif series[:marker_z] !== nothing
series[:markercolor]
elseif series[:line_z] !== nothing
series[:linecolor]
elseif series[:fill_z] !== nothing
series[:fillcolor]
end
end
# this is our new display func... set up the viewport_canvas, compute bounding boxes, and display each subplot # this is our new display func... set up the viewport_canvas, compute bounding boxes, and display each subplot
function gr_display(plt::Plot, fmt="") function gr_display(plt::Plot, fmt="")
GR.clearws() GR.clearws()

View File

@ -200,12 +200,13 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
# As it is likely that all series within the same axis use the same # As it is likely that all series within the same axis use the same
# colormap this should not cause any problem. # colormap this should not cause any problem.
for series in series_list(sp) for series in series_list(sp)
for col in (:markercolor, :fillcolor, :linecolor) if hascolorbar(series)
if typeof(series.plotattributes[col]) == ColorGradient cg = get_colorgradient(series)
cm = pgfx_colormap(get_colorgradient(series))
PGFPlotsX.push_preamble!( PGFPlotsX.push_preamble!(
pgfx_plot.the_plot, pgfx_plot.the_plot,
"""\\pgfplotsset{ """\\pgfplotsset{
colormap={plots$(sp.attr[:subplot_index])}{$(pgfx_colormap(series.plotattributes[col]))}, colormap={plots$(sp.attr[:subplot_index])}{$cm},
}""", }""",
) )
push!( push!(
@ -213,10 +214,16 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
"colorbar" => nothing, "colorbar" => nothing,
"colormap name" => "plots$(sp.attr[:subplot_index])", "colormap name" => "plots$(sp.attr[:subplot_index])",
) )
if cg isa ColorPalette
push!(
axis_opt,
"colormap access" => "piecewise const",
"colorbar sampled" => nothing,
)
end
# goto is needed to break out of col and series for # goto is needed to break out of col and series for
@goto colorbar_end @goto colorbar_end
end end
end
end end
@label colorbar_end @label colorbar_end
@ -677,13 +684,9 @@ function pgfx_filllegend!(series_opt, opt)
}""") }""")
end end
function pgfx_colormap(grad::Union{ColorGradient, ColorPalette}) pgfx_colormap(cl::PlotUtils.AbstractColorList) = pgfx_colormap(color_list(cl))
join(map(color_list(grad)) do c function pgfx_colormap(v::Vector{<:Colorant})
@sprintf("rgb=(%.8f,%.8f,%.8f)", red(c), green(c), blue(c)) join(map(v) do c
end, "\n")
end
function pgfx_colormap(grad::Vector{<:Colorant})
join(map(grad) do c
@sprintf("rgb=(%.8f,%.8f,%.8f)", red(c), green(c), blue(c)) @sprintf("rgb=(%.8f,%.8f,%.8f)", red(c), green(c), blue(c))
end, "\n") end, "\n")
end end

View File

@ -1000,6 +1000,8 @@ _backend_skips = Dict(
16, # pgfplots thinks the upper panel is too small 16, # pgfplots thinks the upper panel is too small
22, # contourf 22, # contourf
23, # pie 23, # pie
25, # @df
30, # @df
31, # animation 31, # animation
32, # spy 32, # spy
38, # histogram2d 38, # histogram2d

View File

@ -541,6 +541,21 @@ for comp in (:line, :fill, :marker)
end end
end end
function get_colorgradient(series::Series)
st = series[:seriestype]
if st in (:surface, :heatmap) || isfilledcontour(series)
series[:fillcolor]
elseif st in (:contour, :wireframe)
series[:linecolor]
elseif series[:marker_z] !== nothing
series[:markercolor]
elseif series[:line_z] !== nothing
series[:linecolor]
elseif series[:fill_z] !== nothing
series[:fillcolor]
end
end
single_color(c, v = 0.5) = c single_color(c, v = 0.5) = c
single_color(grad::ColorGradient, v = 0.5) = grad[v] single_color(grad::ColorGradient, v = 0.5) = grad[v]