From 1f08f9553ae0da8e65747c7690979b7be7f036c5 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Thu, 22 Dec 2022 17:19:21 +0100 Subject: [PATCH] fix colormap sampling --- CairoMakie/src/utils.jl | 6 +++--- src/colorsampler.jl | 20 ++++++++++---------- src/layouting/transformation.jl | 3 ++- src/makielayout/blocks/colorbar.jl | 9 +-------- src/makielayout/lineaxis.jl | 2 +- 5 files changed, 17 insertions(+), 23 deletions(-) diff --git a/CairoMakie/src/utils.jl b/CairoMakie/src/utils.jl index b3c42104540..186c41fd718 100644 --- a/CairoMakie/src/utils.jl +++ b/CairoMakie/src/utils.jl @@ -123,13 +123,13 @@ end function to_rgba_image(img::AbstractMatrix{<: AbstractFloat}, attributes) Makie.@get_attribute attributes (colormap, colorrange, nan_color, lowclip, highclip) - tr = reduce(∘, Makie.transform_func_obs(attributes)[]) + transform = reduce(∘, Makie.transform_func_obs(attributes)[]) nan_color = Makie.to_color(nan_color) lowclip = isnothing(lowclip) ? lowclip : Makie.to_color(lowclip) highclip = isnothing(highclip) ? highclip : Makie.to_color(highclip) - colorrange = tr.(colorrange) - [get_rgba_pixel(pixel, colormap, colorrange, nan_color, lowclip, highclip) for pixel in tr.(img)] + colorrange = transform.(colorrange) + [get_rgba_pixel(pixel, colormap, colorrange, nan_color, lowclip, highclip) for pixel in transform.(img)] end to_rgba_image(img::AbstractMatrix{<: Colorant}, attributes) = RGBAf.(img) diff --git a/src/colorsampler.jl b/src/colorsampler.jl index 7bca6b4c84a..dad42164713 100644 --- a/src/colorsampler.jl +++ b/src/colorsampler.jl @@ -139,31 +139,31 @@ end function numbers_to_colors(numbers::AbstractArray{<:Number}, primitive) colormap = get_attribute(primitive, :colormap)::Vector{RGBAf} - _colorrange = get_attribute(primitive, :colorrange)::Union{Nothing, Vec2f} - if isnothing(_colorrange) + colorrange = get_attribute(primitive, :colorrange)::Union{Nothing, Vec2f} + transform = reduce(∘, transform_func_obs(primitive)[]) + cmin, cmax = if isnothing(colorrange) # TODO, plot primitive should always expand automatic values - colorrange = Vec2f(extrema_nan(numbers)) + transform.(Vec2f(extrema_nan(numbers))) else - colorrange = _colorrange + transform.(colorrange) end lowclip = get_attribute(primitive, :lowclip) highclip = get_attribute(primitive, :highclip) nan_color = get_attribute(primitive, :nan_color, RGBAf(0,0,0,0)) - cmin, cmax = colorrange::Vec2f - return map(numbers) do number - if isnan(number) + scaled_number = transform(Float64(number)) # ints don't work in interpolated_getindex + if isnan(scaled_number) return nan_color - elseif !isnothing(lowclip) && number < cmin + elseif !isnothing(lowclip) && scaled_number < cmin return lowclip - elseif !isnothing(highclip) && number > cmax + elseif !isnothing(highclip) && scaled_number > cmax return highclip end return interpolated_getindex( colormap, - Float64(number), # ints don't work in interpolated_getindex + scaled_number, (cmin, cmax)) end end diff --git a/src/layouting/transformation.jl b/src/layouting/transformation.jl index 5ba7c581e24..5a22f130f53 100644 --- a/src/layouting/transformation.jl +++ b/src/layouting/transformation.jl @@ -360,7 +360,8 @@ function inv_symlog10(x, low, high) end end -const CONCRETE_INVERSE_SCALES = Union{ +const INVERSABLE_SCALES = Union{ + # typeof(identity), # no, this is a noop typeof(log10), typeof(log), typeof(log2), diff --git a/src/makielayout/blocks/colorbar.jl b/src/makielayout/blocks/colorbar.jl index 0cc13fca563..cc245c7ee1d 100644 --- a/src/makielayout/blocks/colorbar.jl +++ b/src/makielayout/blocks/colorbar.jl @@ -25,7 +25,6 @@ function Colorbar(fig_or_scene, plot::AbstractPlot; kwargs...) error("You should not pass the `$key` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.") end end - Colorbar( fig_or_scene; colormap = plot.colormap, @@ -41,7 +40,6 @@ function Colorbar(fig_or_scene, heatmap::Union{Heatmap, Image}; kwargs...) error("You should not pass the `$key` attribute to the colorbar when constructing it using an existing plot object. This attribute is copied from the plot object, and setting it from the colorbar will make the plot object and the colorbar go out of sync.") end end - Colorbar( fig_or_scene; colormap = heatmap.colormap, @@ -77,13 +75,8 @@ function Colorbar(fig_or_scene, contourf::Union{Contourf, Tricontourf}; kwargs.. end -unscale_limits(lims, _) = lims # noop -function unscale_limits(lims, scale::CONCRETE_INVERSE_SCALES) - inverse_transform(scale).(lims) -end - colorbar_range(start, stop, length, _) = LinRange(start, stop, length) # noop -function colorbar_range(start, stop, length, scale::CONCRETE_INVERSE_SCALES) +function colorbar_range(start, stop, length, scale::INVERSABLE_SCALES) inverse_transform(scale).(range(start, stop; length)) end diff --git a/src/makielayout/lineaxis.jl b/src/makielayout/lineaxis.jl index 4558746d9af..a38e3b9bca7 100644 --- a/src/makielayout/lineaxis.jl +++ b/src/makielayout/lineaxis.jl @@ -721,7 +721,7 @@ function get_minor_tickvalues(i::IntervalsBetween, _, tickvalues, vmin, vmax) end # for log scales, we need to step in log steps at the edges -function get_minor_tickvalues(i::IntervalsBetween, scale::CONCRETE_INVERSE_SCALES, tickvalues, vmin, vmax) +function get_minor_tickvalues(i::IntervalsBetween, scale::INVERSABLE_SCALES, tickvalues, vmin, vmax) vals = Float64[] length(tickvalues) < 2 && return vals n = i.n