From e45cc9187e8d6fbdfc297a3e48b740f0f629fb49 Mon Sep 17 00:00:00 2001 From: Elias Carvalho Date: Tue, 26 Nov 2024 10:12:36 -0300 Subject: [PATCH] Set default alpha as 'nothing' instead of '1' --- src/Colorfy.jl | 15 ++++++++------- test/Project.toml | 2 +- test/runtests.jl | 43 ++++++++++++++++++++++++++++++------------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/Colorfy.jl b/src/Colorfy.jl index 7fed389..50b1c16 100644 --- a/src/Colorfy.jl +++ b/src/Colorfy.jl @@ -107,16 +107,14 @@ function defaultalphas(values::Values) vinds = setdiff(1:length(values), minds) if isempty(minds) - fill(1, length(values)) + fill(nothing, length(values)) else valphas = defaultalphas(nonmissingvec(values[vinds])) - malpha = zero(eltype(valphas)) + malpha = nothing genvec(vinds, valphas, minds, malpha, length(values)) end end -defaultalphas(values::Values{Colorant}) = alpha.(values) - """ Colorfy.defaultcolorscheme(values) @@ -178,7 +176,7 @@ function colors(colorfier::Colorfier) # required to handle Vector{Union{Missing,T}} without missing values vvals = nonmissingvec(vals) vcolorfier = update(colorfier, values=vvals) - coloralpha.(getcolors(vcolorfier), alphas(vcolorfier)) + setalpha.(getcolors(vcolorfier), alphas(vcolorfier)) else # get valid colors and set "transparent" for invalid values vvals = nonmissingvec(vals[vinds]) @@ -228,12 +226,15 @@ end # HELPER FUNCTIONS # ----------------- +setalpha(color, alpha) = coloralpha(color, alpha) +setalpha(color, ::Nothing) = color + nonmissingvec(x::AbstractVector{T}) where {T} = convert(AbstractVector{nonmissingtype(T)}, x) function genvec(vecinds, vec, valinds, val, len) valdict = Dict(i => val for i in valinds) - merge!(valdict, Dict(zip(vecinds, vec))) - [valdict[i] for i in 1:len] + dict = merge(valdict, Dict(zip(vecinds, vec))) + [dict[i] for i in 1:len] end end diff --git a/test/Project.toml b/test/Project.toml index cf608be..44e5f85 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,9 +1,9 @@ [deps] CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597" ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" -Colorfy = "03fe91ce-8ec6-4610-8e8d-e7491ccca690" Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" +FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d" diff --git a/test/runtests.jl b/test/runtests.jl index 9930063..3defcd3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,7 @@ using Colorfy using Colors using ColorSchemes +using FixedPointNumbers using CategoricalArrays using Distributions using Unitful @@ -12,7 +13,7 @@ using Test values = rand(10) colorfier = Colorfier(values) @test Colorfy.values(colorfier) == values - @test Colorfy.alphas(colorfier) == fill(1, 10) + @test Colorfy.alphas(colorfier) == fill(nothing, 10) @test Colorfy.colorscheme(colorfier) == colorschemes[:viridis] @test Colorfy.colorrange(colorfier) == :extrema @@ -24,25 +25,25 @@ using Test colorfier = Colorfier(values, colorscheme=:grays) @test Colorfy.values(colorfier) == values - @test Colorfy.alphas(colorfier) == fill(1, 10) + @test Colorfy.alphas(colorfier) == fill(nothing, 10) @test Colorfy.colorscheme(colorfier) == colorschemes[:grays] @test Colorfy.colorrange(colorfier) == :extrema colorfier = Colorfier(values, colorscheme="grays") @test Colorfy.values(colorfier) == values - @test Colorfy.alphas(colorfier) == fill(1, 10) + @test Colorfy.alphas(colorfier) == fill(nothing, 10) @test Colorfy.colorscheme(colorfier) == colorschemes[:grays] @test Colorfy.colorrange(colorfier) == :extrema colorfier = Colorfier(values, colorrange=(0.25, 0.75)) @test Colorfy.values(colorfier) == values - @test Colorfy.alphas(colorfier) == fill(1, 10) + @test Colorfy.alphas(colorfier) == fill(nothing, 10) @test Colorfy.colorscheme(colorfier) == colorschemes[:viridis] @test Colorfy.colorrange(colorfier) == (0.25, 0.75) colorfier = Colorfier(values, colorrange=(0, 0.5)) @test Colorfy.values(colorfier) == values - @test Colorfy.alphas(colorfier) == fill(1, 10) + @test Colorfy.alphas(colorfier) == fill(nothing, 10) @test Colorfy.colorscheme(colorfier) == colorschemes[:viridis] @test Colorfy.colorrange(colorfier) == (0.0, 0.5) @@ -62,16 +63,16 @@ using Test values = rand(10) colorfier = Colorfier(values) colors = get(colorschemes[:viridis], values, :extrema) - @test Colorfy.colors(colorfier) == coloralpha.(colors, 1) + @test Colorfy.colors(colorfier) == colors colorfier = Colorfier(values, colorscheme=:grays, colorrange=(0.25, 0.75)) colors = get(colorschemes[:grays], values, (0.25, 0.75)) - @test Colorfy.colors(colorfier) == coloralpha.(colors, 1) + @test Colorfy.colors(colorfier) == colors colors = [colorant"red", colorant"green", colorant"blue", colorant"white", colorant"black"] values = ["red", "green", "blue", "white", "black"] colorfier = Colorfier(values) - @test Colorfy.colors(colorfier) == coloralpha.(colors, 1) + @test Colorfy.colors(colorfier) == colors values = [:red, :green, :blue, :white, :black] colorfier = Colorfier(values, alphas=0.5) @@ -86,6 +87,22 @@ using Test colorfier = Colorfier(values) @test Colorfy.colors(colorfier) == values + values = [Gray(rand(Q0f7)) for _ in 1:10] + colorfier = Colorfier(values) + @test Colorfy.colors(colorfier) == values + + values = [Gray(rand(Q0f15)) for _ in 1:10] + colorfier = Colorfier(values) + @test Colorfy.colors(colorfier) == values + + values = [Gray(rand(Q0f31)) for _ in 1:10] + colorfier = Colorfier(values) + @test Colorfy.colors(colorfier) == values + + values = [Gray(rand(Q0f63)) for _ in 1:10] + colorfier = Colorfier(values) + @test Colorfy.colors(colorfier) == values + # error: unsupported values values = [nothing, nothing, nothing] colorfier = Colorfier(values) @@ -98,10 +115,10 @@ using Test @testset "colorfy" begin values = rand(10) colors = get(colorschemes[:viridis], values, :extrema) - @test colorfy(values) == coloralpha.(colors, 1) + @test colorfy(values) == colors colors = get(colorschemes[:grays], values, (0.25, 0.75)) - @test colorfy(values, colorscheme=:grays, colorrange=(0.25, 0.75)) == coloralpha.(colors, 1) + @test colorfy(values, colorscheme=:grays, colorrange=(0.25, 0.75)) == colors end @testset "Invalid values" begin @@ -133,19 +150,19 @@ using Test values = categorical(["n", "n", "y", "y", "n", "y"], levels=["y", "n"]) categcolors = colorschemes[:viridis][range(0, 1, length=2)] colors = categcolors[[2, 2, 1, 1, 2, 1]] - @test colorfy(values) == coloralpha.(colors, 1) + @test colorfy(values) == colors @test colorfy(values, alphas=0.5) == coloralpha.(colors, 0.5) values = categorical([2, 1, 1, 3, 1, 3, 3, 2, 1, 2], levels=1:3) categcolors = colorschemes[:viridis][range(0, 1, length=3)] colors = categcolors[[2, 1, 1, 3, 1, 3, 3, 2, 1, 2]] - @test colorfy(values) == coloralpha.(colors, 1) + @test colorfy(values) == colors @test colorfy(values, alphas=0.5) == coloralpha.(colors, 0.5) values = categorical([1, 1, 1, 1, 1], levels=[1]) categcolors = colorschemes[:viridis][range(0, 0, length=1)] colors = categcolors[[1, 1, 1, 1, 1]] - @test colorfy(values) == coloralpha.(colors, 1) + @test colorfy(values) == colors @test colorfy(values, alphas=0.5) == coloralpha.(colors, 0.5) end