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

prevent potential type instabilities #2511

Merged
merged 1 commit into from
Jan 1, 2023
Merged
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
26 changes: 9 additions & 17 deletions src/colorsampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ You can use `norm`, to change the range of 0..1 to whatever you want.
function interpolated_getindex(cmap::AbstractArray, value::Number, norm::VecTypes)
cmin, cmax = norm
cmin == cmax && error("Can't interpolate in a range where cmin == cmax. This can happen, for example, if a colorrange is set automatically but there's only one unique value present.")
i01 = clamp((value - cmin) / (cmax - cmin), 0.0, 1.0)
i01 = clamp((value - cmin) / (cmax - cmin), zero(value), one(value))
return interpolated_getindex(cmap, i01)
end

Expand All @@ -44,16 +44,14 @@ end
Like getindex, but accepts values between 0..1 for `value` and interpolates those to the full range of `cmap`.
"""
function interpolated_getindex(cmap::AbstractArray{T}, i01::AbstractFloat) where T
if !isfinite(i01)
error("Looking up a non-finite or NaN value in a colormap is undefined.")
end
isfinite(i01) || error("Looking up a non-finite or NaN value in a colormap is undefined.")
i1len = (i01 * (length(cmap) - 1)) + 1
down = floor(Int, i1len)
up = ceil(Int, i1len)
down == up && return cmap[down]
interp_val = i1len - down
downc, upc = cmap[down], cmap[up]
return convert(T, (downc * (1.0 - interp_val)) + (upc * interp_val))
return convert(T, downc * (one(interp_val) - interp_val) + upc * interp_val)
end

function nearest_getindex(cmap::AbstractArray, value::AbstractFloat)
Expand All @@ -72,16 +70,10 @@ end

function apply_scaling(value::Number, scaling::Scaling)::Float64
value_scaled = scaling.scaling_function(value)
if scaling.range === nothing
return value_scaled
end
scaling.range === nothing && return value_scaled
cmin, cmax = scaling.range
clamped = clamp((value_scaled - cmin) / (cmax - cmin), 0.0, 1.0)
if isfinite(clamped)
return clamped
else
return 0.0
end
clamped = clamp((value_scaled - cmin) / (cmax - cmin), zero(value), one(value))
return isfinite(clamped) ? clamped : zero(clamped)
end

function Base.getindex(sampler::Sampler, i)::RGBAf
Expand Down Expand Up @@ -140,11 +132,11 @@ 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 = if isnothing(_colorrange)
# TODO, plot primitive should always expand automatic values
colorrange = Vec2f(extrema_nan(numbers))
Vec2f(extrema_nan(numbers))
else
colorrange = _colorrange
_colorrange
end

lowclip = get_attribute(primitive, :lowclip)
Expand Down