ColorSpec (Color Specification)

Color specification

Description

ColorSpec is 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

This code makes the x-axis red.

ax = gca;
ax.XColor = 'red';

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 as fixed colors, as opposed to colormap colors.

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 the CData property of a scatter object to a color name.