Three-Dimensional Mathematica Graphics (Section 4.2)

Extract from Mathematica Graphics: An Intensive Tutorial

by Tom Wickham-Jones

This HTML document is based on Mathematica Graphics: An Intensive Tutorial by Tom Wickham-Jones. It was adapted by Martin Kraus for non-commercial use.

Mathematica and MathLink are registered trademarks, and MathReader, MathSource and 3-Script are trademarks of Wolfram Research, Inc.

All other product names mentioned are trademarks of their producers.

Copyright 1992 by Wolfram Research, Inc.

All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior written permission of the copyright holder.

4.2 Using Color and Lighting

Mathematica graphics have many features designed to use color. Some of these examples will not look so interesting on a monochrome display; however, in color some of them are quite striking.

Function evaluation
Color output
Some of the ways to introduce color into Mathematica graphics.

4.2.1 Directives

There are a number of directives to color primitives.
RGBColor[r, g, b]
Hue[h, s, b]
CMYKColor[c, m, y, k]
Mathematica color directives.

The directives GrayLevel, RGBColor and CMYKColor take as arguments numbers between 0 and 1 inclusive. Thus GrayLevel[0] is black, RGBColor[1, 0, 0] is a red and CMYKColor[0, 1, 0, 0] is a magenta.

The directive Hue is special; it can take one or three arguments. The latter, Hue[h, s, b], describes the hue, saturation and brightness. There is also a one-argument form, Hue[h], which is equivalent to Hue[h, 1, 1]. The arguments for saturation and brightness must be numbers between 0 and 1 inclusive. However the first argument, that for hue, is taken modulo 1. Thus Hue[0], Hue[1] and Hue[2] are all the same color, namely a red. This wrapping around makes Hue a versatile command for introducing color into the graphics.

The directive GrayLevel colors the Point. In[19]:= Show[Graphics3D[{GrayLevel[0.5], PointSize[0.5], Point[{0, 0, 0}]}]]
Out[19]= - Graphics3D -
Lighting -> False switches off the default coloring which is determined by the light sources. FaceForm and EdgeForm are useful methods to color polygons. In[20]:= Show[Graphics3D[{EdgeForm[RGBColor[1, 0, 0]], FaceForm[RGBColor[0, 1, 0]], Polygon[{{0, 0, 0}, {1, 0, 0}, {0, 1, 0}}]}, Lighting -> False]]
Out[20]= - Graphics3D -

4.2.2 Lighting

The lighting of three-dimensional Mathematica graphics is set by a number of options.

Lighting whether or not to use simulated lights
LightSources positions and colors of the lights
AmbientLight ambient light color
Shading whether or not to color the surface
Options for controlling lighting and coloring of three-dimensional graphics.

The lighting model used in Mathematica is not a photo-realistic ray tracer. It provides simulated light sources which do not diverge and do not cast shadows. To use the simulated light sources it is necessary to set the option Lighting -> True, which is set by default. The direction of the lights are determined by the setting of the LightSources options.

The default setting of the light sources. In[21]:= Options[Graphics3D, LightSources]

Out[21]= {LightSources -> {{{1., 0., 1.}, RGBColor[1, 0, 0]}, {{1., 1., 1.}, RGBColor[0, 1, 0]}, {{0., 1., 1.}, RGBColor[0, 0, 1]}}}

The coordinates of these light sources are in display coordinates. Thus the default setting gives lights which are on the right, top and top-right of the screen. The parts of the object which are not illuminated by the light sources are colored by the AmbientLight option. To make a more complex type of surface one can use the SurfaceColor directive.

4.2.3 Function Evaluation

Mathematica provides a number of ways to color a function according to an evaluation.

Plot3D[{fun[x, y], color[x, y]}, ...]
ParametricPlot3D[{x, y, z, color[x, y, z]}, ..., Lighting -> False]
Plot3D[fun[x, y], ..., ColorFunction -> col]
DensityPlot[fun[x, y], ..., ColorFunction -> col]
ContourPlot[fun[x, y], ..., ColorFunction -> col]
Some ways to color plots by function evaluation.
Height is determined by the first element of the list and the color by the second. In[22]:= Plot3D[{Abs[Sqrt[x + I y]], Hue[Arg[Sqrt[x + I y]] / 2 / Pi]},{x, -1, 1}, {y, -1, 1}]
Out[22]= - Graphics3D -
The fourth coordinate determines the color of the patch. In[23]:= ParametricPlot3D[{Cos[p] Sin[t], Sin[p] Sin[t], Cos[t], FaceForm[Hue[(t + p) / Pi]]}, {p, 0, 2 Pi}, {t, 0, Pi}, Lighting -> False]
Out[23]= - Graphics3D -


ColorFunction is an option for SurfaceGraphics, DensityGraphics, ContourGraphics and related commands. It also exists for the Graphics primitive Raster. It must be set to a function of one argument. This is given the height at each point of the array scaled to run from 0 to 1. It must return a color directive.

Regions which are red will be the very highest or the very lowest since Hue wraps around. This will use the one argument version of Hue. This is a very simple way to color a plot. In[24]:= Plot3D[x y, {x, -2, 2}, {y, -2, 2}, ColorFunction -> Hue]
Out[24]= - SurfaceGraphics -

4.2.4 ColorOutput

ColorOutput is an option for all graphics commands. It instructs Mathematica to filter through the PostScript as it is emitted and change color specifications. In this way color separations can be carried out.

Automatic leave original colors untouched
GrayLevel convert to GrayLevel with internal algorithm
RGBColor convert to RGBColor with internal algorithm
CMYKColor convert to CMYKColor with internal algorithm
fun use supplied function to convert colors
Values of the option ColorOutput.
Mathematica will use an internal algorithm to convert PostScript setrgbcolor to setgray. In[25]:= Plot3D[Cos[x] + Sin[y], {x, -4, 4}, {y, -4, 4}, ColorOutput -> GrayLevel]
Out[25]= - SurfaceGraphics -
We can define our own simple graylevel function, which could be used by setting ColorOutput -> grayfilter. In[26]:= grayfilter[RGBColor[r_, g_, b_]] := GrayLevel[(r + g + b) / 3]
In[27]:= grayfilter[GrayLevel[g_]] := GrayLevel[g]
In[28]:= grayfilter[RGBColor[1, 0.5, 1]]

Out[28]= GrayLevel[0.833333]
In[29]:= grayfilter[GrayLevel[0.5]]

Out[29]= GrayLevel[0.5]

next page: 4.3 Combining and Converting Images back to table of contents