21
$\begingroup$

I am using the "TemperatureMap" color scheme for image processing. How can I set the lowest color value to Black?

Generally speaking, is it possible to exchange a specific color (at a certain value of an existing color scheme) by another custom color?

$\endgroup$
1

2 Answers 2

33
$\begingroup$

I wrote the ColorBar package exactly for this purpose and it makes such modifications easy. The README.m should give you all the instructions you need, but I'll summarize it here. After installing the package (copy ColorBar.m to FileNameJoin[{$UserBaseDirectory, "Applications"}]), do the following:

ColorBar["TemperatureMap"]

Now you can click on the left most triangle and change the color to black or anything else. The control points can be manipulated in the following ways:

  • Click and drag the control points (triangles) to change the transition region

  • Click on a control point to change its color

  • Click while holding down Command (or Alt in Windows & Linux) to add a control point at that location.

  • Click on a control point while holding Shift to delete a control point (a minimum of 2 control points will always remain).

To get back a color function, apply Setting on the modified object:

Finally, you can combine these and inline everything so that you can modify it within a plot command. Copy the following and use "Evaluate in place" or CommandEnter on only the ColorBar[...] part:

ContourPlot[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction -> Setting@ColorBar["StarryNightColors"]]

Now you can change the color scheme as per the above example and then evaluate the cell to get the plot:

$\endgroup$
2
  • 2
    $\begingroup$ What a nice solution! $\endgroup$
    – Mr.Wizard
    Dec 3, 2014 at 14:57
  • $\begingroup$ This package of yours looks pretty sweet! I can't wait to get my computer back and try your package out. :D $\endgroup$ May 10, 2015 at 0:21
9
$\begingroup$

I answered this question for indexed color schemes here:

How to change element color in Periodic Table?

The gradient color schemes have a simpler structure, e.g.:

enter image description here

However it seems unclear to me what the desirable semantics of a general function for gradient schemes would be. I think from your description that you wish to replace an existing color "swatch" with another one but do you know what that swatch is ahead of time? For example if you wanted to replace the third blue from the left how would you easily know its specific value? One does not even know the number of swatches used by Blend without additional effort so a replacement by position is not convenient either. Nevertheless I shall pick that for this demonstration. If you can suggest a better approach I will attempt to implement it.

SetAttributes[withGradientScheme, HoldFirst]

withGradientScheme[body_, scheme_, {rules__} | rules_Rule] :=
 With[{CS := DataPaclets`ColorDataDump`colorSchemes},
  ColorData[1, 1] (*preload*);
  Internal`InheritedBlock[{CS, ColorData},
    Unprotect[ColorData]; Quiet[ColorData[scheme] =.];
    CS = MapAt[ReplacePart[{rules}], CS, Append[5] /@ Position[CS, {{scheme, ___}, __}]];
    body
  ]
 ]

Test:

withGradientScheme[
  Plot3D[Sin[x + y^2], {x, -3, 3}, {y, -2, 2}, ColorFunction -> "TemperatureMap"],
  "TemperatureMap",
  1 -> Black
]

enter image description here

Last time I had a few bugs to work out and that may be true here as well as I haven't tested this extensively yet. Please let me know if you run into any problems with it.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.