Skip to content

Commit

Permalink
Set default alpha as 'nothing' instead of '1'
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Nov 26, 2024
1 parent 8e15b76 commit e45cc91
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
15 changes: 8 additions & 7 deletions src/Colorfy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
@@ -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"
43 changes: 30 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Colorfy
using Colors
using ColorSchemes
using FixedPointNumbers
using CategoricalArrays
using Distributions
using Unitful
Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e45cc91

Please sign in to comment.