# How to make custom discrete color data function

Attempting to make custom discrete color data function:

my custom color:

is it possible to use my custom color

colors = Reverse@Table[Hue@h, {h, 0, 0.7, 1/10}] // N;


Thanks to mfvonh, color function and color data

cf[x_] :=
Which @@ Flatten[
List, {x >= # & /@ Reverse@Most@Range[0, 1, 1/Length@colors],  colors}]]

colordata=cf /@ Rescale[pts[[All, 2]], {0,0.1}];


You can just make your own function, though in most situations you can just pass a list of colors.

colors = Table[Hue@h, {h, 0, 0.7, 1/10}];
discrete[x_] := Part[colors, x];
ContourPlot[x + Sin[x^2 + y^2], {x, -4, 4}, {y, -4, 4}, Contours -> 9,


For a gradient, use Blend:

gradient[x_] := Blend[colors, x];
Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},

• Can you give an example? In Graphics you usually specify colors explicitly with directives. Commented Jun 18, 2014 at 17:28
• Here's the basic idea in most Graphics situations: Riffle[Table[Rectangle[{i, 0}], {i, 8}], colors]. (Add //Graphics to see it rendered.) Commented Jun 18, 2014 at 17:33
• I'm not sure exactly what you mean, but perhaps this? Graphics3D[{[email protected], First@colors, Riffle[pts, colors]} /. p : {__?NumericQ} -> Point[p]]. Commented Jun 18, 2014 at 21:16
• Graphics[GraphicsComplex[pts,{Polygon@poly},VertexColors->(gradient/@Rescale[pts[[All,2]],{0,0.1}])],AspectRatio->1/2,Frame->True] Commented Jun 19, 2014 at 18:21