Skip to content

Commit

Permalink
allow calling cgrad with colorlibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
mkborregaard committed Feb 27, 2017
1 parent cc1a37a commit b04b815
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/color_gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -224,6 +228,8 @@ function cgrad(arg; alpha = nothing, scale = :identity)
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

0 comments on commit b04b815

Please sign in to comment.