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

make colorrange_scaled computation safe for inverting / sort-order-breaking scales #4884

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added `font` attribute and fixed faulty selection in `scatter`. Scatter fonts can now be themed with `markerfont`. [#4832](https://github.com/MakieOrg/Makie.jl/pull/4832)
- Fixed categorical `cgrad` interpolating at small enough steps [#4858](https://github.com/MakieOrg/Makie.jl/pull/4858)
- Fixed the computed `colorrange` being out of order with `colorscale = -` or similar colorscale functions that break sorting [#4884](https://github.com/MakieOrg/Makie.jl/pull/4884)

## [0.22.2] - 2025-02-26

Expand Down
12 changes: 9 additions & 3 deletions src/colorsampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ function sampler(cmap::Matrix{<: Colorant}, uv::AbstractVector{Vec2f};
return Sampler(cmap, uv, alpha, interpolation, Scaling())
end

"""
apply_scale(scale, x)

Applies the scale function / callable `scale` to each element of `x`.
If `scale` is an Observable then this returns an Observable via `lift`,
otherwise simply returns `broadcast(scale, x)`.
"""
apply_scale(scale::AbstractObservable, x) = lift(apply_scale, scale, x)
apply_scale(::Union{Nothing,typeof(identity)}, x) = x # noop
apply_scale(scale, x) = broadcast(scale, x)
Expand Down Expand Up @@ -166,8 +173,7 @@ function numbers_to_colors(
)::Union{Array{RGBAf, N},RGBAf} where {N}

cmin, cmax = colorrange
scaled_cmin = apply_scale(colorscale, cmin)
scaled_cmax = apply_scale(colorscale, cmax)
scaled_cmin, scaled_cmax = extrema(apply_scale(colorscale, (cmin, cmax)))

return map(numbers) do number
scaled_number = apply_scale(colorscale, Float64(number)) # ints don't work in interpolated_getindex
Expand Down Expand Up @@ -307,7 +313,7 @@ function _colormapping(
end

colorrange_scaled = lift(colorrange, colorscale; ignore_equal_values=true) do range, scale
return Vec2f(apply_scale(scale, range))
return Vec2f(extrema(apply_scale(scale, range)))
end

color_scaled = Observable(el32convert(apply_scale(colorscale[], color_tight[])))
Expand Down
8 changes: 8 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@
@test p.plots[1].plots[1].transformation.model[][3, 4] == 10.0
@test p.plots[1].plots[2].transformation.model[][3, 4] == 10.0
end

@testset "#4883 colorscale that breaks sort" begin
data = Float32.(Makie.peaks())
f, a, p = image(data; colorscale = -)
@test p.calculated_colors[].colorrange_scaled[][1] == -maximum(data)
@test p.calculated_colors[].colorrange_scaled[][2] == -minimum(data)
@test issorted(p.calculated_colors[].colorrange_scaled[])
end
end
Loading