Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ Distributions = "0.25"
FixedPointNumbers = "0.8"
Unitful = "1.19"
julia = "1.10"

[extras]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Test", "CategoricalArrays", "ColorSchemes", "Colors", "Distributions", "FixedPointNumbers", "Unitful"]
48 changes: 9 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,25 @@
[![Build Status](https://github.com/JuliaGraphics/Colorfy.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/JuliaGraphics/Colorfy.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Coverage](https://codecov.io/gh/JuliaGraphics/Colorfy.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaGraphics/Colorfy.jl)

Colorfy.jl is a package for mapping Julia objects into colors defined by [Colors.jl](https://github.com/JuliaGraphics/Colors.jl).
Colorfy.jl is a package for mapping Julia objects into colors
defined by [Colors.jl](https://github.com/JuliaGraphics/Colors.jl).

## Usage

The use of this package is centralized in the `Colorfier` struct.
This type stores the necessary info to convert a list of values into a list of colors.
To extract the mapped colors from the colorfier, use the `Colorfy.colors` function:

```julia
julia> values = rand(5)
5-element Vector{Float64}:
0.6725922579880301
0.48419175677473947
0.555196651363384
0.9391048282597594
0.06139286380440967

julia> colorfier = Colorfier(values);

julia> Colorfy.colors(colorfier)
5-element Array{RGBA{Float64},1} with eltype ColorTypes.RGBA{Float64}:
RGBA{Float64}(0.25686927730139364,0.744083158641258,0.4461059964847054,1.0)
RGBA{Float64}(0.13396134240010413,0.5479273380822499,0.5536215374839386,1.0)
RGBA{Float64}(0.12033938235581829,0.6238620558326493,0.534269659829833,1.0)
RGBA{Float64}(0.993248,0.906157,0.143936,1.0)
RGBA{Float64}(0.267004,0.004874,0.329415,1.0)

julia> colorfier = Colorfier(values, alphas=0.5);

julia> Colorfy.colors(colorfier)
5-element Array{RGBA{Float64},1} with eltype ColorTypes.RGBA{Float64}:
RGBA{Float64}(0.25686927730139364,0.744083158641258,0.4461059964847054,0.5)
RGBA{Float64}(0.13396134240010413,0.5479273380822499,0.5536215374839386,0.5)
RGBA{Float64}(0.12033938235581829,0.6238620558326493,0.534269659829833,0.5)
RGBA{Float64}(0.993248,0.906157,0.143936,0.5)
RGBA{Float64}(0.267004,0.004874,0.329415,0.5)
```

For convenience, the `colorfy` function is defined. This function
creates a `Colorfier` instance and calls the `Colorfy.colors` function:
The `colorfy` function takes any vector of values with options
(e.g., alpha, colorscheme) and converts into valid colors:

```julia
julia> values = [:red, :green, :blue];

julia> colorfy(values, alphas=[0.5, 0.6, 0.7])
julia> colorfy(values, alpha=[0.5, 0.6, 0.7])
3-element Array{RGBA{N0f8},1} with eltype ColorTypes.RGBA{FixedPointNumbers.N0f8}:
RGBA{N0f8}(1.0,0.0,0.0,0.5)
RGBA{N0f8}(0.0,0.502,0.0,0.6)
RGBA{N0f8}(0.0,0.0,1.0,0.7)
```

See the `Colorfier` docstring for a description of all options.
Please check the `colorfy` docstring for more details.

Developers can register colorful representations for their
types by implementing methods for the `Colorfy.repr` function.
16 changes: 7 additions & 9 deletions ext/ColorfyCategoricalArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

module ColorfyCategoricalArraysExt

using Colorfy
using Colorfy: Values
using ColorSchemes: colorschemes
using CategoricalArrays: CategoricalValue, levels, levelcode
using CategoricalArrays: CategoricalValue
using CategoricalArrays: levels, levelcode

function Colorfy.getcolors(colorfier::Colorfier{<:Values{CategoricalValue}})
values = Colorfy.values(colorfier)
colorscheme = Colorfy.colorscheme(colorfier)
import Colorfy

function Colorfy.repr(values::AbstractVector{<:CategoricalValue}, colorscheme, colorrange)
nlevels = length(levels(values))
categcolors = colorscheme[range(0, nlevels > 1 ? 1 : 0, length=nlevels)]
categcolors[levelcode.(values)]
lcolors = colorscheme[range(0, nlevels > 1 ? 1 : 0, length=nlevels)]
lcolors[levelcode.(values)]
end

end
36 changes: 21 additions & 15 deletions ext/ColorfyDistributionsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@

module ColorfyDistributionsExt

using Colorfy
using Colorfy: Values
using Distributions: Distribution, location, scale

function Colorfy.getcolors(colorfier::Colorfier{<:Values{Distribution}})
values = location.(Colorfy.values(colorfier))
dcolorfier = Colorfy.update(colorfier; values)
Colorfy.getcolors(dcolorfier)
end
using Distributions: Distribution
using Distributions: location, scale
using Colors: coloralpha

import Colorfy

function Colorfy.repr(values::AbstractVector{<:Distribution}, colorscheme, colorrange)
# extract location and scale parameters
μs = location.(values)
σs = scale.(values)

function Colorfy.defaultalphas(values::Values{Distribution})
s = scale.(values)
a, b = extrema(s)
if a == b
fill(1, length(values))
# compute alphas based on scale parameters
a, b = extrema(σs)
αs = if a == b
fill(1, length(μs))
else
@. 1 - (s - a) / (b - a)
@. 1 - (σs - a) / (b - a)
end

# get colors for location parameters
cs = Colorfy.repr(μs, colorscheme, colorrange)

# apply alphas to colors
coloralpha.(cs, αs)
end

end
14 changes: 6 additions & 8 deletions ext/ColorfyUnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

module ColorfyUnitfulExt

using Colorfy
using Colorfy: Values
using Unitful: Quantity, ustrip
using Unitful: Quantity
using Unitful: ustrip

function Colorfy.getcolors(colorfier::Colorfier{<:Values{Quantity}})
values = ustrip.(Colorfy.values(colorfier))
ucolorfier = Colorfy.update(colorfier; values)
Colorfy.getcolors(ucolorfier)
end
import Colorfy

Colorfy.repr(values::AbstractVector{<:Quantity}, colorscheme, colorrange) =
Colorfy.repr(ustrip.(values), colorscheme, colorrange)

Colorfy.ascolorrange(colorrange::NTuple{2,Quantity}) = Colorfy.ascolorrange(ustrip.(colorrange))

Expand Down
Loading
Loading