Define a color function based on color samples obtained from ColorBrewer

I am quite new to Mathematica and I am searching for a way to create a smooth colormap based on the samples given by ColorBrewer. Is it possible to create a interpolated function from those sample points?

• By samples, do you mean color samples, i.e, color schemes?
– VLC
Nov 16 '12 at 13:47
• yes, as you can see on the website there are only color samples (e.g. 6 rgb values) Nov 16 '12 at 13:48
• Look up Blend[]... Nov 16 '12 at 13:54
• How do you want to use your color map in Mathematica? How you incorporate your six RGB values into Blend will depend on you intended use. Nov 16 '12 at 15:30

I enter RGB values in a matrix using Ctrl+, and Ctrl+Enter (tutorial):

{{237, 248, 177}, {127, 205, 187}, {44, 127, 184}}


Then create a color function cf:

With[{rgb = RGBColor @@@ (dat/255)},
cf = Blend[rgb, #] &;
]


And use it:

ArrayPlot[{Range@10}, ColorFunction -> cf]


ListPlot3D[
Table[Sin[j^2 + i], {i, 0, Pi, Pi/5}, {j, 0, Pi, Pi/5}],
Mesh -> None,
InterpolationOrder -> 0,
ColorFunction -> cf
]


• Any reason for the With[] filigree (e.g. pedagogical reasons)? cf = Blend[RGBColor @@@ (dat/255), #] &; seems to work just as well... Nov 16 '12 at 14:29
• great postings here! one last question about colorfunctions: when data values ranging from 0 ... 30 and I want to set a maximum for the colorfunction e.g. 0...20 -> all above 20 is colored with the same color (e.g.color3). is this somehow possible to achive ?! Nov 16 '12 at 14:37
• @J.M. that requires dat to remain defined, does it not? My intention was to create an independent function cf without any traps for beginners. Also, there should be less re-evaluation this way. Nov 16 '12 at 14:37
• @MrMuh the function cf is automatically defined over a range of zero to one. Arguments higher than one produce the same output value, e.g.: cf /@ Range[0, 2, 1/3]. You probably want to establish your color range outside of the cf function itself, that is with the plotting function's options, etc. Perhaps you would post an additional question about this so it can be addressed more completely? Nov 16 '12 at 14:41
• @MrMuh: you'll probably want to use Rescale[] in conjunction with Blend[], then... Nov 16 '12 at 14:50

If these are the RGB values of the color scheme:

color1 = {229, 245, 249};
color2 = {153, 216, 201};
color3 = {44, 162, 95};


you can blend them in this way:

Graphics[Table[{Blend[{{0, RGBColor[color1/255]}, {.5,
RGBColor[color2/255]}, {1, RGBColor[color3/255]}}, x],
Disk[{8 x, 0}]}, {x, 0, 1, 1/8}]]


• Since the colors are equispaced, one could use the more compact Blend[{RGBColor[color1/255], RGBColor[color2/255], RGBColor[color3/255]}, x] instead. Nov 16 '12 at 14:02
• awesome work! hm ok maybe you know how to use get the colorfunction into a ListPlot3D? Nov 16 '12 at 14:22

Here's how to make all the color schemes in ColorBrewer available in Mathematica:

ColorBrewer =
Association /@ Association[Import["http://colorbrewer2.org/export/colorbrewer.json"] /.
{s_String /; StringMatchQ[s, NumberString] :> FromDigits[s],
s_String /; StringMatchQ[s, "rgb(*)"] :> Interpreter["Color"][s]} /.
v_ /; VectorQ[v, ColorQ] :> With[{cols = v}, Blend[cols, #] &]];


Thus, one can now retrieve the color schemes in Wizard's and VLC's answers like so:

{ColorBrewer["YlGnBu", 3], ColorBrewer["BuGn", 3]}


• I love the streak you are on, getting information from web pages! For this one, how could I modify it so that I always get the list with the most colors? For example, compare {BarLegend[{ColorBrewer["Spectral", 10], {0, 1}}], BarLegend[{ColorBrewer["Spectral", 3], {0, 1}}]} - I would always like the first one. The maximum number of colors for any given map is different, so I don't want to have to experiment to see that "PuRd" can do 9 colors, but "Spectral" can give 11. May 23 '16 at 15:36
• Hmm, I'll think about it; a hackish method would be to apply KeySort[] to all the records of ColorBrewer and take the penultimate entry (since the last entry gives the class). May 23 '16 at 15:41
• One could also start with Import["http://colorbrewer2.org/export/colorbrewer.json", "RawJSON"], thus the Association muscles get some more training. May 23 '16 at 15:53
• @Karsten, I actually tried that at first, but then it seems the associations become opaque from replacement rules (as I used here); that's why I fell back to normal JSON importing. May 23 '16 at 16:00