Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new rebase of color-libraries #7

Merged
merged 10 commits into from
Mar 2, 2017
7 changes: 6 additions & 1 deletion src/PlotUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module PlotUtils

using Reexport
@reexport using Colors
import Base: getindex

export
ColorGradient,
Expand All @@ -16,7 +17,11 @@ export
get_color_palette,
isdark,
plot_color,
adapted_grid
adapted_grid,
clibrary,
clibraries,
cgradients,
default_cgrad

include("color_utils.jl")
include("color_gradients.jl")
Expand Down
111 changes: 100 additions & 11 deletions src/color_gradients.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
type ColorLibrary
defaults::Dict{Symbol, Symbol}
lib::Dict{Symbol, Vector{RGBA{Float64}}}
ColorLibrary(defaults = Dict(:default => :sequential), lib = Dict{Symbol, Vector{RGBA{Float64}}}()) = new(defaults, lib)
end

ColorLibrary(lib::Dict{Symbol, Vector{RGBA{Float64}}}) =
ColorLibrary(Dict(:default => keys(lib)[1]), lib)

function ColorLibrary(lib::Dict{Symbol, Vector{RGBA{Float64}}}, default::Symbol)
in(default, keys(lib)) || error("There is no gradient named $default in lib")
ColorLibrary(Dict(:default => default), lib)
end

default_cgrad(cl::Symbol = _gradients[1]; kwargs...) = default_cgrad(color_libraries[cl]; kwargs...)

function default_cgrad(cl::ColorLibrary; default = nothing, sequential = nothing, diverging = nothing)
default == nothing || (cl.defaults[:default] = default)
sequential == nothing || (cl.defaults[:sequential] = sequential)
diverging == nothing || (cl.defaults[:diverging] = diverging)
end

const color_libraries = Dict{Symbol, ColorLibrary}()

function getgradient(gradient::Symbol = :default, clib::Symbol = _gradients[1])
haskey(color_libraries, clib) || error("There is no color library named $clib . Use clibraries() to get a list of available color libraries")
cl = color_libraries[clib]
getgradient(gradient, cl)
end

function getgradient(gradient::Symbol, cl::ColorLibrary)
while haskey(cl.defaults, gradient)
gradient = cl.defaults[gradient]
end
haskey(cl.lib, gradient) && return cl.lib[gradient]

potentials = [name for (name, library) in color_libraries if haskey(library.lib, gradient)]
length(potentials) == 0 && error("There is no gradient named $gradient . Use cgradients() to get a list of gradients in the current color library, clibraries() to get a list of available color libraries")
length(potentials) > 1 && warn("$gradient is found in more than one library: $(join(potentials, ", ")). Choosing $(potentials[1])")
color_libraries[potentials[1]][gradient]
end

getindex(cl::ColorLibrary, key::Symbol) = getgradient(key, cl)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice with a setindex function as well to easily add gradients (quite useful for PlotThemes, I'd imagine)

function register_gradient_colors{C<:Colorant}(name::Symbol, colors::AbstractVector{C}, color_library::Symbol = :default)
haskey(color_libraries, color_library) || register_color_library(color_library, ColorLibrary())
color_libraries[color_library].lib[name] = colors
end

function register_color_library(name::Symbol, color_library::ColorLibrary = ColorLibrary())
color_libraries[name] = color_library
end

"""
clibrary(grad::Symbol)

Set the active color library. A list of possible libraries can be printed with `clibraries()`
"""
function clibrary(grad::Symbol)
haskey(color_libraries, grad) || error("$grad is not a defined color library, valid choices are: "*join([":$(library)" for library in keys(color_libraries)], ", "))
_gradients[1] = grad
end

const _rainbowColors = [colorant"purple", colorant"blue", colorant"green", colorant"orange", colorant"red"]
const _testColors = [colorant"darkblue", colorant"blueviolet", colorant"darkcyan",colorant"green",
darken(colorant"yellow",0.3), colorant"orange", darken(colorant"red",0.2)]

const _gradients = Dict{Symbol,Vector{RGBA{Float64}}}(
const misc = ColorLibrary(Dict(:default => :sequential, :sequential => :heat, :diverging => :bluesreds), Dict(
:blues => [colorant"lightblue", colorant"darkblue"],
:reds => [colorant"lightpink", colorant"darkred"],
:greens => [colorant"lightgreen", colorant"darkgreen"],
Expand All @@ -16,11 +78,25 @@ const _gradients = Dict{Symbol,Vector{RGBA{Float64}}}(
:darkrainbow => map(darken, _rainbowColors),
:darktest => _testColors,
:lighttest => map(c -> lighten(c, 0.3), _testColors),
)
))

function register_gradient_colors{C<:Colorant}(name::Symbol, colors::AbstractVector{C})
_gradients[name] = colors
end

register_color_library(:misc, misc)
const _gradients = [:Plots]

"""
clibraries()

List the available color libraries on the system
"""
clibraries() = collect(keys(color_libraries))

"""
cgradients([color_library::Symbol])

List available color gradients in color_library (defaults to the currently loaded library)
"""
cgradients(color_library::Symbol = _gradients[1]) = collect(keys(color_libraries[color_library].lib))

# --------------------------------------------------------------------------

Expand Down Expand Up @@ -75,24 +151,32 @@ end

function iscgrad_symbol(s::Symbol)
rev, s = cgrad_reverse(s)
haskey(_gradients, s)
lib = color_libraries[_gradients[1]]
haskey(lib.lib,s) && return true
haskey(lib.defaults,s) && return true
for library in values(color_libraries)
haskey(library.lib, s) && return true
end
return false
end

function cgrad_colors(s::Symbol)
function cgrad_colors(s::Symbol; color_library = _gradients[1])
rev, s = cgrad_reverse(s)
if rev
reverse(_gradients[s])
reverse(getgradient(s, color_library))
else
_gradients[s]
getgradient(s, color_library)
end
end

cgrad_colors(grad::ColorGradient) = copy(grad.colors)
cgrad_colors(cs::Vector{RGBA{Float64}}) = cs
cgrad_colors(cs::AbstractVector) = RGBA{Float64}[plot_color(c) for c in cs]

function _color_list(arg, ::Void)
cgrad_colors(arg)
end

function _color_list(arg, alpha)
colors = cgrad_colors(arg)
for i in eachindex(colors)
Expand All @@ -101,6 +185,8 @@ function _color_list(arg, alpha)
colors
end

cgrad(arg::Symbol, cl::Symbol, values; kw...) = cgrad(cgrad_colors(arg, color_library = cl), values; kw...)

# construct a ColorGradient when given explicit values
function cgrad(arg, values; alpha = nothing)
colors = _color_list(arg, alpha)
Expand All @@ -119,6 +205,8 @@ function cgrad(arg, values; alpha = nothing)
ColorGradient(colors, values)
end

cgrad(arg::Symbol, cl::Symbol; kw...) = cgrad(cgrad_colors(arg, color_library = cl); kw...)

# construct a ColorGradient automatically
function cgrad(arg; alpha = nothing, scale = :identity)
colors = _color_list(arg, alpha)
Expand All @@ -140,10 +228,10 @@ function cgrad(arg; alpha = nothing, scale = :identity)
ColorGradient(colors, values)
end

const _default_gradient = Ref(:inferno)


# the default gradient
cgrad(; kw...) = cgrad(_default_gradient[]; kw...)
cgrad(; kw...) = cgrad(:default; kw...)


cvec(s::Symbol, n::Integer = 10; kw...) = cvec(cgrad(s; kw...), n)
Expand All @@ -156,3 +244,4 @@ end

include("gradients/matplotlib.jl")
include("gradients/cmocean.jl")
include("gradients/colorbrewer.jl")
2 changes: 1 addition & 1 deletion src/colors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ invisible() = RGBA{Float64}(0.,0.,0.,0.)

# the one-arg cases, meant for single colors
plot_color(s::AbstractString) = parse(RGBA{Float64}, s)
plot_color(s::Symbol) = iscgrad_symbol(s) ? cgrad(s) : parse(RGBA{Float64}, s)
plot_color(s::Symbol) = (iscgrad_symbol(s) ? cgrad(s) : parse(RGBA{Float64}, s))
plot_color(b::Bool) = b ? error("plot_color(true) not allowed.") : invisible()
plot_color(::Void) = invisible()
plot_color(c::Colorant) = convert(RGBA{Float64}, c)
Expand Down
38 changes: 20 additions & 18 deletions src/gradients/cmocean.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ RGB values were taken from https://github.com/matplotlib/cmocean/tree/master/cmo

=#

register_color_library(:cmocean, ColorLibrary(Dict(:default => :sequential, :sequential => :thermal, :divergent => :balance)))

register_gradient_colors(:algae, sample_evenly([
RGB(8.429022637670927631e-01, 9.769128443086748659e-01, 8.146495714674897304e-01),
RGB(8.379898654100343958e-01, 9.732407342044756549e-01, 8.088430556678033456e-01),
Expand Down Expand Up @@ -286,7 +288,7 @@ register_gradient_colors(:algae, sample_evenly([
RGB(7.158041455970551303e-02, 1.488655123209624287e-01, 8.459734502497687214e-02),
RGB(7.022733114464504989e-02, 1.454810031009998728e-01, 8.182251708486179553e-02),
RGB(6.885643403782271132e-02, 1.420894601159049808e-01, 7.903362825094448207e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:amp, sample_evenly([
RGB(9.463470914425774483e-01, 9.290101343908121478e-01, 9.257532417012246384e-01),
Expand Down Expand Up @@ -545,7 +547,7 @@ register_gradient_colors(:amp, sample_evenly([
RGB(2.459428063629143790e-01, 3.762148549775875400e-02, 7.460911056507216199e-02),
RGB(2.410021600694670640e-01, 3.645746689804394564e-02, 7.203245689473128377e-02),
RGB(2.360563646646140490e-01, 3.529747994604028744e-02, 6.943744239412558139e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:balance, sample_evenly([
RGB(9.317630180115785143e-02, 1.111733294776027225e-01, 2.615123885530547532e-01),
Expand Down Expand Up @@ -804,7 +806,7 @@ register_gradient_colors(:balance, sample_evenly([
RGB(2.558840557281191197e-01, 3.990046570244704105e-02, 7.972236454569985031e-02),
RGB(2.459622220783502511e-01, 3.762595141506584057e-02, 7.461913567560218841e-02),
RGB(2.360563646646140490e-01, 3.529747994604028744e-02, 6.943744239412558139e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:curl, sample_evenly([
RGB(8.225559928700268419e-02, 1.149244079727295142e-01, 2.647901677800857390e-01),
Expand Down Expand Up @@ -1319,7 +1321,7 @@ register_gradient_colors(:curl, sample_evenly([
RGB(2.131352588343927157e-01, 5.385491895770589538e-02, 2.173030574920574165e-01),
RGB(2.082620235717315138e-01, 5.257438772048273617e-02, 2.129328977906284059e-01),
RGB(2.034002463374002811e-01, 5.125715285178860520e-02, 2.085372063771265272e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:deep, sample_evenly([
RGB(9.928371765383620096e-01, 9.943734553013935384e-01, 8.001361955494933342e-01),
Expand Down Expand Up @@ -1578,7 +1580,7 @@ register_gradient_colors(:deep, sample_evenly([
RGB(1.614878059346168959e-01, 1.086331782717616379e-01, 1.834416540200605461e-01),
RGB(1.587995102818766935e-01, 1.056281331759973130e-01, 1.780821476813713722e-01),
RGB(1.561019746507273376e-01, 1.026082525875711138e-01, 1.727215696232307918e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:delta, sample_evenly([
RGB(6.597738601379860013e-02, 1.238600499381984077e-01, 2.494811599712867811e-01),
Expand Down Expand Up @@ -2093,7 +2095,7 @@ register_gradient_colors(:delta, sample_evenly([
RGB(9.241015243151923242e-02, 1.447574737795673805e-01, 7.947496936453349314e-02),
RGB(9.149313879389489590e-02, 1.410504572025015335e-01, 7.638732537156053826e-02),
RGB(9.053276383981978537e-02, 1.373386075843833487e-01, 7.325761429945673586e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:dense, sample_evenly([
RGB(9.022021640633741679e-01, 9.441797977915000750e-01, 9.438027309131502562e-01),
Expand Down Expand Up @@ -2352,7 +2354,7 @@ register_gradient_colors(:dense, sample_evenly([
RGB(2.226121431237116921e-01, 5.808842465569693386e-02, 1.511590815358170026e-01),
RGB(2.178074381899826051e-01, 5.700049790743180050e-02, 1.466558187034970040e-01),
RGB(2.129839422000848193e-01, 5.589168645699472276e-02, 1.422095068240733784e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:gray, sample_evenly([
RGB(5.119113838889112324e-07, 2.052270195661655352e-06, 5.982577941045682640e-06),
Expand Down Expand Up @@ -2611,7 +2613,7 @@ register_gradient_colors(:gray, sample_evenly([
RGB(9.888151187175039381e-01, 9.880911463320615207e-01, 9.827359324769276983e-01),
RGB(9.941250223040635214e-01, 9.934101568888956679e-01, 9.880231373182063459e-01),
RGB(9.994561956101176703e-01, 9.987503615523224410e-01, 9.933314215482736964e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:haline, sample_evenly([
RGB(1.629529545569048110e-01, 9.521591660747855124e-02, 4.225729247643043585e-01),
Expand Down Expand Up @@ -2870,7 +2872,7 @@ register_gradient_colors(:haline, sample_evenly([
RGB(9.808627042040826138e-01, 9.317124815536732552e-01, 5.875425838151492330e-01),
RGB(9.874684104099172854e-01, 9.342202886448683907e-01, 5.950648878797101249e-01),
RGB(9.940805805099582892e-01, 9.367275819156850591e-01, 6.026699962989522374e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:ice, sample_evenly([
RGB(1.531167435543729846e-02, 2.252059388699531942e-02, 7.272873735907764425e-02),
Expand Down Expand Up @@ -3129,7 +3131,7 @@ register_gradient_colors(:ice, sample_evenly([
RGB(9.045013427138774986e-01, 9.831399005223971921e-01, 9.860005773650694083e-01),
RGB(9.113300542301955298e-01, 9.874449459956177177e-01, 9.894442642623689776e-01),
RGB(9.180592960081255249e-01, 9.918135358838490179e-01, 9.928328638314803944e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:matter, sample_evenly([
RGB(9.942936149611202312e-01, 9.303277953232079733e-01, 6.910969022498407721e-01),
Expand Down Expand Up @@ -3388,7 +3390,7 @@ register_gradient_colors(:matter, sample_evenly([
RGB(1.950016002461211762e-01, 6.199020593164828591e-02, 2.499680455203335816e-01),
RGB(1.900821894571196047e-01, 6.057894133574073109e-02, 2.465160535124999996e-01),
RGB(1.851717128353368158e-01, 5.913348735199071976e-02, 2.430426744218359136e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:oxy, sample_evenly([
RGB(2.503217690585841648e-01, 2.046237300762866404e-02, 1.966891524096342492e-02),
Expand Down Expand Up @@ -3647,7 +3649,7 @@ register_gradient_colors(:oxy, sample_evenly([
RGB(8.680428176626151515e-01, 6.956348241211850469e-01, 1.038977462027144416e-01),
RGB(8.664728647912948167e-01, 6.908014665312249836e-01, 1.019343675562955354e-01),
RGB(8.648889520799001307e-01, 6.859800077414488495e-01, 9.998838514326469085e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:phase, sample_evenly([
RGB(6.583083928922510708e-01, 4.699391690315133929e-01, 4.941288203988051381e-02),
Expand Down Expand Up @@ -3906,7 +3908,7 @@ register_gradient_colors(:phase, sample_evenly([
RGB(6.440757220432393737e-01, 4.781157049081125598e-01, 5.119960076823739520e-02),
RGB(6.512128893528150719e-01, 4.740624350126631525e-01, 5.044367478760234530e-02),
RGB(6.583083928921535932e-01, 4.699391690315524728e-01, 4.941288204103298082e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:solar, sample_evenly([
RGB(2.014250997833959556e-01, 7.730778455372402935e-02, 9.342024025258441333e-02),
Expand Down Expand Up @@ -4165,7 +4167,7 @@ register_gradient_colors(:solar, sample_evenly([
RGB(8.802811281654138176e-01, 9.829160047653974219e-01, 2.882122575277183407e-01),
RGB(8.804129372223431504e-01, 9.884419266545670935e-01, 2.909276978215925569e-01),
RGB(8.805080058500511786e-01, 9.939881188401472611e-01, 2.936474048368232226e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:speed, sample_evenly([
RGB(9.996253193176977137e-01, 9.913711226010460953e-01, 8.041012438578545307e-01),
Expand Down Expand Up @@ -4424,7 +4426,7 @@ register_gradient_colors(:speed, sample_evenly([
RGB(9.241015243151923242e-02, 1.447574737795673805e-01, 7.947496936453349314e-02),
RGB(9.149313879389489590e-02, 1.410504572025015335e-01, 7.638732537156053826e-02),
RGB(9.053276383981978537e-02, 1.373386075843833487e-01, 7.325761429945673586e-02),
], 30))
], 30), :cmocean)

register_gradient_colors(:tempo, sample_evenly([
RGB(9.985763296811461798e-01, 9.632965417140263442e-01, 9.577895036430327247e-01),
Expand Down Expand Up @@ -4683,7 +4685,7 @@ register_gradient_colors(:tempo, sample_evenly([
RGB(8.400180885962132971e-02, 1.231074880892656653e-01, 2.689526699064171411e-01),
RGB(8.312616532498406929e-02, 1.190383729463048712e-01, 2.668628892216621806e-01),
RGB(8.225559928700268419e-02, 1.149244079727295142e-01, 2.647901677800857390e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:thermal, sample_evenly([
RGB(1.555601333154079877e-02, 1.382442454646408414e-01, 2.018108864558305071e-01),
Expand Down Expand Up @@ -4942,7 +4944,7 @@ register_gradient_colors(:thermal, sample_evenly([
RGB(9.156931782520092433e-01, 9.685354904254356301e-01, 3.482056900726151483e-01),
RGB(9.124490701578419349e-01, 9.753266872784461805e-01, 3.518533597970244786e-01),
RGB(9.090418416674036495e-01, 9.821574063216705897e-01, 3.555078064299531104e-01),
], 30))
], 30), :cmocean)

register_gradient_colors(:turbid, sample_evenly([
RGB(9.128247827303703765e-01, 9.639053479101408195e-01, 6.723488894068933019e-01),
Expand Down Expand Up @@ -5201,4 +5203,4 @@ register_gradient_colors(:turbid, sample_evenly([
RGB(1.417824484273444985e-01, 1.250035003675231404e-01, 1.101417246101454861e-01),
RGB(1.378861632611661503e-01, 1.223645204774909678e-01, 1.081750159008208478e-01),
RGB(1.339921324751868759e-01, 1.197113766395997425e-01, 1.061926684632616136e-01),
], 30))
], 30), :cmocean)
Loading