# How to color the colormap with a set of hexadecimal colors?

85 vues (au cours des 30 derniers jours)
Sujata Dhar le 14 Déc 2022
Commenté : Toshia M le 30 Avr 2024 à 22:26
I want to colormap to plot data. I want to use a set of hexadecimal code for the colors of the plot. If I run with following code, I get the plot.
map=[1 0.9 0; 1 0.75 0; 1 0.625 0; 1 0.5 0; 1 0.375 0; 1 0.25 0; 1 0.125 0; 1 0 0];
colormap(map);
But instead I want to use the following set of hexadecimal codes for the plot, for eg.
But I run into error stating that
"Error using colormap
Colormap must have 3 columns: [R,G,B]."
How should I solve this issue, please.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Dave B le 14 Déc 2022
Modifié(e) : Dave B le 14 Déc 2022
Starting in R2020b you can use the validatecolor function to convert hex to rgb (using the 'multiple' option to convert more than one color):
cmap = validatecolor(map, 'multiple')
cmap = 7×3
0.8824 0.9490 0.8902 0.8039 0.8980 0.8235 0.6118 0.8078 0.6549 0.4235 0.7294 0.4902 0.2510 0.6784 0.3529 0.1333 0.7333 0.2314 0.0235 0.3490 0.1647
imagesc(peaks)
colormap(cmap)
colorbar
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Stephan le 14 Déc 2022
Toshia M le 30 Avr 2024 à 22:26
Starting in R2024a, you can use the hex2rgb function to convert hexadecimal color codes to RGB triplets. This provides an alternative way to create a colormap from a set of hexadecimal values, but you can also use the function any time you want to calculate the equivalent RGB triplet from a hexadecimal color code.
cmap = hex2rgb(map)
cmap = 7x3
0.8824 0.9490 0.8902 0.8039 0.8980 0.8235 0.6118 0.8078 0.6549 0.4235 0.7294 0.4902 0.2510 0.6784 0.3529 0.1333 0.7333 0.2314 0.0235 0.3490 0.1647
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can also go the other way by calling the rgb2hex function. For example, you can convert five colors from the parula colormap to hexadecimal color codes:
p5 = parula(5);
pHex = rgb2hex(p5)
pHex = 5x1 string array
"#3E26A8" "#347AFD" "#12BEB9" "#C8C129" "#F9FB15"

Connectez-vous pour commenter.

### Plus de réponses (1)

Bora Eryilmaz le 14 Déc 2022
Modifié(e) : Bora Eryilmaz le 14 Déc 2022
You can use a function like this in a loop:
map = 1×7 cell array
{'E1F2E3'} {'CDE5D2'} {'9CCEA7'} {'6CBA7D'} {'40AD5A'} {'22BB3B'} {'06592A'}
N = numel(map);
newMap = zeros(N,3);
for i = 1:N
newMap(i,:) = hex2rgb(map{i});
end
newMap
newMap = 7×3
225 242 227 205 229 210 156 206 167 108 186 125 64 173 90 34 187 59 6 89 42
function result = hex2rgb(h)
d = hex2dec(h);
r = bitshift(bitand(d, 0xFF0000), -16);
g = bitshift(bitand(d, 0x00FF00), -8);
b = bitshift(bitand(d, 0x0000FF), 0);
result = [r, g, b];
end
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Colormaps dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by