How to color the colormap with a set of hexadecimal colors?
    26 ビュー (過去 30 日間)
  
       古いコメントを表示
    
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.
map = ('#E1F2E3','#CDE5D2','#9CCEA7','#6CBA7D','#40AD5A','#22BB3B','#06592A');
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 件のコメント
採用された回答
  Dave B
    
 2022 年 12 月 14 日
        
      編集済み: Dave B
    
 2022 年 12 月 14 日
  
      Starting in R2020b you can use the validatecolor function to convert hex to rgb (using the 'multiple' option to convert more than one color):
map = {'#E1F2E3','#CDE5D2','#9CCEA7','#6CBA7D','#40AD5A','#22BB3B','#06592A'};
cmap = validatecolor(map, 'multiple')
imagesc(peaks)
colormap(cmap)
colorbar
3 件のコメント
  Stephan
      
      
 2022 年 12 月 14 日
				For older versions: https://www.mathworks.com/matlabcentral/fileexchange/46289-rgb2hex-and-hex2rgb by @Chad Greene
  Toshia M
    
 2024 年 4 月 30 日
				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.
map = {'#E1F2E3','#CDE5D2','#9CCEA7','#6CBA7D','#40AD5A','#22BB3B','#06592A'};
cmap = hex2rgb(map)
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)
その他の回答 (1 件)
  Bora Eryilmaz
    
 2022 年 12 月 14 日
        
      編集済み: Bora Eryilmaz
    
 2022 年 12 月 14 日
  
      You can use a function like this in a loop:
map = {'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
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 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Color and Styling についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





