Code covered by the BSD License  

Highlights from
rgb2hex and hex2rgb

5.0

5.0 | 2 ratings Rate this file 56 Downloads (last 30 days) File Size: 18.2 KB File ID: #46289
image thumbnail

rgb2hex and hex2rgb

by

 

18 Apr 2014 (Updated )

Convert colors between rgb and hex values

| Watch this File

File Information
Description

Simply convert between hex color values and rgb color values. These two functions can handle arrays as inputs. Default rgb values are scaled from 0 to 1 to match Matlab's syntax. However, if you'd like to use RGB values scaled from 0 to 255, that'll work too.
SYNTAX:
rgb = hex2rgb(hex) returns rgb color values in an n x 3 array. Values are scaled from 0 to 1 by default.
                   
rgb = hex2rgb(hex,255) returns RGB values scaled from 0 to 255.

* * * * * * * * * * * * * * * * * * * *
EXAMPLES:

myrgbvalue = hex2rgb('#334D66')
   = 0.2000 0.3020 0.4000

myrgbvalue = hex2rgb('334D66') % <-the # sign is optional
   = 0.2000 0.3020 0.4000

myRGBvalue = hex2rgb('#334D66',255)
   = 51 77 102

myhexvalues = ['#334D66';'#8099B3';'#CC9933';'#3333E6'];
myrgbvalues = hex2rgb(myhexvalues)
   = 0.2000 0.3020 0.4000
       0.5020 0.6000 0.7020
       0.8000 0.6000 0.2000
       0.2000 0.2000 0.9020

myhexvalues = ['#334D66';'#8099B3';'#CC9933';'#3333E6'];
myRGBvalues = hex2rgb(myhexvalues,255)
   = 51 77 102
      128 153 179
      204 153 51
       51 51 230

********************************************************
THE OTHER FUNCTION
********************************************************
SYNTAX:
hex = rgb2hex(rgb) returns the hexadecimal color value of the n x 3 rgb values. rgb can be an array. This function assumes rgb values are in [r g b] format on the 0 to 1 scale. If, however, any value r, g, or b exceed 1, the function assumes [r g b] are scaled between 0 and 255.

* * * * * * * * * * * * * * * * * * * *
EXAMPLES:

myhexvalue = rgb2hex([0 1 0])
   = #00FF00

myhexvalue = rgb2hex([0 255 0])
   = #00FF00

myrgbvalues = [.2 .3 .4;
               .5 .6 .7;
               .8 .6 .2;
               .2 .2 .9];
myhexvalues = rgb2hex(myrgbvalues)
   = #334D66
     #8099B3
     #CC9933
     #3333E6

Acknowledgements

Rgbconv.M, Grayscale To Rgb Converter, Colormap And Colorbar Utilities (Jul 2014), Beautiful And Distinguishable Line Colors + Colormap, Hextorgb, and Hex2 Rgb inspired this file.

This file inspired Rgbmap Color Maps and Intuitive Rgb Color Values From Xkcd.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
13 Aug 2014 Stephen Cobeldick  
08 Aug 2014 Chad Greene

Stephen: you have given a perfect example of how feedback can be great without being good. Thank you for your thoughtful, helpful suggestions--I have incorporated them into the latest update.

08 Aug 2014 Stephen Cobeldick

Does what it says on the box. Clearly written, several helpful examples, with help text and an H1 line. I like the style: the author has provided relevant usage information to the end-user, and makes it clear that they have tried to make it useful, although the 'See also' line is a bit brief: "num2str", "sscanf", "dec2hex" as well, maybe?

The code could have a few tweaks to make it tidier, as the mlint message indicates...

- "hex2rgb" splits up the input matrix into separate vectors and parses these with "hex2dec", before merging these back together later. Keeping the matrix together makes it simpler and faster:
rgb = reshape(sscanf(hex.','%2x'),3,[]).';

- "hex2rgb" uses "exist" for the optional argument, "nargin" is faster.

- "rgb2hex" calls multiple "sprintf" instances within a "for" loop without any array preallocation: this will be slow for large inputs, but could be replaced with one call to "sprintf" and no loops (vectorized!):
hex(:,2:7) = reshape(sprintf('%02X',rgb.'),6,[]).';
hex(:,1) = '#';

Five stars for the help and clarity, three stars for the code.

Updates
08 Aug 2014

Increased speed and robustness, added an example file.

Contact us