File Exchange

image thumbnail

Convert between RGB and Color Names

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


Updated 19 Nov 2020

View Version History

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 Cobeldick (2021). Convert between RGB and Color Names (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (25)

Thomas Martin

Jonas Osterley

E. Cheynet

Stephen Cobeldick

@Selena Milanovic: the usual ways to control plotted line colors is to either set the axes ColorOrder property or to call plot in a loop (using a different color on each iteration), in either case using the colormap of your choice. If you want to specify the colormap colors by name, then COLORNAMES() could be used to generate that colormap, e.g.:

[~,M] = colornames('SVG','mintcream','tomato','olive','papayawhip');
X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)n*sin(x+2*n*pi/N), X.', 1:size(M,1));
plot(X,Y, 'linewidth',4)

Selena Milanovic

Hi, could you please make an example of how I can add this to my code?

Thank you!

xlabel('time (s)', 'Fontname','LM Roman 10','Fontsize', 40)
ylabel('GLC_c change (%)','Fontname','LM Roman 10','Fontsize', 40)
set(gca,'Fontname','Times New Roman','FontSize',40,'box','off')

Nathan Zimmerberg

Russell Marx

This is all I want in life

Ed Tunnacliffe

Aiman Raza

Russell Marx


Yes, your 3/18/2019 version fixed the bug for me. Thank you for the update!

Stephen Cobeldick

@KAE: thank you for the feedback and rating. Unfortunately your fix (for MATLAB versions R2014b and later) removes the colors of the text boxes, which is rather the point of the deltaE GUI. It is easy to keep the original GUI functionality by simply calling SET separately after the TEXT calls:

tmp = @(s,n) text((2*n-1)*ones(1,N)/(2*dEn), mean(Y.',2), zeros(1,N), s,...
'Parent',axh, 'HorizontalAlignment','center');
txh = cellfun(tmp, Nam, num2cell(1:dEn), 'UniformOutput',false);
tmp = @(h,c,b) set(h, {'BackgroundColor'},num2cell(c,2), {'Color'},num2cell(b(:,[1,1,1]),2));
cellfun(tmp, txh, RGB, BAW)

I will update the submission shortly with this fix.


In R2018b, I get the same error as Tasneem for colornames_deltaE('HTML4',jet(16)). You can get around it by editing a line in colornames_deltaE,
tmp = @(s,c,b,n) text((2*n-1)*ones(1,N)/(2*dEn), mean(Y.',2), zeros(1,N), s,...
'Parent',axh, 'HorizontalAlignment','center');
In other words, comment out {'BackgroundColor'},num2cell(c,2), {'Color'},num2cell(b(:,[1,1,1]),2)).
Very useful otherwise!

Chad Greene

Wonderful. Well designed and very useful!

Stephen Cobeldick

@Tasneem Al-Tmimi: the error message you show for COLORNAMES is exactly what I would expect, and is exactly as documented in the help and shown in the HTML examples: you provided some color names that cannot be matched and therefore an error is thrown, exactly as documented.

What MATLAB version do you use? I wrote this submission in R2010b, and there is absolutely no guarantee that the graphics functions will work with any post R2014b version (with the new handle graphics engine). See:


Tasneem Al-Tmimi

Yes, I know they are functions and am calling them from the command window exactly the same commands you have in the example tab

>> colornames_deltaE('HTML4',jet(16))

The Error is:
Error using text
Value cell array handle dimension must match handle vector length

Error in
(line 83)
tmp = @(s,c,b,n) text((2*n-1)*ones(1,N)/(2*dEn), mean(Y.',2), zeros(1,N), s, 'Parent',axh,
'HorizontalAlignment','center', {'BackgroundColor'},num2cell(c,2), {'Color'},num2cell(b(:,[1,1,1]),2));

Error in colornames_deltaE (line 84)
txh = cellfun(tmp, Nam, RGB, BAW, num2cell(1:dEn), 'UniformOutput',false);


>> colornames_view('dvips','Lab')
Undefined function or variable 'prv'.

Error in colornames_view/cnvResize (line 245)
tmp = get(prv, 'EdgeColor');

Error using colornames_view (line 88)
Error while evaluating Figure SizeChangedFcn.
You might see this error if any of the variables in the figure's SizeChangedFcn are undefined when the figure becomes visible. Consider waiting to set the figure's Visible property to 'on' until all the SizeChangedFcn variables are defined.

Error using cellfun output type is not supported. Set 'UniformOutput' to false.

Error in colornames_view/cnvTxtDraw (line 209)
txh = cellfun(txf,clr,num2cell(rgb,2),num2cell(baw(:,[1,1,1]),2));

Error in colornames_view (line 291)


>> colornames('CSS', 'bleu', 'blanc', 'rouge')
Error using colornames>cnNoMatch (line 293)
The palette 'CSS' does not support these colors: 'bleu', 'blanc', 'rouge'.

Some color names that are similar to those input strings:
bleu -> 'Blue', 'Black', 'Peru', 'Plum', 'Red', 'Aqua', 'Beige', 'Bisque', 'Brown', 'Cyan', 'Gold', 'Gray'.
blanc -> 'Black', 'Blue', 'Cyan', 'Tan', 'Beige', 'Brown', 'Gray', 'Khaki', 'Lime', 'Linen', 'Navy', 'Olive'.
rouge -> 'Azure', 'Beige', 'Blue', 'Orange', 'Aqua', 'Brown', 'Coral', 'Gold', 'Lime', 'Olive', 'Plum'.

Call COLORNAMES('CSS') to list all color names for that palette,
or COLORNAMES_VIEW('CSS') to view the palette in a 2D list,
or COLORNAMES_CUBE('CSS') to view the palette in a 3D cube.

Error in colornames (line 240)

Stephen Cobeldick

@Tasneem Al-Tmimi : what error message are you getting? How are you calling the function/s? Did you download and unzip the files onto your MATLAB path?

Note that there are no scripts in this submission, only functions. If you do not know the difference then this might cause you to use them incorrectly. Read the MATLAB documentation to know more:


Tasneem Al-Tmimi

Hi, am trying to use the files, but I always get an error am running the script file
can you help me please ??

Anh Phan

Mario Koddenbrock

Hao Xie

Jan Studnicka

Artemiy Vyacheslavov

Saved my thesis, thanks a lot!

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!