File Exchange

image thumbnail

Convert between RGB and Color Names

version 3.5 (199 KB) by

RGB to color name. Color name to RGB. Palletes: CSS, HTML, MATLAB, SVG, X11, xcolor, xkcd,...

27 Downloads

Updated

View License

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

The function COLORNAMES accepts either RGB values or colorname strings 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 RGB, CMCl:c, CIE76, CIE94, or CIEDE2000 color difference calculation.
* Input color name matching is case insensitive with optional spaces, except for some schemes where CamelCase is significant.

Whether matching RGB values or color name strings, COLORNAMES has the same two outputs:
1. Cell array of the matched color name strings.
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 RGB, Lab, LCh, HSV, or XYZ.
* 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, CMCl:c, or RGB). For more information:
https://en.wikipedia.org/wiki/Color_difference

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

### Examples ###

colornames() % list all supported palettes
ans =
'Alphabet'
'AmstradCPC'
'AppleII'
'Bang'
'BSC381'
'CGA'
'Crayola'
'CSS'
'dvips'
'Foster'
'HTML4'
'ISCC'
'Kelly'
'MacBeth'
'MATLAB'
'Natural'
'R'
'RAL'
'Resene'
'Resistor'
'SVG'
'Wikipedia'
'Wolfram'
'X11'
'xcolor'
'xkcd'

colornames('Natural') % all color names for one palette
ans =
'Black'
'Blue'
'Green'
'Red'
'White'
'Yellow'

[clr,rgb] = colornames('HTML4','blue','red','teal','olive')
clr =
'Blue'
'Red'
'Teal'
'Olive'
rgb =
0 0 1
1 0 0
0 0.50196 0.50196
0.50196 0.50196 0

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

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

[clr,rgb] = colornames('MATLAB');
[char(strcat(clr,{' '})),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

colornames('MATLAB','c','m','y','k')
ans =
'Cyan'
'Magenta'
'Yellow'
'Black'

Comments and Ratings (9)

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:

<https://www.mathworks.com/help/matlab/graphics_transition/major-graphics-changes-in-r2014b.html>

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
colornames_deltaE>@(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))
(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
matlab.graphics.primitive.Text 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)
cnvTxtDraw()

=========================================================================

>> 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)
cnNoMatch(pal,clr,arg(idn==0))

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:

<https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html>

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

Hao Xie

Jan Studnicka

Saved my thesis, thanks a lot!

Updates

3.5

* VIEW: show RGB value with click on color name.
* Combine into one resistor palette.
* Allow for negative leading integer.

3.4

* Change Lab illuminant to D65.
* Fix bug in camorbit for CUBE.
* Improve help examples.

3.3

* Add AmstradCPC palette.
* Match initial letters for palettes Alphabet, MATLAB, and Natural.

3.2

* Documentation changes.

3.1

* Add CIEDE2000 deltaE color distance calculation.

3.1

* Improved HTML documentation.

3.1

* Add COLORNAMES_CUBE function.
* Change input argument name from "scheme" to "palette".

3.0

* Total rewrite of the RGB matching algorithm.
* Add HTML documentation.

2.1

* Much more robust string matching using Levenshtein distance to pick closest colorname (Wagner–Fischer algorithm).
* Rename supplementary function (deltaE viewer) and add default values.

2.0

* Add lots of colorschemes.
* Add new fields to .MAT file: <spaces> and <indices>.

1.2

* Add Alphabet and Kelly colorschemes.

1.1

- Downloadable as toolbox

MATLAB Release
MATLAB 7.11 (R2010b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video