Skip to content

Commit

Permalink
fix colormap sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 22, 2022
1 parent 37d23e4 commit 1f08f95
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
6 changes: 3 additions & 3 deletions CairoMakie/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions src/colorsampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/layouting/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 1 addition & 8 deletions src/makielayout/blocks/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/makielayout/lineaxis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f08f95

Please sign in to comment.