image thumbnail

Convert between RGB and Color Names

version 5.0.1 (288 KB) by Stephen
RGB to color name. Color name to RGB. Palettes: CSS, HTML, MATLAB, SVG, X11, xcolor, xkcd,...


Updated 19 Nov 2020

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

One function converts between RGB and color names, in both directions!

The function COLORNAMES accepts either RGB values or color names as its inputs, and returns the color names and RGB values from the palette of your choice. The color palette is selected by the first input, the trailing inputs are either color names or RGB values:

* Input RGB are matched using CIEDE2000, CIE94 (default), CIE76, DIN99, CMCl:c, or RGB color difference (deltaE) calculation.
* Input color name matching is case insensitive with optional spaces, except for some palettes where CamelCase is significant.

COLORNAMES always returns the same two output arguments:
1. Cell array of the matched color names (cell of 1xN char).
2. Numeric array of the RGB values (a standard MATLAB colormap).

A list of the available palettes is returned by calling COLORNAMES with no inputs.

Three bonus functions are also included:

* COLORNAMES_CUBE shows the colors from any palette in a 3D cube. The cube may be Lab, LCh, DIN99, HSV, XYZ, or RGB.
* COLORNAMES_VIEW shows the colors from any palette in 2D axes, and sorting them by different color space values (Lab, XYZ, YUV, HSV, or RGB).
* COLORNAMES_DELTAE creates a figure comparing the different color difference calculations (deltaE, e.g. CIEDE2000, CIE94, CIE76, DIN99, CMCl:c, or RGB). For more information:

Suggestions of useful palettes (with named colors) are very welcome!

### Examples ###

>> palettes = colornames()
palettes =

>> colornames('Natural') % all color names for one palette
ans =

>> [names,rgb] = colornames('HTML4','blue','red','teal','olive')
names =
rgb =
0 0 1.0000
1.0000 0 0
0 0.5020 0.5020
0.5020 0.5020 0

>> colornames('HTML4',[0,0.5,1;1,0.5,0]) % default deltaE = CIE94
ans =

>> colornames('HTML4',[0,0.5,1;1,0.5,0],'rgb') % specify deltaE
ans =

>> colornames('MATLAB','c','m','y','k')
ans =

>> [names,rgb] = colornames('MATLAB');
>> [char(strcat(names,{' '})),num2str(rgb)]
ans =
Black 0 0 0
Blue 0 0 1
Cyan 0 1 1
Green 0 1 0
Magenta 1 0 1
Red 1 0 0
White 1 1 1
Yellow 1 1 0

Cite As

Stephen (2022). Convert between RGB and Color Names (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
Compatible with R2009b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!