Documentation

ColorSpec (Color Specification)

Color specification

Description

ColorSpecis not a function; it refers to the three ways in which you specify color for MATLAB®graphics:

  • RGB triplet

  • Short name

  • Long name

The short names and long names are character vectors that specify one of eight predefined colors. The RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color; the intensities must be in the range [0 1]. The following table lists the predefined colors and their RGB triplet equivalents.

RGB Triplet

Short Name

Long Name

[1 1 0]

y

yellow

[1 0 1]

m

magenta

[0 1 1]

c

cyan

[1 0 0]

r

red

[0 1 0]

g

green

[0 0 1]

b

blue

(1 1 1)

w

white

[0 0 0]

k

black

Examples

To change the background color of a figure to green, specify the color with a short name, a long name, or an RGB triplet. These statements generate equivalent results:

whitebg('g') whitebg('green') whitebg([0 1 0]);

This code changes the figure background color to pink:

fig = gcf; fig.Color = [1 0.4 0.6];

Tips

The eight predefined colors and any colors you specify as RGB triplets are not part of a figure's colormap, nor are they affected by changes to the figure's colormap. They are referred to asfixedcolors, as opposed tocolormapcolors.

In most cases, you can specify a color using either an RGB triplet or a character vector of a color name. However, in some cases, you cannot specify the color as a color name. For example, you cannot set theCDataproperty of a scatter object to a color name.

Was this topic helpful?