validatecolor

Validates a color specification and returns it as an RGB triplet.

Category: Programming > Functions

Syntax

rgb = validatecolor(c)

Converts one color given as a name, short name, hex code or RGB row into a 1-by-3 RGB row, or errors if it is invalid.

rgb = validatecolor(c, 'multiple')

Accepts several colors (an m-by-3 matrix, a cell array or a string array) and returns an m-by-3 matrix.

Description

The function accepts the same color forms that plotting functions do and normalizes them to numeric RGB, which is handy before storing or interpolating colors. A name that is not recognized, a malformed hex code or an RGB value outside the range 0 to 1 raises an error that explains what is accepted.

Input arguments

c
A color name such as 'red', a short name such as 'r', a hex code like '#FF8000' or '#F80', or an RGB triplet with values from 0 to 1.

Output arguments

rgb
Double RGB values between 0 and 1, one row per color.

Example

Names, hex codes and several colors

validatecolor('r')
validatecolor('#FF8000')
validatecolor([1 0 0; 0 1 0], 'multiple')
Output
ans = 1×3

     1     0     0
ans = 1×3

    1.0000    0.5020         0
ans = 2×3

     1     0     0
     0     1     0

Notes

MatLivre returns only the RGB output.

See also

colormap rgb2hsv hex2dec plot