JuliaGraphics / ColorSchemeTools.jl

tools for working with color schemes, color maps, gradients

Home Page:https://juliagraphics.github.io/ColorSchemeTools.jl/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

unordered indexed list of triples fails to construct real colors

haakon-e opened this issue · comments

On ColorSchemeTools@1.3.0, indexedlist is assumed to be sorted.

julia> indexedlist = ((0.0, (0.6470, 0.0, 0.1490)), (0.4, (0.9929, 0.7215, 0.4172)), (1.0, (0.4549, 0.6784, 0.8196)))

julia> ColorSchemeTools.get_indexed_list_color(reverse(indexedlist), 0.2)
(1.172233, 0.735867, 0.283067)  # components not constrained to [0,1]

this also means that constructing colormaps is silently incorrect:

julia> make_colorscheme(reverse(indexedlist), length=4).colors
4-element Array{RGB{Float64},1} with eltype ColorTypes.RGB{Float64}:
 RGB{Float64}(1.351567,0.750233,0.148933)
 RGB{Float64}(1.052678,0.726289,0.372489)
 RGB{Float64}(0.753789,0.702344,0.596044)
 RGB{Float64}(0.4549,0.6784,0.8196)

vaguely related to this, I've also noticed that attempting to construct a colormap if the endpoints (0, 1) aren't specified in indexedlist results in a generic error. An argument check and more useful error message may be more useful in this case:

julia> make_colorscheme(indexedlist[1:2], length=4).colors
ERROR: MethodError: no method matching isless(::Nothing, ::Int64)
Closest candidates are:
[...]

Good find. Could either specify that the list should be pre-sorted, or insert a sort operation. Since you seem to use indexedlists (I don't know where they're commonly used), which do you think would be best?

I found this in context of trying to create colormaps from sets of colors with unequal spacing (see MakieOrg/Makie.jl#2485). In that suggestion, I ended up using cgrad from PlotUtils.jl in order to not add this package as a dependency (PlotUtils.jl actually seems to have a similar bug in that they do sort the indices -- but not the corresponding colors: link).

Anyways -- I think inserting a sort-operation is probably the easiest, but I'd also be happy if it just throws an error.

hopefully fixed in 1.4