rgb2hex and hex2rgb
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
Citation pour cette source
Chad Greene (2024). rgb2hex and hex2rgb (https://www.mathworks.com/matlabcentral/fileexchange/46289-rgb2hex-and-hex2rgb), MATLAB Central File Exchange. Extrait(e) le .
Compatibilité avec les versions de MATLAB
Plateformes compatibles
Windows macOS LinuxCatégories
Tags
Remerciements
Inspiré par : rgbconv.m, Grayscale to RGB Converter, COLORMAP and COLORBAR utilities (Jul 2014), Beautiful and distinguishable line colors + colormap, hextorgb, HEX2RGB
A inspiré : BiofilmQ, colorpicker, EquiDistantColorGenerator, Intuitive RGB color values from XKCD, rgbmap color maps, Murphy Diagrams, image2palette: Simple K-means color clustering
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Découvrir Live Editor
Créez des scripts avec du code, des résultats et du texte formaté dans un même document exécutable.
rgb2hex_and_hex2rgb_documentation
Version | Publié le | Notes de version | |
---|---|---|---|
1.1.1 | Added live scripts |
||
1.1.0.0 | Increased speed and robustness, added an example file. |
||
1.0.0.0 |