From 5a17c98a1ec1981a2ac11d37510aad40cc94fcd3 Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Tue, 21 Feb 2017 21:22:48 +0100 Subject: [PATCH 1/9] new rebase of color-libraries --- src/PlotUtils.jl | 8 +- src/color_gradients.jl | 127 ++++++++-- src/colors.jl | 4 +- src/gradients/cmocean.jl | 38 +-- src/gradients/colorbrewer.jl | 434 +++++++++++++++++++++++++++++++++++ src/gradients/matplotlib.jl | 10 +- 6 files changed, 575 insertions(+), 46 deletions(-) create mode 100644 src/gradients/colorbrewer.jl diff --git a/src/PlotUtils.jl b/src/PlotUtils.jl index 7ceecf0..db0911a 100644 --- a/src/PlotUtils.jl +++ b/src/PlotUtils.jl @@ -5,6 +5,7 @@ module PlotUtils using Reexport @reexport using Colors +import Base: getindex export ColorGradient, @@ -16,7 +17,12 @@ export get_color_palette, isdark, plot_color, - adapted_grid + adapted_grid, + set_clibrary, + clibrary, + clibraries, + cgradients, + cgraddefaults include("color_utils.jl") include("color_gradients.jl") diff --git a/src/color_gradients.jl b/src/color_gradients.jl index e1d3f58..af8a9ab 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -1,9 +1,74 @@ +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 + +function cgraddefaults(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] + 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) + +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) + 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 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)], ", ")) + 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 Plots_internal = ColorLibrary(Dict(:default => :heat), Dict( :blues => [colorant"lightblue", colorant"darkblue"], :reds => [colorant"lightpink", colorant"darkred"], :greens => [colorant"lightgreen", colorant"darkgreen"], @@ -16,11 +81,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(:Plots_internal, Plots_internal) +const _gradients = [:matplotlib] + +""" + 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)) # -------------------------------------------------------------------------- @@ -73,28 +152,35 @@ function cgrad_reverse(s::Symbol) end end -function iscgrad_symbol(s::Symbol) +function iscgrad_symbol(s::Symbol; color_library::Symbol = _gradients[1]) rev, s = cgrad_reverse(s) - haskey(_gradients, s) + haskey(color_libraries[color_library].lib,s) && return true + haskey(color_libraries[color_library].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::Symbol = _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) +function _color_list(arg, ::Void; color_library::Symbol = _gradients[1]) + cgrad_colors(arg; color_library = color_library) end -function _color_list(arg, alpha) - colors = cgrad_colors(arg) + +function _color_list(arg, alpha; color_library::Symbol = _gradients[1]) + colors = cgrad_colors(arg; color_library = color_library) for i in eachindex(colors) colors[i] = RGBA{Float64}(convert(RGB{Float64}, colors[i]), alpha) end @@ -102,8 +188,8 @@ function _color_list(arg, alpha) end # construct a ColorGradient when given explicit values -function cgrad(arg, values; alpha = nothing) - colors = _color_list(arg, alpha) +function cgrad(arg, values; alpha = nothing, color_library::Symbol = _gradients[1]) + colors = _color_list(arg, alpha; color_library = color_library) values = if length(colors) == length(values) && values[1] == 0 && values[end] == 1 values else @@ -120,8 +206,8 @@ function cgrad(arg, values; alpha = nothing) end # construct a ColorGradient automatically -function cgrad(arg; alpha = nothing, scale = :identity) - colors = _color_list(arg, alpha) +function cgrad(arg; alpha = nothing, scale = :identity, color_library::Symbol = _gradients[1]) + colors = _color_list(arg, alpha, color_library = color_library) values = if scale in (:log, :log10) log10(linspace(1,10,30)) elseif scale == :log2 @@ -140,10 +226,8 @@ 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) @@ -156,3 +240,4 @@ end include("gradients/matplotlib.jl") include("gradients/cmocean.jl") +include("gradients/colorbrewer.jl") diff --git a/src/colors.jl b/src/colors.jl index 3f88371..174323d 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -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; color_library::Symbol = _gradients[1]) = (iscgrad_symbol(s, color_library = color_library) ? 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) @@ -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) = (iscgrad_symbol(s) ? cgrad(s, alpha=α) : RGBA{Float64}(convert(RGB, plot_color(s)), α)) +plot_color(s::Symbol, α::Number; color_library::Symbol = _gradients[1]) = (iscgrad_symbol(s, color_library = color_library) ? cgrad(s, alpha=α) : RGBA{Float64}(convert(RGB, plot_color(s)), α)) plot_color(grad::ColorGradient, α::Number) = cgrad(grad, alpha=α) function plot_color(cs::AbstractArray) diff --git a/src/gradients/cmocean.jl b/src/gradients/cmocean.jl index 2a2158e..7fde103 100644 --- a/src/gradients/cmocean.jl +++ b/src/gradients/cmocean.jl @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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), @@ -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) diff --git a/src/gradients/colorbrewer.jl b/src/gradients/colorbrewer.jl new file mode 100644 index 0000000..58039ea --- /dev/null +++ b/src/gradients/colorbrewer.jl @@ -0,0 +1,434 @@ +#= + +Apache-Style Software License for ColorBrewer software and ColorBrewer Color Schemes + +Copyright (c) 2002 Cynthia Brewer, Mark Harrower, and The Pennsylvania State University. + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed +under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +CONDITIONS OF ANY KIND, either express or implied. See the License for the +specific language governing permissions and limitations under the License. + +This text from my earlier Apache License Version 1.1 also remains in place for guidance on attribution and permissions: +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +1. Redistributions as source code must retain the above copyright notice, this list of conditions and the following disclaimer. +2. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: +"This product includes color specifications and designs developed by Cynthia Brewer (http://colorbrewer.org/)." +Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. +4. The name "ColorBrewer" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact Cynthia Brewer at cbrewer@psu.edu. +5. Products derived from this software may not be called "ColorBrewer", nor may "ColorBrewer" appear in their name, without prior written permission of Cynthia Brewer. + +RGB values were taken from http://www.colorbrewer2.org +=# + +register_color_library(:colorbrewer, ColorLibrary(Dict(:default => :sequential, :sequential => :Spectral, :divergent => :RdBu))) + +register_gradient_colors(:Spectral, [ + RGB(158/255, 1/255, 66/255), + RGB(213/255, 62/255, 79/255), + RGB(244/255, 109/255, 67/255), + RGB(253/255, 174/255, 97/255), + RGB(254/255, 224/255, 139/255), + RGB(255/255, 255/255, 191/255), + RGB(230/255, 245/255, 152/255), + RGB(171/255, 221/255, 164/255), + RGB(102/255, 194/255, 165/255), + RGB(50/255, 136/255, 189/255), + RGB(94/255, 79/255, 162/255)], :colorbrewer) + +register_gradient_colors(:RdYlGn, [ + RGB(165/255, 0/255, 38/255), + RGB(215/255, 48/255, 39/255), + RGB(244/255, 109/255, 67/255), + RGB(253/255, 174/255, 97/255), + RGB(254/255, 224/255, 139/255), + RGB(255/255, 255/255, 191/255), + RGB(217/255, 239/255, 139/255), + RGB(166/255, 217/255, 106/255), + RGB(102/255, 189/255, 99/255), + RGB(26/255, 152/255, 80/255), + RGB(0/255, 104/255, 55/255)], :colorbrewer) + +register_gradient_colors(:RdBu, [ + RGB(103/255, 0/255, 31/255), + RGB(178/255, 24/255, 43/255), + RGB(214/255, 96/255, 77/255), + RGB(244/255, 165/255, 130/255), + RGB(253/255, 219/255, 199/255), + RGB(247/255, 247/255, 247/255), + RGB(209/255, 229/255, 240/255), + RGB(146/255, 197/255, 222/255), + RGB(67/255, 147/255, 195/255), + RGB(33/255, 102/255, 172/255), + RGB(5/255, 48/255, 97/255)], :colorbrewer) + +register_gradient_colors(:PiYG, [ + RGB(142/255, 1/255, 82/255), + RGB(197/255, 27/255, 125/255), + RGB(222/255, 119/255, 174/255), + RGB(241/255, 182/255, 218/255), + RGB(253/255, 224/255, 239/255), + RGB(247/255, 247/255, 247/255), + RGB(230/255, 245/255, 208/255), + RGB(184/255, 225/255, 134/255), + RGB(127/255, 188/255, 65/255), + RGB(77/255, 146/255, 33/255), + RGB(39/255, 100/255, 25/255)], :colorbrewer) + +register_gradient_colors(:PRGn, [ + RGB(64/255, 0/255, 75/255), + RGB(118/255, 42/255, 131/255), + RGB(153/255, 112/255, 171/255), + RGB(194/255, 165/255, 207/255), + RGB(231/255, 212/255, 232/255), + RGB(247/255, 247/255, 247/255), + RGB(217/255, 240/255, 211/255), + RGB(166/255, 219/255, 160/255), + RGB(90/255, 174/255, 97/255), + RGB(27/255, 120/255, 55/255), + RGB(0/255, 68/255, 27/255)], :colorbrewer) + +register_gradient_colors(:RdYlBu, [ + RGB(165/255, 0/255, 38/255), + RGB(215/255, 48/255, 39/255), + RGB(244/255, 109/255, 67/255), + RGB(253/255, 174/255, 97/255), + RGB(254/255, 224/255, 144/255), + RGB(255/255, 255/255, 191/255), + RGB(224/255, 243/255, 248/255), + RGB(171/255, 217/255, 233/255), + RGB(116/255, 173/255, 209/255), + RGB(69/255, 117/255, 180/255), + RGB(49/255, 54/255, 149/255)], :colorbrewer) + +register_gradient_colors(:BrBG, [ + RGB(84/255, 48/255, 5/255), + RGB(140/255, 81/255, 10/255), + RGB(191/255, 129/255, 45/255), + RGB(223/255, 194/255, 125/255), + RGB(246/255, 232/255, 195/255), + RGB(245/255, 245/255, 245/255), + RGB(199/255, 234/255, 229/255), + RGB(128/255, 205/255, 193/255), + RGB(53/255, 151/255, 143/255), + RGB(1/255, 102/255, 94/255), + RGB(0/255, 60/255, 48/255)], :colorbrewer) + +register_gradient_colors(:RdGy, [ + RGB(103/255, 0/255, 31/255), + RGB(178/255, 24/255, 43/255), + RGB(214/255, 96/255, 77/255), + RGB(244/255, 165/255, 130/255), + RGB(253/255, 219/255, 199/255), + RGB(255/255, 255/255, 255/255), + RGB(224/255, 224/255, 224/255), + RGB(186/255, 186/255, 186/255), + RGB(135/255, 135/255, 135/255), + 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, [ +# RGB(102/255, 194/255, 165/255), +# RGB(252/255, 141/255, 98/255), +# RGB(141/255, 160/255, 203/255), +# RGB(231/255, 138/255, 195/255), +# RGB(166/255, 216/255, 84/255), +# RGB(255/255, 217/255, 47/255), +# RGB(229/255, 196/255, 148/255), +# RGB(179/255, 179/255, 179/255)], :colorbrewer) +# +# register_gradient_colors(:Accent, [ +# RGB(127/255, 201/255, 127/255), +# RGB(190/255, 174/255, 212/255), +# RGB(253/255, 192/255, 134/255), +# RGB(255/255, 255/255, 153/255), +# RGB(56/255, 108/255, 176/255), +# RGB(240/255, 2/255, 127/255), +# RGB(191/255, 91/255, 23/255), +# RGB(102/255, 102/255, 102/255)], :colorbrewer) +# +# register_gradient_colors(:Set1, [ +# RGB(228/255, 26/255, 28/255), +# RGB(55/255, 126/255, 184/255), +# RGB(77/255, 175/255, 74/255), +# RGB(152/255, 78/255, 163/255), +# RGB(255/255, 127/255, 0/255), +# RGB(255/255, 255/255, 51/255), +# RGB(166/255, 86/255, 40/255), +# RGB(247/255, 129/255, 191/255), +# RGB(153/255, 153/255, 153/255)], :colorbrewer) +# +# register_gradient_colors(:Set3, [ +# RGB(141/255, 211/255, 199/255), +# RGB(255/255, 255/255, 179/255), +# RGB(190/255, 186/255, 218/255), +# RGB(251/255, 128/255, 114/255), +# RGB(128/255, 177/255, 211/255), +# RGB(253/255, 180/255, 98/255), +# RGB(179/255, 222/255, 105/255), +# RGB(252/255, 205/255, 229/255), +# RGB(217/255, 217/255, 217/255), +# RGB(188/255, 128/255, 189/255), +# RGB(204/255, 235/255, 197/255), +# RGB(255/255, 237/255, 111/255)], :colorbrewer) +# +# register_gradient_colors(:Dark2, [ +# RGB(27/255, 158/255, 119/255), +# RGB(217/255, 95/255, 2/255), +# RGB(117/255, 112/255, 179/255), +# RGB(231/255, 41/255, 138/255), +# RGB(102/255, 166/255, 30/255), +# RGB(230/255, 171/255, 2/255), +# RGB(166/255, 118/255, 29/255), +# RGB(102/255, 102/255, 102/255)], :colorbrewer) +# +# register_gradient_colors(:Paired, [ +# RGB(166/255, 206/255, 227/255), +# RGB(31/255, 120/255, 180/255), +# RGB(178/255, 223/255, 138/255), +# RGB(51/255, 160/255, 44/255), +# RGB(251/255, 154/255, 153/255), +# RGB(227/255, 26/255, 28/255), +# RGB(253/255, 191/255, 111/255), +# RGB(255/255, 127/255, 0/255), +# RGB(202/255, 178/255, 214/255), +# RGB(106/255, 61/255, 154/255), +# RGB(255/255, 255/255, 153/255), +# RGB(177/255, 89/255, 40/255)], :colorbrewer) +# +# register_gradient_colors(:Pastel2, [ +# RGB(179/255, 226/255, 205/255), +# RGB(253/255, 205/255, 172/255), +# RGB(203/255, 213/255, 232/255), +# RGB(244/255, 202/255, 228/255), +# RGB(230/255, 245/255, 201/255), +# RGB(255/255, 242/255, 174/255), +# RGB(241/255, 226/255, 204/255), +# RGB(204/255, 204/255, 204/255)], :colorbrewer) +# +# register_gradient_colors(:Pastel1, [ +# RGB(251/255, 180/255, 174/255), +# RGB(179/255, 205/255, 227/255), +# RGB(204/255, 235/255, 197/255), +# RGB(222/255, 203/255, 228/255), +# RGB(254/255, 217/255, 166/255), +# RGB(255/255, 255/255, 204/255), +# RGB(229/255, 216/255, 189/255), +# RGB(253/255, 218/255, 236/255), +# RGB(242/255, 242/255, 242/255)], :colorbrewer) + +register_gradient_colors(:OrRd, [ + RGB(255/255, 247/255, 236/255), + RGB(254/255, 232/255, 200/255), + RGB(253/255, 212/255, 158/255), + RGB(253/255, 187/255, 132/255), + RGB(252/255, 141/255, 89/255), + RGB(239/255, 101/255, 72/255), + RGB(215/255, 48/255, 31/255), + RGB(179/255, 0/255, 0/255), + RGB(127/255, 0/255, 0/255)], :colorbrewer) + +register_gradient_colors(:PuBu, [ + RGB(255/255, 247/255, 251/255), + RGB(236/255, 231/255, 242/255), + RGB(208/255, 209/255, 230/255), + RGB(166/255, 189/255, 219/255), + RGB(116/255, 169/255, 207/255), + RGB(54/255, 144/255, 192/255), + RGB(5/255, 112/255, 176/255), + RGB(4/255, 90/255, 141/255), + RGB(2/255, 56/255, 88/255)], :colorbrewer) + +register_gradient_colors(:BuPu, [ + RGB(247/255, 252/255, 253/255), + RGB(224/255, 236/255, 244/255), + RGB(191/255, 211/255, 230/255), + RGB(158/255, 188/255, 218/255), + RGB(140/255, 150/255, 198/255), + RGB(140/255, 107/255, 177/255), + RGB(136/255, 65/255, 157/255), + RGB(129/255, 15/255, 124/255), + RGB(77/255, 0/255, 75/255)], :colorbrewer) + +register_gradient_colors(:Oranges, [ + RGB(255/255, 245/255, 235/255), + RGB(254/255, 230/255, 206/255), + RGB(253/255, 208/255, 162/255), + RGB(253/255, 174/255, 107/255), + RGB(253/255, 141/255, 60/255), + RGB(241/255, 105/255, 19/255), + RGB(217/255, 72/255, 1/255), + RGB(166/255, 54/255, 3/255), + RGB(127/255, 39/255, 4/255)], :colorbrewer) + +register_gradient_colors(:BuGn, [ + RGB(247/255, 252/255, 253/255), + RGB(229/255, 245/255, 249/255), + RGB(204/255, 236/255, 230/255), + RGB(153/255, 216/255, 201/255), + RGB(102/255, 194/255, 164/255), + RGB(65/255, 174/255, 118/255), + RGB(35/255, 139/255, 69/255), + RGB(0/255, 109/255, 44/255), + RGB(0/255, 68/255, 27/255)], :colorbrewer) + +register_gradient_colors(:YlOrBr, [ + RGB(255/255, 255/255, 229/255), + RGB(255/255, 247/255, 188/255), + RGB(254/255, 227/255, 145/255), + RGB(254/255, 196/255, 79/255), + RGB(254/255, 153/255, 41/255), + RGB(236/255, 112/255, 20/255), + RGB(204/255, 76/255, 2/255), + RGB(153/255, 52/255, 4/255), + RGB(102/255, 37/255, 6/255)], :colorbrewer) + +register_gradient_colors(:YlGn, [ + RGB(255/255, 255/255, 229/255), + RGB(247/255, 252/255, 185/255), + RGB(217/255, 240/255, 163/255), + RGB(173/255, 221/255, 142/255), + RGB(120/255, 198/255, 121/255), + RGB(65/255, 171/255, 93/255), + RGB(35/255, 132/255, 67/255), + RGB(0/255, 104/255, 55/255), + RGB(0/255, 69/255, 41/255)], :colorbrewer) + +register_gradient_colors(:Reds, [ + RGB(255/255, 245/255, 240/255), + RGB(254/255, 224/255, 210/255), + RGB(252/255, 187/255, 161/255), + RGB(252/255, 146/255, 114/255), + RGB(251/255, 106/255, 74/255), + RGB(239/255, 59/255, 44/255), + RGB(203/255, 24/255, 29/255), + RGB(165/255, 15/255, 21/255), + RGB(103/255, 0/255, 13/255)], :colorbrewer) + +register_gradient_colors(:RdPu, [ + RGB(255/255, 247/255, 243/255), + RGB(253/255, 224/255, 221/255), + RGB(252/255, 197/255, 192/255), + RGB(250/255, 159/255, 181/255), + RGB(247/255, 104/255, 161/255), + RGB(221/255, 52/255, 151/255), + RGB(174/255, 1/255, 126/255), + RGB(122/255, 1/255, 119/255), + RGB(73/255, 0/255, 106/255)], :colorbrewer) + +register_gradient_colors(:Greens, [ + RGB(247/255, 252/255, 245/255), + RGB(229/255, 245/255, 224/255), + RGB(199/255, 233/255, 192/255), + RGB(161/255, 217/255, 155/255), + RGB(116/255, 196/255, 118/255), + RGB(65/255, 171/255, 93/255), + RGB(35/255, 139/255, 69/255), + RGB(0/255, 109/255, 44/255), + RGB(0/255, 68/255, 27/255)], :colorbrewer) + +register_gradient_colors(:YlGnBu, [ + RGB(255/255, 255/255, 217/255), + RGB(237/255, 248/255, 177/255), + RGB(199/255, 233/255, 180/255), + RGB(127/255, 205/255, 187/255), + RGB(65/255, 182/255, 196/255), + RGB(29/255, 145/255, 192/255), + RGB(34/255, 94/255, 168/255), + RGB(37/255, 52/255, 148/255), + RGB(8/255, 29/255, 88/255)], :colorbrewer) + +register_gradient_colors(:Purples, [ + RGB(252/255, 251/255, 253/255), + RGB(239/255, 237/255, 245/255), + RGB(218/255, 218/255, 235/255), + RGB(188/255, 189/255, 220/255), + RGB(158/255, 154/255, 200/255), + RGB(128/255, 125/255, 186/255), + RGB(106/255, 81/255, 163/255), + RGB(84/255, 39/255, 143/255), + RGB(63/255, 0/255, 125/255)], :colorbrewer) + +register_gradient_colors(:GnBu, [ + RGB(247/255, 252/255, 240/255), + RGB(224/255, 243/255, 219/255), + RGB(204/255, 235/255, 197/255), + RGB(168/255, 221/255, 181/255), + RGB(123/255, 204/255, 196/255), + RGB(78/255, 179/255, 211/255), + RGB(43/255, 140/255, 190/255), + RGB(8/255, 104/255, 172/255), + RGB(8/255, 64/255, 129/255)], :colorbrewer) + +register_gradient_colors(:Greys, [ + RGB(255/255, 255/255, 255/255), + RGB(240/255, 240/255, 240/255), + RGB(217/255, 217/255, 217/255), + RGB(189/255, 189/255, 189/255), + RGB(150/255, 150/255, 150/255), + RGB(115/255, 115/255, 115/255), + RGB(82/255, 82/255, 82/255), + RGB(37/255, 37/255, 37/255), + RGB(0/255, 0/255, 0/255)], :colorbrewer) + +register_gradient_colors(:YlOrRd, [ + RGB(255/255, 255/255, 204/255), + RGB(255/255, 237/255, 160/255), + RGB(254/255, 217/255, 118/255), + RGB(254/255, 178/255, 76/255), + RGB(253/255, 141/255, 60/255), + RGB(252/255, 78/255, 42/255), + RGB(227/255, 26/255, 28/255), + RGB(177/255, 0/255, 38/255)], :colorbrewer) + +register_gradient_colors(:PuRd, [ + RGB(247/255, 244/255, 249/255), + RGB(231/255, 225/255, 239/255), + RGB(212/255, 185/255, 218/255), + RGB(201/255, 148/255, 199/255), + RGB(223/255, 101/255, 176/255), + RGB(231/255, 41/255, 138/255), + RGB(206/255, 18/255, 86/255), + RGB(152/255, 0/255, 67/255), + RGB(103/255, 0/255, 31/255)], :colorbrewer) + +register_gradient_colors(:Blues, [ + RGB(247/255, 251/255, 255/255), + RGB(222/255, 235/255, 247/255), + RGB(198/255, 219/255, 239/255), + RGB(158/255, 202/255, 225/255), + RGB(107/255, 174/255, 214/255), + RGB(66/255, 146/255, 198/255), + RGB(33/255, 113/255, 181/255), + RGB(8/255, 81/255, 156/255), + RGB(8/255, 48/255, 107/255)], :colorbrewer) + +register_gradient_colors(:PuBuGn, [ + RGB(255/255, 247/255, 251/255), + RGB(236/255, 226/255, 240/255), + RGB(208/255, 209/255, 230/255), + RGB(166/255, 189/255, 219/255), + RGB(103/255, 169/255, 207/255), + RGB(54/255, 144/255, 192/255), + RGB(2/255, 129/255, 138/255), + RGB(1/255, 108/255, 89/255), + RGB(1/255, 70/255, 54/255)], :colorbrewer) diff --git a/src/gradients/matplotlib.jl b/src/gradients/matplotlib.jl index 0decc3e..c1cb53b 100644 --- a/src/gradients/matplotlib.jl +++ b/src/gradients/matplotlib.jl @@ -21,6 +21,8 @@ # 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_gradient_colors(:magma, sample_evenly([ RGB(0.001462, 0.000466, 0.013866), RGB(0.002258, 0.001295, 0.018331), @@ -278,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)) +], 30), :matplotlib) register_gradient_colors(:inferno, sample_evenly([ RGB(0.001462, 0.000466, 0.013866), @@ -537,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)) +], 30), :matplotlib) register_gradient_colors(:plasma, sample_evenly([ RGB(0.050383, 0.029803, 0.527975), @@ -796,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)) +], 30), :matplotlib) register_gradient_colors(:viridis, sample_evenly([ RGB(0.267004, 0.004874, 0.329415), @@ -1055,7 +1057,7 @@ 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)) +], 30), :matplotlib) # end of matplotlib colormaps From c3e1e7d426210e474066c99836f7d718ae1ad75a Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Tue, 21 Feb 2017 21:29:12 +0100 Subject: [PATCH 2/9] get a clibrary by symbol --- src/color_gradients.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color_gradients.jl b/src/color_gradients.jl index af8a9ab..20d7965 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -61,7 +61,7 @@ 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)], ", ")) - grad + color_libraries[grad] end const _rainbowColors = [colorant"purple", colorant"blue", colorant"green", colorant"orange", colorant"red"] From e3c40d6cf3ea4f4ee21b2c8e518e4ff8bdd815e1 Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Tue, 21 Feb 2017 22:49:46 +0100 Subject: [PATCH 3/9] allow passing a ColorLibrary (not just the name) to cgrad --- src/color_gradients.jl | 17 +++++++++-------- src/colors.jl | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/color_gradients.jl b/src/color_gradients.jl index 20d7965..95cc0fb 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -152,17 +152,18 @@ function cgrad_reverse(s::Symbol) end end -function iscgrad_symbol(s::Symbol; color_library::Symbol = _gradients[1]) +function iscgrad_symbol(s::Symbol; color_library = _gradients[1]) rev, s = cgrad_reverse(s) - haskey(color_libraries[color_library].lib,s) && return true - haskey(color_libraries[color_library].defaults,s) && return true + lib = isa(color_library, Symbol) ? color_libraries[color_library] : color_library + 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; color_library::Symbol = _gradients[1]) +function cgrad_colors(s::Symbol; color_library = _gradients[1]) rev, s = cgrad_reverse(s) if rev reverse(getgradient(s, color_library)) @@ -175,11 +176,11 @@ 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; color_library::Symbol = _gradients[1]) +function _color_list(arg, ::Void; color_library = _gradients[1]) cgrad_colors(arg; color_library = color_library) end -function _color_list(arg, alpha; color_library::Symbol = _gradients[1]) +function _color_list(arg, alpha; color_library = _gradients[1]) colors = cgrad_colors(arg; color_library = color_library) for i in eachindex(colors) colors[i] = RGBA{Float64}(convert(RGB{Float64}, colors[i]), alpha) @@ -188,7 +189,7 @@ function _color_list(arg, alpha; color_library::Symbol = _gradients[1]) end # construct a ColorGradient when given explicit values -function cgrad(arg, values; alpha = nothing, color_library::Symbol = _gradients[1]) +function cgrad(arg, values; alpha = nothing, color_library = _gradients[1]) colors = _color_list(arg, alpha; color_library = color_library) values = if length(colors) == length(values) && values[1] == 0 && values[end] == 1 values @@ -206,7 +207,7 @@ function cgrad(arg, values; alpha = nothing, color_library::Symbol = _gradients[ end # construct a ColorGradient automatically -function cgrad(arg; alpha = nothing, scale = :identity, color_library::Symbol = _gradients[1]) +function cgrad(arg; alpha = nothing, scale = :identity, color_library = _gradients[1]) colors = _color_list(arg, alpha, color_library = color_library) values = if scale in (:log, :log10) log10(linspace(1,10,30)) diff --git a/src/colors.jl b/src/colors.jl index 174323d..ce6bca9 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -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::Symbol = _gradients[1]) = (iscgrad_symbol(s, color_library = color_library) ? cgrad(s) : 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(b::Bool) = b ? error("plot_color(true) not allowed.") : invisible() plot_color(::Void) = invisible() plot_color(c::Colorant) = convert(RGBA{Float64}, c) @@ -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::Symbol = _gradients[1]) = (iscgrad_symbol(s, color_library = color_library) ? cgrad(s, alpha=α) : RGBA{Float64}(convert(RGB, plot_color(s)), α)) +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(grad::ColorGradient, α::Number) = cgrad(grad, alpha=α) function plot_color(cs::AbstractArray) From bda5c54322bbb335520240a4832b8c20aa3acc40 Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Thu, 23 Feb 2017 14:30:02 +0100 Subject: [PATCH 4/9] fix cgrad_colors for Vector of Symbols --- src/color_gradients.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/color_gradients.jl b/src/color_gradients.jl index 95cc0fb..6130a6c 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -174,7 +174,7 @@ 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] +cgrad_colors(cs::AbstractVector; color_library = _gradients[1]) = RGBA{Float64}[plot_color(c; color_library = color_library) for c in cs] function _color_list(arg, ::Void; color_library = _gradients[1]) cgrad_colors(arg; color_library = color_library) From f923e67ee7f4099911e6dfa058bd6bdea2ae2a2c Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Sun, 26 Feb 2017 17:32:59 +0100 Subject: [PATCH 5/9] add symbol argument to cgraddefaults --- src/color_gradients.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/color_gradients.jl b/src/color_gradients.jl index 6130a6c..026d019 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -12,6 +12,8 @@ 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...) + function cgraddefaults(cl::ColorLibrary; default = nothing, sequential = nothing, diverging = nothing) default == nothing || (cl.defaults[:default] = default) sequential == nothing || (cl.defaults[:sequential] = sequential) @@ -45,7 +47,7 @@ function register_gradient_colors{C<:Colorant}(name::Symbol, colors::AbstractVec color_libraries[color_library].lib[name] = colors end -function register_color_library(name::Symbol, color_library::ColorLibrary) +function register_color_library(name::Symbol, color_library::ColorLibrary = ColorLibrary()) color_libraries[name] = color_library end From cc1a37a2da4506711fcd8a04b7e130f702b647b9 Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Mon, 27 Feb 2017 21:42:44 +0100 Subject: [PATCH 6/9] Simplify by disallowing directly passing color library --- src/PlotUtils.jl | 3 +-- src/color_gradients.jl | 49 +++++++++++++++++-------------------- src/gradients/matplotlib.jl | 14 +++++------ 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/PlotUtils.jl b/src/PlotUtils.jl index db0911a..2093728 100644 --- a/src/PlotUtils.jl +++ b/src/PlotUtils.jl @@ -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") diff --git a/src/color_gradients.jl b/src/color_gradients.jl index 026d019..47deaec 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -12,9 +12,9 @@ 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) @@ -22,9 +22,9 @@ 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 @@ -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 => :heat), Dict( :blues => [colorant"lightblue", colorant"darkblue"], :reds => [colorant"lightpink", colorant"darkred"], :greens => [colorant"lightgreen", colorant"darkgreen"], @@ -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() @@ -156,7 +151,7 @@ end function iscgrad_symbol(s::Symbol; color_library = _gradients[1]) 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) @@ -165,25 +160,25 @@ function iscgrad_symbol(s::Symbol; color_library = _gradients[1]) return false end -function cgrad_colors(s::Symbol; color_library = _gradients[1]) +function cgrad_colors(s::Symbol) rev, s = cgrad_reverse(s) if rev - reverse(getgradient(s, color_library)) + reverse(getgradient(s)) else - getgradient(s, color_library) + getgradient(s) end 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 @@ -191,8 +186,8 @@ function _color_list(arg, alpha; color_library = _gradients[1]) end # 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 @@ -209,8 +204,8 @@ function cgrad(arg, values; alpha = nothing, color_library = _gradients[1]) end # 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 diff --git a/src/gradients/matplotlib.jl b/src/gradients/matplotlib.jl index c1cb53b..7d47f0b 100644 --- a/src/gradients/matplotlib.jl +++ b/src/gradients/matplotlib.jl @@ -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 / @@ -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))) register_gradient_colors(:magma, sample_evenly([ RGB(0.001462, 0.000466, 0.013866), @@ -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), @@ -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), @@ -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), @@ -1057,9 +1057,9 @@ 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 +# end of Plots colormaps # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- From b04b8152c2a347f6752b7cd2d09916e910d4fb3d Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Mon, 27 Feb 2017 22:45:30 +0100 Subject: [PATCH 7/9] allow calling cgrad with colorlibrary --- src/color_gradients.jl | 14 ++++++++++---- src/colors.jl | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/color_gradients.jl b/src/color_gradients.jl index 47deaec..94f5669 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -149,7 +149,7 @@ 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 = color_libraries[_gradients[1]] haskey(lib.lib,s) && return true @@ -160,12 +160,12 @@ function iscgrad_symbol(s::Symbol; color_library = _gradients[1]) 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(getgradient(s)) + reverse(getgradient(s, color_library)) else - getgradient(s) + getgradient(s, color_library) end end @@ -185,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) @@ -203,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) @@ -224,6 +228,8 @@ function cgrad(arg; alpha = nothing, scale = :identity) ColorGradient(colors, values) end + + # the default gradient cgrad(; kw...) = cgrad(:default; kw...) diff --git a/src/colors.jl b/src/colors.jl index ce6bca9..b65291d 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -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) @@ -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) From 5175ee84c45c2a01265232bb2c86c29ecc896bb7 Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Tue, 28 Feb 2017 12:43:24 +0100 Subject: [PATCH 8/9] make sure all libraries have a diverging type --- src/color_gradients.jl | 2 +- src/gradients/colorbrewer.jl | 12 ------------ src/gradients/matplotlib.jl | 16 +++++++++++++++- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/color_gradients.jl b/src/color_gradients.jl index 94f5669..e95e370 100644 --- a/src/color_gradients.jl +++ b/src/color_gradients.jl @@ -65,7 +65,7 @@ const _rainbowColors = [colorant"purple", colorant"blue", colorant"green", color const _testColors = [colorant"darkblue", colorant"blueviolet", colorant"darkcyan",colorant"green", darken(colorant"yellow",0.3), colorant"orange", darken(colorant"red",0.2)] -const misc = 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"], diff --git a/src/gradients/colorbrewer.jl b/src/gradients/colorbrewer.jl index 58039ea..a90783f 100644 --- a/src/gradients/colorbrewer.jl +++ b/src/gradients/colorbrewer.jl @@ -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, [ diff --git a/src/gradients/matplotlib.jl b/src/gradients/matplotlib.jl index 7d47f0b..7dc0f93 100644 --- a/src/gradients/matplotlib.jl +++ b/src/gradients/matplotlib.jl @@ -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(:Plots, 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), @@ -1060,6 +1060,20 @@ register_gradient_colors(:viridis, sample_evenly([ ], 30), :Plots) + +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 # ---------------------------------------------------------------------- # ---------------------------------------------------------------------- From c13ecc06b413774dcaba9b0b511b68b33e6575a5 Mon Sep 17 00:00:00 2001 From: "Michael K. Borregaard" Date: Tue, 28 Feb 2017 21:36:58 +0100 Subject: [PATCH 9/9] change name to pu_or --- src/gradients/colorbrewer.jl | 14 ++++++++++++++ src/gradients/matplotlib.jl | 24 ++++++++++++------------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/gradients/colorbrewer.jl b/src/gradients/colorbrewer.jl index a90783f..6b7919f 100644 --- a/src/gradients/colorbrewer.jl +++ b/src/gradients/colorbrewer.jl @@ -224,6 +224,20 @@ register_gradient_colors(:RdGy, [ # RGB(253/255, 218/255, 236/255), # RGB(242/255, 242/255, 242/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) + + register_gradient_colors(:OrRd, [ RGB(255/255, 247/255, 236/255), RGB(254/255, 232/255, 200/255), diff --git a/src/gradients/matplotlib.jl b/src/gradients/matplotlib.jl index 7dc0f93..3afa161 100644 --- a/src/gradients/matplotlib.jl +++ b/src/gradients/matplotlib.jl @@ -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(:Plots, ColorLibrary(Dict(:default => :sequential, :sequential => :inferno, :diverging => :PuOr))) +register_color_library(:Plots, ColorLibrary(Dict(:default => :sequential, :sequential => :inferno, :diverging => :pu_or))) register_gradient_colors(:magma, sample_evenly([ RGB(0.001462, 0.000466, 0.013866), @@ -1061,18 +1061,18 @@ register_gradient_colors(:viridis, sample_evenly([ -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), +register_gradient_colors(:pu_or, [ + RGB(45/255, 0/255, 75/255), RGB(84/255, 39/255, 136/255), - RGB(45/255, 0/255, 75/255)], :Plots) + RGB(128/255, 115/255, 172/255), + RGB(178/255, 171/255, 210/255), + RGB(216/255, 218/255, 235/255), + RGB(247/255, 247/255, 247/255), + RGB(254/255, 224/255, 182/255), + RGB(253/255, 184/255, 99/255), + RGB(224/255, 130/255, 20/255), + RGB(179/255, 88/255, 6/255), + RGB(127/255, 59/255, 8/255)], :Plots) # end of Plots colormaps # ----------------------------------------------------------------------