From 9f082ec31d6f6731d793215dc321c3090d8380bd Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 25 May 2022 16:49:26 -0400 Subject: [PATCH 1/7] Respect scale and nonlinear values in PlotUtils cgrads --- src/conversions.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/conversions.jl b/src/conversions.jl index bf0d1a3a395..e83f293d98a 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -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( @@ -1099,11 +1099,14 @@ function to_colormap(cs::Union{String, Symbol})::Vector{RGBAf} end end -to_colormap(cg::PlotUtils.ContinuousColorGradient)::Vector{RGBAf} = to_colormap(cg.colors) +# Handle inbuilt PlotUtils types +function to_colormap(cg::PlotUtils.ContinuousColorGradient)::Vector{RGBAf} + return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), n))) +end function to_colormap(cg::PlotUtils.CategoricalColorGradient)::Vector{RGBAf} - colors = to_colormap(cg.colors) - return repeat(colors; inner=20) + n = length(cg.colors) + return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), n*20))) end """ From 7dea41037fb8b3a37e980c1062713db072357b17 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 25 May 2022 17:35:38 -0400 Subject: [PATCH 2/7] Add test for colormap scales using `lines` --- ReferenceTests/src/tests/examples2d.jl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index 2b0f60e469c..286260badd2 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -466,3 +466,23 @@ end end f end + +@reference_test "nonlinear colormap" begin + n = 100 + colormaps = [:viridis, :magma] + scales = [exp, identity, log, log10] + fig = Figure(resolution = (500, 250)) + ax = Axis(fig[1, 1]) + for (i, cmap) in enumerate(colormaps) + for (j, scale) in enumerate(scales) + cg = Makie.PlotUtils.cgrad(cmap; scale = scale) + x0 = i + y0 = j + lines!(ax, Point2f.(LinRange(x0+0.1, x0+0.9, n), y0); color = 1:n, colormap = cg, linewidth = 10) + end + end + ax.xticks[] = ((1:length(colormaps)) .+ 0.5, string.(colormaps)) + ax.yticks[] = ((1:length(scales)), string.(scales)) + + fig +end From 12f11d0c74cd068a8c230e23e45469e387c33bd3 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 25 May 2022 17:40:46 -0400 Subject: [PATCH 3/7] hardcode n to 30 --- src/conversions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conversions.jl b/src/conversions.jl index e83f293d98a..e55733c41f9 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -1101,7 +1101,7 @@ end # Handle inbuilt PlotUtils types function to_colormap(cg::PlotUtils.ContinuousColorGradient)::Vector{RGBAf} - return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), n))) + return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), 30))) end function to_colormap(cg::PlotUtils.CategoricalColorGradient)::Vector{RGBAf} From 2a6422af590db2f02cf4c65fee3d9eedeae69d4a Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 25 May 2022 22:14:38 -0400 Subject: [PATCH 4/7] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index e65a52a60cc..8c3290926dc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 From 40c9cc612e0be9ae033e99b859fdd4ef13a4cb0f Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Mon, 30 May 2022 17:15:09 +0200 Subject: [PATCH 5/7] clean up implementation and add test for categorical --- ReferenceTests/src/tests/examples2d.jl | 28 ++++++++++++++++++-------- src/conversions.jl | 12 +++++------ src/makielayout/blocks/colorbar.jl | 5 +---- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index 314b49156ed..47064d77a65 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -469,23 +469,35 @@ end @reference_test "nonlinear colormap" begin n = 100 - colormaps = [:viridis, :magma] + categorical = [false, true] scales = [exp, identity, log, log10] fig = Figure(resolution = (500, 250)) - ax = Axis(fig[1, 1]) - for (i, cmap) in enumerate(colormaps) + ax = Axis(fig[1, 1]) + for (i, cat) in enumerate(categorical) for (j, scale) in enumerate(scales) - cg = Makie.PlotUtils.cgrad(cmap; scale = scale) - x0 = i - y0 = j - lines!(ax, Point2f.(LinRange(x0+0.1, x0+0.9, n), y0); color = 1:n, colormap = cg, linewidth = 10) + 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(colormaps)) .+ 0.5, string.(colormaps)) + ax.xticks[] = ((1:length(colormaps)) .+ 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,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 diff --git a/src/conversions.jl b/src/conversions.jl index e55733c41f9..0fd0345a312 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -1100,13 +1100,11 @@ function to_colormap(cs::Union{String, Symbol})::Vector{RGBAf} end # Handle inbuilt PlotUtils types -function to_colormap(cg::PlotUtils.ContinuousColorGradient)::Vector{RGBAf} - return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), 30))) -end - -function to_colormap(cg::PlotUtils.CategoricalColorGradient)::Vector{RGBAf} - n = length(cg.colors) - return to_colormap(getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), n*20))) +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 """ diff --git a/src/makielayout/blocks/colorbar.jl b/src/makielayout/blocks/colorbar.jl index 7171015cde4..bd86b059dad 100644 --- a/src/makielayout/blocks/colorbar.jl +++ b/src/makielayout/blocks/colorbar.jl @@ -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 @@ -181,12 +181,10 @@ 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, @@ -194,7 +192,6 @@ function initialize_block!(cb::Colorbar) inspectable = false ) - # for continous colormaps we sample a 1d image # to avoid white lines when rendering vector graphics From ceccf22112b6c77336de8d8c20a38c91ef092530 Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Mon, 30 May 2022 17:42:51 +0200 Subject: [PATCH 6/7] fix oversight --- ReferenceTests/src/tests/examples2d.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index 47064d77a65..491a17031da 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -483,7 +483,7 @@ 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(colormaps)) .+ 0.5, ["categorical=false", "categorical=true"]) + ax.xticks[] = ((1:length(categorical)) .+ 0.5, ["categorical=false", "categorical=true"]) ax.yticks[] = ((1:length(scales)), string.(scales)) fig end From a3c178915c7c40bcc8e59a5f302f5b30f68a202e Mon Sep 17 00:00:00 2001 From: SimonDanisch Date: Mon, 30 May 2022 18:11:07 +0200 Subject: [PATCH 7/7] sigh, 1.6 --- ReferenceTests/src/tests/examples2d.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index 491a17031da..e154354447f 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -492,7 +492,7 @@ end cmap = cgrad([:black,:white,:orange],[0,0.2,1]) fig = Figure(resolution=(400,200)) ax = Axis(fig[1,1]) - x = range(0,1,50) + 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)