Skip to content

Commit 14c96f9

Browse files
authored
Merge pull request #18 from tlienart/master
Fix for #17
2 parents b51c162 + f489eb0 commit 14c96f9

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/ColorSchemes.jl

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -249,33 +249,17 @@ function sortcolorscheme(colorscheme::Vector{C}, field = :l; kwargs...) where {C
249249
end
250250

251251
"""
252-
get(cscheme, x)
253252
254-
Find the nearest color in a colorscheme `cscheme` corresponding to a point `x` between 0 and 1.
253+
get(cscheme, inData, rangescale)
255254
256-
Returns a single color.
257-
"""
258-
function get(cscheme::Vector{C}, x, rangescale) where {C<:Colorant}
259-
if rangescale==:clamp
260-
get(cscheme, x, (0.0, 1.0))
261-
elseif (rangescale==:extrema)
262-
get(cscheme, x, extrema(x))
263-
else
264-
error("rangescale ($rangescale) not supported, should be :clamp, :extrema or tuple (minVal, maxVal)")
265-
end
266-
end
267-
268-
"""
269-
270-
get(cscheme, inData :: Array{Number, 2}, rangescale=:clamp)
271-
get(cscheme, inData :: Array{Number, 2}, rangescale=(minVal, maxVal))
272-
273-
Return an RGB image generated by applying the colorscheme to the 2D input data.
255+
Return an RGB image generated by applying the colorscheme to the `inData`.
274256
275-
If `rangescale` is `:clamp` the colorscheme is applied to values between 0.0-1.0, and values
276-
outside this range get clamped to the ends of the colorscheme.
257+
If `rangescale` is `:clamp` the colorscheme is applied to values between
258+
0.0-1.0, and values outside this range get clamped to the ends of the
259+
colorscheme.
277260
278-
Else, if `rangescale` is `:extrema`, the colorscheme is applied to the range `minimum(indata)..maximum(indata)`.
261+
Else, if `rangescale` is `:extrema`, the colorscheme is applied to the range
262+
`minimum(indata)..maximum(indata)`.
279263
280264
# Examples
281265
@@ -291,7 +275,17 @@ using PerceptualColourMaps
291275
img4 = get(PerceptualColourMaps.cmap("R1"), rand(10,10))
292276
```
293277
"""
294-
function get(cscheme::Vector{C}, x, rangescale :: Tuple{Number, Number}=(0.0, 1.0)) where {C<:Colorant}
278+
function get(cscheme::Vector{<:Colorant},
279+
x::Union{<:Real, Array{<:Real}, AbstractRange{<:Real}},
280+
rangescale::Union{Symbol, NTuple{2, <:Real}}=(0.0, 1.0))
281+
282+
# NOTE: the Union type for `x` is needed to avoid ambiguity with Base.get
283+
# when using ranges
284+
285+
rangescale == :clamp && (rangescale = (0.0, 1.0))
286+
rangescale == :extrema && (rangescale = extrema(x))
287+
(rangescale isa NTuple{2, Number}) || error("rangescale ($rangescale) not supported, should be :clamp, :extrema or tuple (minVal, maxVal)")
288+
x isa AbstractRange && (x = collect(x))
295289
x = clamp.(x, rangescale...)
296290
before_fp = remap(x, rangescale..., 1, length(cscheme))
297291
before = round.(Int, before_fp, RoundDown)

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ function run_minimum_tests()
128128
# test conversion with manually supplied range
129129
y3=get(ColorSchemes.leonardo, x, (-1.0, 2.0))
130130
@test y3 == y2
131+
132+
# test with steplen (#17)
133+
r = range(0, stop=5, length=10)
134+
y = get(ColorSchemes.leonardo, r)
135+
y2 = get(ColorSchemes.leonardo, collect(r))
136+
@test y == y2
137+
138+
# test for specific value
139+
val = 0.2
140+
y = get(ColorSchemes.leonardo, [val])
141+
y2 = get(ColorSchemes.leonardo, val)
142+
@test y2 == y[1]
131143
end
132144

133145
if get(ENV, "COLORSCHEMES_KEEP_TEST_RESULTS", false) == "true"

0 commit comments

Comments
 (0)