No License

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

» Watch video

Highlights from

3.5 | 2 ratings Rate this file 6 Downloads (last 30 days) File Size: 2.78 KB File ID: #4265 Version: 1.0



Rasmus Anthin (view profile)


15 Dec 2003 (Updated )

Convert hex color to or from MATLAB color vector. (with syntactic sugar).

| Watch this File

File Information

I know this routine might be a clone. But the nifty thing with this is that you can enter the color code in many different formats as explained below.

It also senses what type of data you input wheter it is hexadecimal or if it is a MATLAB rgb vector, so you don't have to tell it which format you want to convert to!

Here comes the help section:

RGBCONV Convert hex color to or from MATLAB rgb vector.
    RGB = RGBCONV(HEX) where HEX is a string of hexadecimal numbers
       'RRGGBB' where the parts RR, GG, BB are two digit hexadecimal
       numberes corresponding to the strengths of the colors red,
       green and blue. RGB is the matlab rgb-vector.
    RGB = RGBCONV(HEX) where HEX is a string matrix of the form
       ['RR';'GG';'BB'], or a cell string {'RR','GG','BB'}, or
    RGB = RGBCONV('RR','GG','BB')
    [R,G,B] = RGBCONV(...) gives the corresponding matlab rgb-values
    as scalar values in R, G and B correspondingly.
    HEX = RGBCONV(RGB) where RGB is a matlab rgb-vector with
       three elements each corresponding to strengs of
       red, green and blue and ranging between 0 and 1.
    HEX = RGBCONV(R,G,B) where R, G and B are scalar values
       ranging between 0 and 1 each corresponding to red, green
       and blue.
       HEX is a string containing the hex values and is of length 6.
    ['RR','GG','BB'] = RGBCONV(...) gives the corresponding hex
       values as two character strings 'RR', 'GG' and 'BB'
       rgbconv([.1 .2 .3])
       [r,g,b]=rgbconv([.1 .2 .3])
    See also COLORMAP.


This file inspired Colormap Dropdown Menu and Rgb2hex And Hex2rgb.

MATLAB release MATLAB 5.2 (R10)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
18 Sep 2014 Jeffrey Girard

With the fix from per isakson, this works well.

11 Dec 2011 Thomas Guillod

Nice code, was very helpful for GUI dev.

08 Apr 2010 per isakson

No, the problem is with rgbconv. To fix it add ",2" to line 67:


Comment only
01 Apr 2010 per isakson

[0,0,0] cuases a runtime error (R2008a). I would say it is rather a bug in Matlab than in rgbconv.
/ per

>> rgbconv([0,0,0])
??? Error using ==> reshape
To RESHAPE the number of elements must not change.

Error in ==> rgbconv at 68

Comment only

Contact us