Wolfram Blog
Yu-Sung Chang

Mathematica‘s True Colors

June 22, 2007 — Yu-Sung Chang, Technical Communication & Strategy

We can’t emphasize enough how important colors are in scientific visualization. Colors can convey the information which cannot be represented by geometry only. Sometimes, the data is just unreadable without proper colors in place. Most of all, colors can make graphics and plots more attractive and appealing.

In previous versions of Mathematica, it was not always easy to pick the right colors or color functions. Probably, you would end up playing with the values of RGBColor or Hue, which can be both tedious and time consuming.

During the development of Mathematica 6, we committed to change this situation. First off, we made–among dozens of newly added controls–a few specifically dedicated to color input.

Let’s start with the simplest one, ColorSetter:

In[1]:= ColorSetter[] | Out[1]: (interactive color setter box)

If you click this naive-looking square in Mathematica, the system color dialog box is brought up and you can interactively choose colors.

Color-selector window

For more fun, you can use the setter with the new Dynamic function:

In[2]:= {ColorSetter[Dynamic[c]],<br>  Graphics[{Dynamic[c], EdgeForm[Black], Disk[]}]} | Out[2]= (ColorSetter with dynamically colored Disk)

Change the value of “c” in the color dialog box and the color of the disk changes dynamically along with it.

Of course, if you do not wish to use the dialog box, and want to make a self-contained control within a notebook, you can use ColorSlider instead:

In[3]:= {ColorSlider[Dynamic[c]],<br>  Graphics[{Dynamic[c], EdgeForm[Black], Disk[]}]} | Out[3]= (ColorSlider with dynamically colored disk

So, the interactive controls are nice and all, but how about programmatic ways to work with color? In Mathematica 6, we’ve extended the color-related functions in several different ways.

First, there are new system color symbols. In addition to the usual Red, Blue, etc., we now have “light” versions of all the system colors, such as LightBlue and LightPink.

Secondly, we’ve introduced a few new color-manipulating functions. They are Lighter, Darker, and Blend. It is not hard to guess what they do by their names, as shown in this animation:

Manipulate[Row@{ Graphics[{EdgeForm[Black], Lighter[Red, i], Disk[]}], Graphics[{EdgeForm[Black], Darker [Red, i],Disk[]}], Graphics[{EdgeForm[Black], Blend[{Red, Green}, i], Disk[]}]}, {i, 0, 1}]

Interactive visualization of colors

The way in which we assign colors to graphics objects has been expanded, too. Now, we have the new VertexColors option, which assigns individual colors of vertices in Polygon, GraphicsComplex, etc.:

In[5]:= Graphics[Polygon[{{-1, 0}, {1, 0}, {0, Sqrt[3]}}, VertextColors->{Red, Green, Blue}]] | Out[5]=  (Triangle with red, green, and blue vertex colors)

If you feel adventurous, try this:

In[6]:= pic = Reverse[ExampleData[{TestImage, Lena}, Data]][[5 ;; -5 ;; 5, 5 ;; -5 ;; 5]];  |  In[7]:= ListPlot3D[Table[x + Sin[x y], {x, -5, 5, .1}, {y, -5, 5, .1}], Mesh -> None, VertexColors -> {pic/255.}, Lighting -> Neutral]  |  Out[7]= (Textured plot with image)

For each point on the plot, we pick a color from the image, which, in return, produces a texture-like effect. Texturing a surface is not what VertexColors is originally intended for, but still you can simulate the effect.

Last but not least, there is ColorData. It seemed to us that many people want to use names for their colors. On the other hand, we also found that those names are quite arbitrary, and differ from one context to the other. So, instead of adding a bunch of new color names at will, we organized popular named colors and grouped them together. And what is the best way to present them? Using built-in computable data sources, of course.

Here is an example. If you want to specify the color “DeepSkyBlue”, defined in the HTML standard color names, you can use:

In[8]:= Graphics[{ColorData[HTML][DeepSkyBlue], Rectangle[]}]  |  Out[8]= (DeepSkyBlue rectangle)

Or, ColorData["HTML","DeepSkyBlue"] can be used instead. In addition, you can choose the RGB values of the colors interactively (complete with Tooltips):


Of course, we didn’t forget our beloved named colors, such as Gold or Banana, which were part of the Graphics`Colors package in previous versions. Now, they can be accessed immediately using ColorData["Legacy"]["Gold"], for instance.

You will be happy to know that there are even colors like ColorData["GeologicAges"]["Jurassic"]–useful if you’re a geologist. The list of these named color schemes can be acquired by entering ColorData["Named"].

When ColorData was first conceived during development, we were somewhat worried of colors being presented through curated data. But it turned out that it wasn’t a bad idea at all. In addition to named color schemes, we were able to introduce a great number of different sets of colors. Here is one example:

In[10]:= ContourPlot[x + Sin[x^2 + y^2], {x, -4, 4}, {y, -4, 4},<br>  ColorFunction -> Pastel]

You will notice that there is an unusual color function called “Pastel“. This is a shortcut for ColorData["Pastel"]. It returns a gradient function, defined over a unit interval.

This gradient is part of a big collection called ColorData["Gradients"]. The next example generates a list of thumbnails of all the gradients:

In[11]:= GraphicsGrid[Partition[ColorData[#, Image] & /@ ColorData[Gradients], 3], ImageSize -> 300]

In addition to “Named” and “Gradients“, ColorData contains other collections, too. One of the easiest ways to access everything in ColorData is to use the new Color Schemes palette, under the Palettes menu:

Color Schemes palette

There are so many things that you can achieve with the new color-related functions. I’ll end here by showing a few neat examples from our new Documentation Center as well as from The Wolfram Demonstrations Project site.

You’ll find that discovering your own true colors is easier than ever with Mathematica 6.

Examples from Documentation Center and The Wolfram Demonstrations Project

Posted in: Mathematica News

Comments are closed.