JuliaGraphics / ColorSchemes.jl

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

How to control the center color of diverging colorschemes #116

Closed briochemc closed 9 months ago

briochemc commented 9 months ago

I would like to be able to slightly modify diverging color schemes that have a "center" color that is not exactly white in such a way that said center becomes exactly white. I can think of different ways to do it manually, like below (note the slight difference in the middle):

plot_203 plot_202

done via:

# ColorSchemes.RdBu but with a true white 
reds = ColorSchemes.RdBu[1:end÷2]
blues = ColorSchemes.RdBu[end÷2+2:end]
colormap = ColorScheme(reds) * ColorScheme([colorant"white"]) * ColorScheme(blues)

but I'm sure I'm not doing it wrong, so I'm fishing for the correct way to do that thing above.

cormullion commented 9 months ago

Hi! I don't have any insight in any "correct" way of working with colorschemes - perhaps there are colour specialists who have better insight, or perhaps there are best practices current in other language's color libraries - I wouldn't know myself.

As you say, it doesn't seem to difficult to modify them manually:

using ColorSchemes, Colors

cs = deepcopy(ColorSchemes.RdBu)
cs.colors[6] = colorant"green" # for testing :)
Screenshot 2023-11-27 at 11 47 19

This one (RdBu) is from ColorBrewer, so perhaps you can locate the method they used to build this scheme and modify it...

To build color schemes in a more programmatic way, you could look at ColorSchemeTools.jl but I don't know the method for making this particular one.

briochemc commented 9 months ago

Oh I did not mean to go as deep as adapting the code that generated the original colors! I was merely fishing for things like you just wrote, which looks much simpler than mine. For the record, I think I will be using a function like below from now on

function withwhitecenter(cs)
    cs = deepcopy(cs)
    cs.colors[end÷2+1] = colorant"white"
    return cs
end