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

[WIP] Customizable colorrange handling #3688

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [Unreleased]
- Added supported markers hint to unsupported marker warn message.

- `colorrange` can now accept any `Function` which returns a 2-tuple or length-2 vector, as well as an extensible interface for users to integrate their own colorrange objects. [#3688](https://github.com/MakieOrg/Makie.jl/pull/3688)
- Remove StableHashTraits in favor of calculating hashes directly with CRC32c [#3667](https://github.com/MakieOrg/Makie.jl/pull/3667).

## [0.20.8] - 2024-02-22
Expand Down
24 changes: 23 additions & 1 deletion src/colorsampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ colormapping_type(@nospecialize(colormap)) = continuous
colormapping_type(::PlotUtils.CategoricalColorGradient) = banded
colormapping_type(::Categorical) = categorical

"""
evaluate_colorrange(colorrange, color)::Vec2{Float64}

This function must return a `Vec2{Float64}` of the colorrange as `(low, high)`.
Add additional dispatches to this function for your own custom colorrange objects!

The catch-all definition for this function is to convert any provided `colorrange` into
a `Vec2{Float64}`. By default, two more dispatches are defined for `Makie.automatic`
and for `Function`s (but not all callables, since that's not possible to define on).

You can define your method like so:

```julia
Makie.evaluate_colorrange(colorrange::MyCustomType, color) = ...
```

and `color` is always an array of numbers. Be warned that the vector may have only one element.
"""
evaluate_colorrange(colorrange, color) = Vec2{Float64}(colorrange)
evaluate_colorrange(colorrange::Automatic, color) = Vec2{Float64}(Makie.distinct_extrema_nan(color))
evaluate_colorrange(colorrange::Function, color) = Vec2{Float64}(colorrange(color))


function _colormapping(
color_tight::Observable{V},
Expand Down Expand Up @@ -292,7 +314,7 @@ function _colormapping(
end

colorrange = lift(color_tight, colorrange; ignore_equal_values=true) do color, crange
return crange isa Automatic ? Vec2{Float64}(distinct_extrema_nan(color)) : Vec2{Float64}(crange)
return evaluate_colorrange(crange, color)
end

colorrange_scaled = lift(colorrange, colorscale; ignore_equal_values=true) do range, scale
Expand Down