Skip to content

Commit

Permalink
Merge pull request #18 from tlienart/master
Browse files Browse the repository at this point in the history
Fix for #17
  • Loading branch information
cormullion authored Nov 30, 2018
2 parents b51c162 + f489eb0 commit 14c96f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/ColorSchemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,33 +249,17 @@ function sortcolorscheme(colorscheme::Vector{C}, field = :l; kwargs...) where {C
end

"""
get(cscheme, x)
Find the nearest color in a colorscheme `cscheme` corresponding to a point `x` between 0 and 1.
get(cscheme, inData, rangescale)
Returns a single color.
"""
function get(cscheme::Vector{C}, x, rangescale) where {C<:Colorant}
if rangescale==:clamp
get(cscheme, x, (0.0, 1.0))
elseif (rangescale==:extrema)
get(cscheme, x, extrema(x))
else
error("rangescale ($rangescale) not supported, should be :clamp, :extrema or tuple (minVal, maxVal)")
end
end

"""
get(cscheme, inData :: Array{Number, 2}, rangescale=:clamp)
get(cscheme, inData :: Array{Number, 2}, rangescale=(minVal, maxVal))
Return an RGB image generated by applying the colorscheme to the 2D input data.
Return an RGB image generated by applying the colorscheme to the `inData`.
If `rangescale` is `:clamp` the colorscheme is applied to values between 0.0-1.0, and values
outside this range get clamped to the ends of the colorscheme.
If `rangescale` is `:clamp` the colorscheme is applied to values between
0.0-1.0, and values outside this range get clamped to the ends of the
colorscheme.
Else, if `rangescale` is `:extrema`, the colorscheme is applied to the range `minimum(indata)..maximum(indata)`.
Else, if `rangescale` is `:extrema`, the colorscheme is applied to the range
`minimum(indata)..maximum(indata)`.
# Examples
Expand All @@ -291,7 +275,17 @@ using PerceptualColourMaps
img4 = get(PerceptualColourMaps.cmap("R1"), rand(10,10))
```
"""
function get(cscheme::Vector{C}, x, rangescale :: Tuple{Number, Number}=(0.0, 1.0)) where {C<:Colorant}
function get(cscheme::Vector{<:Colorant},
x::Union{<:Real, Array{<:Real}, AbstractRange{<:Real}},
rangescale::Union{Symbol, NTuple{2, <:Real}}=(0.0, 1.0))

# NOTE: the Union type for `x` is needed to avoid ambiguity with Base.get
# when using ranges

rangescale == :clamp && (rangescale = (0.0, 1.0))
rangescale == :extrema && (rangescale = extrema(x))
(rangescale isa NTuple{2, Number}) || error("rangescale ($rangescale) not supported, should be :clamp, :extrema or tuple (minVal, maxVal)")
x isa AbstractRange && (x = collect(x))
x = clamp.(x, rangescale...)
before_fp = remap(x, rangescale..., 1, length(cscheme))
before = round.(Int, before_fp, RoundDown)
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ function run_minimum_tests()
# test conversion with manually supplied range
y3=get(ColorSchemes.leonardo, x, (-1.0, 2.0))
@test y3 == y2

# test with steplen (#17)
r = range(0, stop=5, length=10)
y = get(ColorSchemes.leonardo, r)
y2 = get(ColorSchemes.leonardo, collect(r))
@test y == y2

# test for specific value
val = 0.2
y = get(ColorSchemes.leonardo, [val])
y2 = get(ColorSchemes.leonardo, val)
@test y2 == y[1]
end

if get(ENV, "COLORSCHEMES_KEEP_TEST_RESULTS", false) == "true"
Expand Down

0 comments on commit 14c96f9

Please sign in to comment.