Colors in Makie.jl

·

3 min read

# https://github.com/JuliaPlots/PlotUtils.jl/blob/master/src/colorschemes.jl
abstract type AbstractColorList end

abstract type ColorGradient <: AbstractColorList end

struct ContinuousColorGradient <: ColorGradient
    colors::ColorScheme
    values::Vector{Float64}

    function ContinuousColorGradient(colors, values = get_range(colors))
        c, v = prepare_continuous_cgrad_colors(colors, values)
        new(c, v)
    end
end

struct CategoricalColorGradient <: ColorGradient
    colors::ColorScheme
    values::Vector{Float64}

    function CategoricalColorGradient(colors, values = get_range(colors))
        c, v = prepare_categorical_cgrad_colors(colors, values)
        new(c, v)
    end
end

struct ColorPalette <: AbstractColorList
    colors::ColorScheme
end

"""
cgrad(colors, [values]; 
  categorical = nothing, scale = nothing, rev = false, alpha = nothing)

Construct a Colorgradient from `colors` and `values`.

`colors` can be a symbol for ColorSchemes.jl `ColorScheme`s, a `ColorScheme`, a vector of colors, a `ColorGradient` or a `ColorPalette`.
If `values` is an integer, it specifies the numbers of colors chosen equidistantly from the colorscheme specified by colors.
Otherwise vectors are accepted.
For continuous color gradients `values` indicate where between 0 and 1 the colors are positioned.
For categorical color gradients `values` indicate where a color ends and where a new one begins between 0 and 1.
0 and 1 are added to `values` if not already present.

If `rev` is `true` colors are reversed.
`scale` accepts the symbols `:log`, `:log10`, `:log2`, `:ln`, `:exp`, `:exp10` or functions.
If `alpha` is set, it is applied to all colors.
"""


"""
palette(colors, [n]; rev = false, alpha = nothing)

Construct a `ColorPalette`.
Accepts symbols for Colorschemes.jl `ColorScheme`s, `ColorScheme`s, vectors of colors, `ColorGradient`s and `ColorPalettes`.
If 'n' is provided, `n` colors are chosen equidistantly from the colorscheme.
If `rev` is `true` colors are reversed.
"""
# https://juliagraphics.github.io/ColorSchemes.jl/stable/
] add ColorSchemes

using ColorSchemes

ColorSchemes.Purples_5 
# => a ColorScheme 

colorschemes[:Purples_5]
# => a ColorScheme 

ColorSchemes.Purples_5.colors
# => array of five RGB colors

ColorSchemes.Purples_5.colors[3]
# => the third color in the colorscheme

get(ColorSchemes.Purples_5, 0.5)
# => the midway point of the colorscheme

colorschemes
# => Dict{Symbol, ColorScheme} with 983 entries

findcolorscheme("purple")
# => display list of matching schemes
# https://juliagraphics.github.io/ColorSchemeTools.jl/stable/makingschemes/
roygbiv = [
    colorant"red",
    colorant"orange",
    colorant"yellow",
    colorant"green",
    colorant"blue",
    colorant"indigo",
    colorant"violet"
]
scheme = make_colorscheme(roygbiv, 10)

# You can supply the colors in any format, as long as it's a Colorant:
cols = Any[
    RGB(0, 0, 1),
    Gray(0.5),
    HSV(50., 0.7, 1.),
    Gray(0.4),
    LCHab(54, 105, 40),
    HSV(285., 0.9, 0.8),
    colorant"#FFEEFF",
    colorant"hotpink",
]
scheme = make_colorscheme(cols, 8)

Color attributes

color=theme(scene, :linecolor) sets the color of the line. If no color is set, multiple calls to line! will cycle through the axis color palette. Otherwise, one can set one color per line point by passing a Vector{<:Colorant}, or one colorant for the whole line. If color is a vector of numbers, the colormap args are used to map the numbers to colors.

cycle::Vector{Symbol} = [:color] sets which attributes to cycle when creating multiple plots.

colormap::Union{Symbol, Vector{<:Colorant}} = :viridis sets the colormap that is sampled for numeric colors. PlotUtils.cgrad(...), Makie.Reverse(any_colormap) can be used as well, or any symbol from ColorBrewer or PlotUtils. To see all available color gradients, you can call Makie.available_gradients().

colorscale::Function = identity color transform function. Can be any function, but only works well together with Colorbar for identity, log, log2, log10, sqrt, logit, Makie.pseudolog10 and Makie.Symlog10.

colorrange::Tuple{<:Real, <:Real} sets the values representing the start and end points of colormap.

nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0) sets a replacement color for color = NaN.

lowclip::Union{Nothing, Symbol, <:Colorant} = nothing sets a color for any value below the colorrange.

highclip::Union{Nothing, Symbol, <:Colorant} = nothing sets a color for any value above the colorrange.

alpha = 1.0 sets the alpha value of the colormap or color attribute. Multiple alphas like in plot(alpha=0.2, color=(:red, 0.5), will get multiplied.