JuliaGraphics / ColorSchemes.jl

colorschemes, colormaps, gradients, and palettes
http://juliagraphics.github.io/ColorSchemes.jl/
Other
187 stars 33 forks source link

Feature request: Resample Gradient #128

Closed alex-s-gardner closed 2 days ago

alex-s-gardner commented 3 weeks ago

It would be nice to have a function within ColorSchemes to convert between a Gradient.

To Gradient a ColorSchemes gradient you need a function from another package: either 'PlotUtils.palette' or 'Makie.resample_cmap'

It seems that such a basic functionality should live within ColorSchemes

cormullion commented 3 weeks ago

Hi! What exactly do those functions (PlotUtils.palette Makie.resample_cmap) do?

alex-s-gardner commented 3 weeks ago

They simply discretize a color gradient with a requested number of samplings .

I often find myself wanting to create a new color palette of n colors from a gradient. Does that make sense?

alex-s-gardner commented 3 weeks ago

For example:

the desired function f could create :acton10:

acton10 = f(:acton, 10)

but also any other integer number of samples

cormullion commented 3 weeks ago

This is sort of built in:

using ColorSchemes

acton10 = ColorScheme(
    get(ColorSchemes.acton, 0.0:0.1:1.0))

get allows various kinds of sampling; the ColorScheme constructor converts the vector of colors to a ColorScheme.

alex-s-gardner commented 3 weeks ago

Ah man... I wish I had found get a long time ago.. didn't think of using ranges. I wonder if it's worth including this example under basic usage

I do think a helper function would still be welcome:

function resample(colorscheme, n)

  newscheme = ColorScheme(
      get(ColorSchemes[colorscheme], (0:n)/n))

end
cormullion commented 2 weeks ago

I think it would be a good idea to add more information to the docstrings and documentation, particularly if it's not that easy to find at the moment. Perhaps using words like "resample" might help discoverability....

cormullion commented 2 weeks ago

I added some more words to the documentation.

alex-s-gardner commented 2 weeks ago

Looks good, thanks. Would you be oppose to adding a resample function? For people that are newer to the language:

get(ColorSchemes.darkrainbow, range(0.0, 1.0, length=20)) |> ColorScheme

can be a bit intimidating to resample a colormap. I think a resample function is simple, robust and intuitive. I'd be happy to make a PR.