Skip to content

Commit

Permalink
Update color mapping for categorical values (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv authored Apr 8, 2024
1 parent 257c3bd commit 950ba75
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions ext/ColorfyCategoricalArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ module ColorfyCategoricalArraysExt
using Colorfy
using Colorfy: Values
using ColorSchemes: colorschemes
using CategoricalArrays: CategoricalValue, levelcode
using CategoricalArrays: CategoricalValue, levels, levelcode

function Colorfy.getcolors(colorfier::Colorfier{<:Values{CategoricalValue}})
values = Colorfy.values(colorfier)
colorscheme = Colorfy.colorscheme(colorfier)
colorscheme[levelcode.(values)]
nlevels = length(levels(values))
categcolors = colorscheme[range(0, 1, length=nlevels)]
categcolors[levelcode.(values)]
end

Colorfy.defaultcolorscheme(::Values{CategoricalValue}) = colorschemes[:Set3_9]

end
9 changes: 8 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ using Test

@testset "CategoricalArrays" begin
values = categorical(["n", "n", "y", "y", "n", "y"], levels=["y", "n"])
colors = colorschemes[:Set3_9][[2, 2, 1, 1, 2, 1]]
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, 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, alphas=0.5) == coloralpha.(colors, 0.5)
end
Expand Down

0 comments on commit 950ba75

Please sign in to comment.