Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect scale and nonlinear values in PlotUtils cgrads #1979

Merged
merged 9 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Switched to `MathTeXEngine v0.4`, which improves the look of LaTeXStrings [#1952](https://github.com/JuliaPlots/Makie.jl/pull/1952).
- Added subtitle capability to `Axis` [#1859](https://github.com/JuliaPlots/Makie.jl/pull/1859).
- Fixed a bug where scaled colormaps constructed using `Makie.cgrad` were not interpreted correctly.

## v0.17.2

Expand Down
31 changes: 31 additions & 0 deletions ReferenceTests/src/tests/examples2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,37 @@ end
f
end

@reference_test "nonlinear colormap" begin
n = 100
categorical = [false, true]
scales = [exp, identity, log, log10]
fig = Figure(resolution = (500, 250))
ax = Axis(fig[1, 1])
for (i, cat) in enumerate(categorical)
for (j, scale) in enumerate(scales)
cg = if cat
cgrad(:viridis, 5; scale = scale, categorical=true)
else
cgrad(:viridis; scale = scale, categorical=nothing)
end
lines!(ax, Point2f.(LinRange(i+0.1, i+0.9, n), j); color = 1:n, colormap = cg, linewidth = 10)
end
end
ax.xticks[] = ((1:length(categorical)) .+ 0.5, ["categorical=false", "categorical=true"])
ax.yticks[] = ((1:length(scales)), string.(scales))
fig
end

@reference_test "colormap with specific values" begin
cmap = cgrad([:black,:white,:orange],[0,0.2,1])
fig = Figure(resolution=(400,200))
ax = Axis(fig[1,1])
x = range(0,1,length=50)
scatter!(fig[1,1],Point2.(x,fill(0.,50)),color=x,colormap=cmap)
hidedecorations!(ax)
Colorbar(fig[2,1],vertical=false,colormap=cmap)
fig
end

@reference_test "multi rect with poly" begin
# use thick strokewidth, so it will make tests fail if something is missing
Expand Down
13 changes: 7 additions & 6 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ function to_colormap(cs::Union{String, Symbol})::Vector{RGBAf}
return to_colormap(ColorBrewer.palette(cs_string, 8))
else
# cs_string must be in plotutils_names
return to_colormap(PlotUtils.get_colorscheme(Symbol(cs_string)).colors)
return to_colormap(PlotUtils.get_colorscheme(Symbol(cs_string)))
end
else
error(
Expand All @@ -1099,11 +1099,12 @@ function to_colormap(cs::Union{String, Symbol})::Vector{RGBAf}
end
end

to_colormap(cg::PlotUtils.ContinuousColorGradient)::Vector{RGBAf} = to_colormap(cg.colors)

function to_colormap(cg::PlotUtils.CategoricalColorGradient)::Vector{RGBAf}
colors = to_colormap(cg.colors)
return repeat(colors; inner=20)
# Handle inbuilt PlotUtils types
function to_colormap(cg::PlotUtils.ColorGradient)::Vector{RGBAf}
# We sample the colormap using cg[val]. This way, we get a concrete representation of
# the underlying gradient, like it being categorical or using a log scale.
# 256 is just a high enough constant, without being too big to slow things down.
return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), 256)))
end

"""
Expand Down
5 changes: 1 addition & 4 deletions src/makielayout/blocks/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function block_docs(::Type{Colorbar})
chosen according to the colorrange.

You can set colorrange and colormap manually, or pass a plot object as the second argument
to copy its respective attributes.
to copy its respective attributes.

## Constructors

Expand Down Expand Up @@ -181,20 +181,17 @@ function initialize_block!(cb::Colorbar)
end

colors = get.(Ref(gradient), (steps[1:end-1] .+ steps[2:end]) ./2)

rects, colors
end

colors = lift(x -> getindex(x, 2), rects_and_colors)

rects = poly!(blockscene,
lift(x -> getindex(x, 1), rects_and_colors),
color = colors,
visible = map_is_categorical,
inspectable = false
)


# for continous colormaps we sample a 1d image
# to avoid white lines when rendering vector graphics

Expand Down