Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
fix color resampling (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch authored Mar 2, 2020
1 parent dcd1513 commit 07137d0
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/utilities/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ You can use `norm`, to change the range of 0..1 to whatever you want.
"""
function interpolated_getindex(cmap::AbstractArray{T}, value::AbstractFloat, norm = (0.0, 1.0)) where T
cmin, cmax = norm
if cmin == cmax
return cmap[1]
end
i01 = clamp((value - cmin) / (cmax - cmin), 0.0, 1.0)
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]
convert(T, (downc * (1.0 - interp_val)) + (upc * interp_val))
return convert(T, (downc * (1.0 - interp_val)) + (upc * interp_val))
end

function to_image(image::AbstractMatrix{<: AbstractFloat}, colormap::AbstractVector{<: Colorant}, colorrange)
interpolated_getindex.((to_value(colormap),), image, (to_value(colorrange),))
return interpolated_getindex.((to_value(colormap),), image, (to_value(colorrange),))
end

"""
Expand All @@ -27,7 +30,7 @@ Resample a vector with linear interpolation to have length `len`
"""
function resample(A::AbstractVector, len::Integer)
length(A) == len && return A
interpolated_getindex.((A,), range(0.0, stop=1.0, length=len))
return interpolated_getindex.((A,), range(0.0, stop=1.0, length=len))
end

"""
Expand Down

0 comments on commit 07137d0

Please sign in to comment.