Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

我们为许可用户提供了部分翻译好的中文文档。您只需登录便可查阅这些文档

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

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];

More About

collapse all

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.

Was this topic helpful?