Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
fix to_colormap
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Feb 18, 2020
1 parent ed06de8 commit f7d593f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
27 changes: 15 additions & 12 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -667,27 +667,31 @@ struct Reverse{T}
data::T
end

function convert_attribute(r::Reverse, ::key"colormap", n::Integer = 20)
function convert_attribute(r::Reverse, ::key"colormap", n::Integer=20)
reverse(to_colormap(r.data, n))
end

function convert_attribute(cs::ColorScheme, ::key"colormap", n::Integer = 20)
function convert_attribute(cs::ColorScheme, ::key"colormap", n::Integer=20)
return to_colormap(cs.colors, n)
end


"""
to_colormap(b, x)
An `AbstractVector{T}` with any object that [`to_color`](@ref) accepts.
"""
convert_attribute(cm::AbstractVector, ::key"colormap") = to_color.(cm)
convert_attribute(cm::AbstractVector, ::key"colormap", n::Int=length(cm)) = to_colormap(to_color.(cm), n)

function convert_attribute(cm::AbstractVector{<: Colorant}, ::key"colormap", n::Int=length(cm))
colormap = length(cm) == n ? cm : resample(cm, n)
return el32convert(colormap)
end

"""
Tuple(A, B) or Pair{A, B} with any object that [`to_color`](@ref) accepts
"""
function convert_attribute(cs::Union{Tuple, Pair}, ::key"colormap")
[to_color.(cs)...]
function convert_attribute(cs::Union{Tuple, Pair}, ::key"colormap", n::Int=2)
return to_colormap([to_color.(cs)...], n)
end

to_colormap(x, n::Integer) = convert_attribute(x, key"colormap"(), n)
Expand All @@ -696,23 +700,22 @@ to_colormap(x, n::Integer) = convert_attribute(x, key"colormap"(), n)
A Symbol/String naming the gradient. For more on what names are available please see: `available_gradients()`.
For now, we support gradients from `PlotUtils` natively.
"""
function convert_attribute(cs::Union{String, Symbol}, ::key"colormap", n::Integer = 20)
function convert_attribute(cs::Union{String, Symbol}, ::key"colormap", n::Integer=20)
cs_string = string(cs)

if cs_string in all_gradient_names
if cs_string in colorbrewer_8color_names # special handling for 8 color only
return resample(ColorBrewer.palette(cs_string, 8), n)
return to_colormap(ColorBrewer.palette(cs_string, 8), n)
else # cs_string must be in plotutils_names
return PlotUtils.cvec(Symbol(cs), n) .|> color .|> x -> convert(RGB{FixedPointNumbers.Normed{UInt8,8}}, x)
return RGBf0.(PlotUtils.cvec(Symbol(cs), n))
end
else
error("There is no color gradient named: $cs")
end
end

function AbstractPlotting.convert_attribute(cg::PlotUtils.ColorGradient, ::key"colormap", n::Integer = 30)
function AbstractPlotting.convert_attribute(cg::PlotUtils.ColorGradient, ::key"colormap", n::Integer=length(cg.values))
# PlotUtils does not always give [0, 1] range, so we adapt to what it has
return getindex.(Ref(cg), LinRange(first(c.values), last(c.values), n)) # workaround until PlotUtils tags a release
return getindex.(Ref(cg), LinRange(first(cg.values), last(cg.values), n)) # workaround until PlotUtils tags a release
# TODO change this once PlotUtils supports collections of indices
end

Expand Down
1 change: 0 additions & 1 deletion src/layouting/boundingbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ function boundingbox(x::Text, text::String)
FRect3D(minimum(bb) .- aoffset, whp)
end


boundingbox(x::Text) = boundingbox(x, to_value(x[1]))

function boundingbox(
Expand Down

0 comments on commit f7d593f

Please sign in to comment.