Skip to content

Commit

Permalink
Merge pull request #1 from mkborregaard/no-explicit-passing-of-libraries
Browse files Browse the repository at this point in the history
Simplify by disallowing directly passing color library
  • Loading branch information
mkborregaard authored Feb 28, 2017
2 parents f923e67 + 5175ee8 commit f649698
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/PlotUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ export
isdark,
plot_color,
adapted_grid,
set_clibrary,
clibrary,
clibraries,
cgradients,
cgraddefaults
default_cgrad

include("color_utils.jl")
include("color_gradients.jl")
Expand Down
51 changes: 26 additions & 25 deletions src/color_gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ function ColorLibrary(lib::Dict{Symbol, Vector{RGBA{Float64}}}, default::Symbol)
ColorLibrary(Dict(:default => default), lib)
end

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

function cgraddefaults(cl::ColorLibrary; default = nothing, sequential = nothing, diverging = nothing)
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, clibrary::Symbol = _gradients[1])
haskey(color_libraries, clibrary) || error("There is no color library named $clibrary . Use clibraries() to get a list of available color libraries")
cl = color_libraries[clibrary]
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

Expand Down Expand Up @@ -56,21 +56,16 @@ end
Set the active color library. A list of possible libraries can be printed with `clibraries()`
"""
function set_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

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)], ", "))
color_libraries[grad]
_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 Plots_internal = ColorLibrary(Dict(:default => :heat), Dict(
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 @@ -86,8 +81,8 @@ const Plots_internal = ColorLibrary(Dict(:default => :heat), Dict(
))


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

"""
clibraries()
Expand Down Expand Up @@ -154,9 +149,9 @@ function cgrad_reverse(s::Symbol)
end
end

function iscgrad_symbol(s::Symbol; color_library = _gradients[1])
function iscgrad_symbol(s::Symbol)
rev, s = cgrad_reverse(s)
lib = isa(color_library, Symbol) ? color_libraries[color_library] : color_library
lib = color_libraries[_gradients[1]]
haskey(lib.lib,s) && return true
haskey(lib.defaults,s) && return true
for library in values(color_libraries)
Expand All @@ -176,23 +171,25 @@ end

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

function _color_list(arg, ::Void; color_library = _gradients[1])
cgrad_colors(arg; color_library = color_library)
function _color_list(arg, ::Void)
cgrad_colors(arg)
end

function _color_list(arg, alpha; color_library = _gradients[1])
colors = cgrad_colors(arg; color_library = color_library)
function _color_list(arg, alpha)
colors = cgrad_colors(arg)
for i in eachindex(colors)
colors[i] = RGBA{Float64}(convert(RGB{Float64}, colors[i]), alpha)
end
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, color_library = _gradients[1])
colors = _color_list(arg, alpha; color_library = color_library)
function cgrad(arg, values; alpha = nothing)
colors = _color_list(arg, alpha)
values = if length(colors) == length(values) && values[1] == 0 && values[end] == 1
values
else
Expand All @@ -208,9 +205,11 @@ function cgrad(arg, values; alpha = nothing, color_library = _gradients[1])
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, color_library = _gradients[1])
colors = _color_list(arg, alpha, color_library = color_library)
function cgrad(arg; alpha = nothing, scale = :identity)
colors = _color_list(arg, alpha)
values = if scale in (:log, :log10)
log10(linspace(1,10,30))
elseif scale == :log2
Expand All @@ -229,6 +228,8 @@ function cgrad(arg; alpha = nothing, scale = :identity, color_library = _gradien
ColorGradient(colors, values)
end



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

Expand Down
4 changes: 2 additions & 2 deletions 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; color_library = _gradients[1]) = (iscgrad_symbol(s, color_library = color_library) ? 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 All @@ -17,7 +17,7 @@ plot_color(x, ::Void) = plot_color(x)
# alpha override
plot_color(x, α::Number) = RGBA{Float64}(convert(RGB, plot_color(x)), α)
plot_color(c::Colorant, α::Number) = RGBA{Float64}(red(c), green(c), blue(c), α)
plot_color(s::Symbol, α::Number; color_library = _gradients[1]) = (iscgrad_symbol(s, color_library = color_library) ? cgrad(s, alpha=α) : RGBA{Float64}(convert(RGB, plot_color(s)), α))
plot_color(s::Symbol, α::Number) = (iscgrad_symbol(s) ? cgrad(s, alpha=α) : RGBA{Float64}(convert(RGB, plot_color(s)), α))
plot_color(grad::ColorGradient, α::Number) = cgrad(grad, alpha=α)

function plot_color(cs::AbstractArray)
Expand Down
12 changes: 0 additions & 12 deletions src/gradients/colorbrewer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,6 @@ register_gradient_colors(:RdGy, [
RGB(77/255, 77/255, 77/255),
RGB(26/255, 26/255, 26/255)], :colorbrewer)

register_gradient_colors(:PuOr, [
RGB(127/255, 59/255, 8/255),
RGB(179/255, 88/255, 6/255),
RGB(224/255, 130/255, 20/255),
RGB(253/255, 184/255, 99/255),
RGB(254/255, 224/255, 182/255),
RGB(247/255, 247/255, 247/255),
RGB(216/255, 218/255, 235/255),
RGB(178/255, 171/255, 210/255),
RGB(128/255, 115/255, 172/255),
RGB(84/255, 39/255, 136/255),
RGB(45/255, 0/255, 75/255)], :colorbrewer)

# These are commented out as they constitute non-continuous gradients, and are thus used differently by Plots
# register_gradient_colors(:Set2, [
Expand Down
28 changes: 21 additions & 7 deletions src/gradients/matplotlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# The following gradients were taken from https://github.com/BIDS/colormap/blob/master/colormaps.py
# Here is the licensing note which accompanied this:

# New matplotlib colormaps by Nathaniel J. Smith, Stefan van der Walt,
# New Plots colormaps by Nathaniel J. Smith, Stefan van der Walt,
# and (in the case of viridis) Eric Firing.
#
# This file and the colormaps in it are released under the CC0 license /
Expand All @@ -21,7 +21,7 @@

# note: to use the full arrays, just take out the second param (n) from the sample_evenly calls

register_color_library(:matplotlib, ColorLibrary(Dict(:default => :sequential, :sequential => :inferno)))
register_color_library(:Plots, ColorLibrary(Dict(:default => :sequential, :sequential => :inferno, :diverging => :PuOr)))

register_gradient_colors(:magma, sample_evenly([
RGB(0.001462, 0.000466, 0.013866),
Expand Down Expand Up @@ -280,7 +280,7 @@ register_gradient_colors(:magma, sample_evenly([
RGB(0.987691, 0.977154, 0.734536),
RGB(0.987387, 0.984288, 0.742002),
RGB(0.987053, 0.991438, 0.749504)
], 30), :matplotlib)
], 30), :Plots)

register_gradient_colors(:inferno, sample_evenly([
RGB(0.001462, 0.000466, 0.013866),
Expand Down Expand Up @@ -539,7 +539,7 @@ register_gradient_colors(:inferno, sample_evenly([
RGB(0.976511, 0.989753, 0.616760),
RGB(0.982257, 0.994109, 0.631017),
RGB(0.988362, 0.998364, 0.644924)
], 30), :matplotlib)
], 30), :Plots)

register_gradient_colors(:plasma, sample_evenly([
RGB(0.050383, 0.029803, 0.527975),
Expand Down Expand Up @@ -798,7 +798,7 @@ register_gradient_colors(:plasma, sample_evenly([
RGB(0.944152, 0.961916, 0.146861),
RGB(0.941896, 0.968590, 0.140956),
RGB(0.940015, 0.975158, 0.131326)
], 30), :matplotlib)
], 30), :Plots)

register_gradient_colors(:viridis, sample_evenly([
RGB(0.267004, 0.004874, 0.329415),
Expand Down Expand Up @@ -1057,9 +1057,23 @@ register_gradient_colors(:viridis, sample_evenly([
RGB(0.974417, 0.903590, 0.130215),
RGB(0.983868, 0.904867, 0.136897),
RGB(0.993248, 0.906157, 0.143936)
], 30), :matplotlib)
], 30), :Plots)


# end of matplotlib colormaps

register_gradient_colors(:PuOr, [
RGB(127/255, 59/255, 8/255),
RGB(179/255, 88/255, 6/255),
RGB(224/255, 130/255, 20/255),
RGB(253/255, 184/255, 99/255),
RGB(254/255, 224/255, 182/255),
RGB(247/255, 247/255, 247/255),
RGB(216/255, 218/255, 235/255),
RGB(178/255, 171/255, 210/255),
RGB(128/255, 115/255, 172/255),
RGB(84/255, 39/255, 136/255),
RGB(45/255, 0/255, 75/255)], :Plots)

# end of Plots colormaps
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------

0 comments on commit f649698

Please sign in to comment.