From 602bd496b4d1a7b2a864c81f482e9659e24c6bcd Mon Sep 17 00:00:00 2001 From: Thibaut Lienart Date: Thu, 29 Nov 2018 11:16:29 +1100 Subject: [PATCH 1/2] Fix for #17 --- src/ColorSchemes.jl | 42 ++++++++++++++++++------------------------ test/runtests.jl | 6 ++++++ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/ColorSchemes.jl b/src/ColorSchemes.jl index cfac072..7be3dae 100644 --- a/src/ColorSchemes.jl +++ b/src/ColorSchemes.jl @@ -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 @@ -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{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) diff --git a/test/runtests.jl b/test/runtests.jl index ce452f6..2edf28c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -128,6 +128,12 @@ 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 end if get(ENV, "COLORSCHEMES_KEEP_TEST_RESULTS", false) == "true" From f489eb059ac658b6e97b365fa6c634277e4305f6 Mon Sep 17 00:00:00 2001 From: Thibaut Lienart Date: Fri, 30 Nov 2018 10:47:39 +1100 Subject: [PATCH 2/2] adding <:Real as a explicit model type for get function --- src/ColorSchemes.jl | 4 ++-- test/runtests.jl | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ColorSchemes.jl b/src/ColorSchemes.jl index 7be3dae..e190d24 100644 --- a/src/ColorSchemes.jl +++ b/src/ColorSchemes.jl @@ -276,8 +276,8 @@ img4 = get(PerceptualColourMaps.cmap("R1"), rand(10,10)) ``` """ function get(cscheme::Vector{<:Colorant}, - x::Union{Array{<:Real}, AbstractRange{<:Real}}, - rangescale::Union{Symbol, NTuple{2, Real}}=(0.0, 1.0)) + 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 diff --git a/test/runtests.jl b/test/runtests.jl index 2edf28c..3f00b45 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -134,6 +134,12 @@ function run_minimum_tests() 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"