File Exchange

image thumbnail

RGB triple of color name, version 2

version (4.46 KB) by Kristjan Jonasson
Returns a Matlab RGB color specifier corresponding to a given color name (139 colors supported).


Updated 05 Jul 2009

View Version History

View License

RGB = RGB('COLORNAME') returns the red-green-blue triple corresponding to the color named COLORNAME by the CSS3 proposed standard [1], which contains 139 different colors (an rgb triple is a 1x3 vector of numbers between 0 and 1). The color names are the ones accepted by almost all web browsers, for example Brown, DarkRed, SlateGray.

RGB CHART creates a figure window showing all the available colors with their names.

COLORNAME = RGB(r,g,b) and COLORNAME = RGB([r,g,b]) both find the name of the color with the triple that is closest to [r,b,g] (measured by sum of squares).

c = rgb('DarkRed') gives c = [0.5430 0 0]
c = rgb('Green') gives c = [0 0.5 0]
plot(x,y,'color',rgb('orange')) plots an orange line through x and y
rgb chart shows all the colors
cnam = rgb(0.8,0.3,0.3) gives cnam = 'IndianRed'

[1] "CSS Color module level 3", W3C (World Wide Web Consortium) working draft 21 July 2008,
[2] "Web colors",

Cite As

Kristjan Jonasson (2021). RGB triple of color name, version 2 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (83)

Jacqueline Rigatto

Leah Spain

Daniella Zecchino

excellent! thank you for solving my problem.

Iven Yi

Stephen Cobeldick

@Adam Danz: "nearest" in which colorspace? Or using which deltaE algorithm? Hopefully not just the Euclidean distance in sRGB!

Nicolas Hoelsch

Jacqueline Rigatto

Shahzeb Tariq

Beatriz Gaitan Soto

Frazer Musonda

Luisa Barrera


Ruoyu Lan

Clair Stark

Adam Danz

Feature requests
1) rgb({'green','blue',red','Lime'}) % to return a matrix of triplets
2) rgb(10) %to return a predefined list of different colors
3) rgb([0.86 0.86 0.86]) % to return the nearest color name
These are all features I've added to the file and others may find useful.

Saket Adhau

Great Work

David Garcia

Very useful. Thanks!!

Christian Iandiorio

Dimitrij Chudinzow


Cange Colum 46:
function rgb = rgb(s)
function rgbname = rgb(s)


This is very helpful!!!!! Thank you for your effort!!!!!!

Eduard Reitmann


Thank you very much for your efforts to ease the process.

Adam Danz

Domenico Perillo


Juan Ramirez

Linwei Hu

Thank you so much! This is very useful for line/scatter plots.

Meredith Fish

Adam Danz


Katsuyuki Taguchi

Farhad Sedaghati

Greg King

Just Great!!

tianfu xu

Very well! Thanks.

Þorsteinn Markússon

Karina Gutierrez

I am having troubles running the rgb.m, when I try to run it Matlab gives me the following error:
function rgb = rgb(s)

Error: Function definitions are not permitted in this context.

Someone has any ideas? Thanks

Julien Reynes

Very helpful! My advice : print the sheet with all colors and save the rgb function in a folder then add this folder with 'set path'. Then it would be very easy to use any color ;)


Xinyi Yang

straightforward and very helpful! Thanks a lot!

Carl Reiner

Very easy to use and works perfectly! Thanks a lot

weiwei wu

juan lopez


Really good perfect program!

Stephen Cobeldick

@Artemiy Vyacheslavov: you don't need to "combine" anything. The function that I wrote (at the link you gave) converts in both directions: from RGB to colorname, and also from colorname to RGB.

So why "combine" when my submission does both conversions already? It does not require any supporting functions.

Artemiy Vyacheslavov

Combined with the reverse conversion script ( works perfectly!

Donald Chu

Thanks so much. It's great to be able to use more than the basic colors.


Very helpful!


and it would be even better, if you will consider to add recently updated default colororder used in plots for the list of color shortcuts! thanks


works great

Adam Danz

John Trimper


Stephen Cobeldick

As some commenters have noted, this submission does not match RGB -> colornames. I wrote a color identifier function to convert between RGB and colornames (both ways):

The user can also choose the to match RGB values using least-squares, or one of the color-difference methods CMC2:1, CIE76, or CIE94 (the default). A bonus function is also included that demonstrates that differences between these matching methods. For more info see also:

Chad Greene

By coincidence, this function works with the rgbmap function described here:

Jens Richter

Very useful :-)




Pit D

Dimitra Zarbouti

Very useful. Thanks!


Great!! very useful.




Great function! Very helpful.

As others have mentioned the colorname = rgb(r, g, b) has not been implemented though!

Katarzyna Zarnowiec

Benjamin Bayes

Highly useful function! However, your Version 2 seems to have not been uploaded. The current function will not accept 3 inputs [R,G,B] to return a color name, as you describe. Please update!

Pio Nonus

At last an easy way to select Matlab colors. Before your script it was frustrating.

Bahman Abbasi

Very useful. Given the frustratingly few default choices in Matlab, this can extend your lifespan by a few day!

SeungGyu cho

cool~, I was looking for this, thank you.

John Trimper

so great.



Ryan Stanley

This is a great tool however I am having a few issues. When I use your example code:

cnam = rgb(0.8,0.3,0.3)

Matlab returns:

??? Error using ==> rgb
Too many input arguments.

Any ideas?

Darwin@cal Chang

Great job!

April Warnock

This is great! Thanks so much!

Richard Crozier

very handy, thanks!


this is so simple and brilliant!!! What a massive time-saver and figure-improver!! A million thanks!!


Husam Aldahiyat



excellent in-depth help including many references...
easily expandable look-up table engine...
certainly very useful for color-freaks...

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!