logo

Package for Color Processing in Julia 📂Julia

Package for Color Processing in Julia

Introduction1

Introducing the capabilities of Colors.jl, a package for color processing in Julia. When using the visualization package Plots.jl, there’s no need to load Colors.jl separately. It provides the following functionalities:

  • Color parsing and conversion
  • Color maps
  • Color scales

Parsing and Conversion

Assuming str is a string representing color information, you can parse the string into a color code of a specific color space using @colorant_str or parse(Colorant, str). Note that colorant means a dye or pigment.

julia> using Colors

julia> colorant"red"
RGB{N0f8}(1.0,0.0,0.0)

julia> colorant"rgb(255, 0, 0)"
RGB{N0f8}(1.0,0.0,0.0)

julia> parse(Colorant, "rgba(0, 255, 0, 0.5)")
RGBA{N0f8}(0.0,1.0,0.0,0.502)

julia> parse(Colorant, "#FF0000")
RGB{N0f8}(1.0,0.0,0.0)

julia> parse(Colorant, "hsl(120, 100%, 25%)")
HSL{Float32}(120.0f0,1.0f0,0.25f0)

The convert() function can be used to convert to a color code of a different color space.

julia> convert(RGB, HSL(270, 0.5, 0.5))
RGB{Float64}(0.5,0.25,0.75)

Color Interpolation

Using the range() function, colors can be interpolated. This operation is quite logical and intuitive. For example, consider the RGB codes. Red is represented by (255,0,0)(255, 0, 0) or (1,0,0)(1, 0, 0), which is essentially a 3D vector. Therefore, there’s no reason not to use the color code as an argument for the range() function which interpolates between two vectors.

julia> v1 = [1.0, 0.0, 0.0];

julia> v2 = [0.0, 0.5, 0.0];

julia> collect(range(v1, v2, length = 15))
15-element Vector{Vector{Float64}}:
 [1.0, 0.0, 0.0]
 [0.9285714285714286, 0.03571428571428571, 0.0]
 [0.8571428571428572, 0.07142857142857142, 0.0]
 [0.7857142857142857, 0.10714285714285714, 0.0]
 [0.7142857142857143, 0.14285714285714285, 0.0]
 [0.6428571428571428, 0.17857142857142858, 0.0]
 [0.5714285714285714, 0.21428571428571427, 0.0]
 [0.5, 0.25, 0.0]
 [0.4285714285714286, 0.2857142857142857, 0.0]
 [0.3571428571428571, 0.32142857142857145, 0.0]
 [0.2857142857142857, 0.35714285714285715, 0.0]
 [0.2142857142857143, 0.39285714285714285, 0.0]
 [0.1428571428571429, 0.42857142857142855, 0.0]
 [0.0714285714285714, 0.4642857142857143, 0.0]
 [0.0, 0.5, 0.0]

julia> c1 = colorant"rgb(255, 0, 0)"
RGB{N0f8}(1.0,0.0,0.0)

julia> c2 = colorant"rgb(0, 128, 0)"
RGB{N0f8}(0.0,0.502,0.0)

julia> range(colorant"rgb(255,0,0)", colorant"rgb(0,128,0)", length=15)
15-element Array{RGB{N0f8},1} with eltype RGB{FixedPointNumbers.N0f8}:
 RGB{N0f8}(1.0,0.0,0.0)
 RGB{N0f8}(0.929,0.035,0.0)
 RGB{N0f8}(0.859,0.071,0.0)
 RGB{N0f8}(0.784,0.11,0.0)
 RGB{N0f8}(0.714,0.145,0.0)
 RGB{N0f8}(0.643,0.18,0.0)
 RGB{N0f8}(0.573,0.216,0.0)
 RGB{N0f8}(0.502,0.251,0.0)
 RGB{N0f8}(0.427,0.286,0.0)
 RGB{N0f8}(0.357,0.322,0.0)
 RGB{N0f8}(0.286,0.357,0.0)
 RGB{N0f8}(0.216,0.392,0.0)
 RGB{N0f8}(0.141,0.431,0.0)
 RGB{N0f8}(0.071,0.467,0.0)
 RGB{N0f8}(0.0,0.502,0.0)

Visualization of the above color range can be obtained in VS Code by installing and running the Julia extension as follows.

Also, the return of range() can be used as a palette.

my_palette = range(colorant"rgb(255,0,0)", colorant"rgb(0,128,0)", length=15)

plot(rand(10, 15), palette = my_palette)

Environment

  • OS: Windows11
  • Version: Julia 1.9.4, Plots v1.39.0, Colors v0.12.10

See Also