3-D Visualization

Indexed Color Data

MATLAB interprets indexed color data as either values to scale before mapping to the colormap, or directly as indices into the colormap. You control the interpretation by setting the `CDataMapping` property. The default is to scale the data.

Scaled Color

By default, MATLAB scales the color data so that the minimum value maps to the first color in the colormap, the maximum value maps to the last color in the colormap, and values in between are linearly transformed to span the colormap. This enables you to use colormaps of different sizes without changing your data and to use data in any range of values without changing the colormap.

For example, the following patch has eight triangular faces with a total of 24 (nonunique) vertices. The color data are integers that range from one to 24, but could be any values.

The variable `c` contains the color data. It is a 3-by-8 matrix, with each column specifying the colors for the three vertices of each face.

• ```c =
1     4     7    10    13    16    19    22
2     5     8    11    14    17    20    23
3     6     9    12    15    18    21    24
```

The color bar (`colorbar`) on the right side of the patch illustrates the colormap used and indicates with the vertical axis which color is mapped to the respective data value.

You can alter the mapping of color data to colormap entry using the `caxis` command. This command uses a two-element vector [`cmin` `cmax`] to specify what data values map to the beginning and end of the colormap, thereby shifting the color mapping.

By default, MATLAB sets `cmin` to the minimum value and `cmax` to the maximum value of the color data of all graphics objects within the axes. However, you can set these limits to span any range of values and thereby shift the color mapping. See "Calculating Color Limits" in the "Axes Properties" chapter for more information.

The color data does not need to be a sequential list of integers; it can be any matrix with dimensions matching the coordinate data. For example,

• ```patch(x,y,z,rand(size(z)))
```

Direct Color

If you set the patch `CDataMapping` property to `off`,

• ```set(patch_handle,'CDataMapping','off')
```

MATLAB interprets each color data value as a direct index into the colormap. That is, a value of 1 maps to the first color, a value of 2 maps to the second color, and so on.

The patch from the previous example would then use only the first 24 colors in the colormap.

This example uses integer color data. However, if the values are not integers, MATLAB converts them according to these rules:

• If value is < 1, it maps to the first color in the colormap.
• If value is not an integer, it is rounded to the nearest integer towards zero.
• If value > `length(colormap)`, it maps to the last color in the colormap.

Unscaled color data is more commonly used for images where there is typically a colormap associated with a particular image.

 Interpreting Indexed and Truecolor Data Truecolor Patches