MATLAB: How to make the own custom colormap

colormapcustom

Hello, how can I define my colormap to be with the following steps: (those are hex colors)
100% - #f8fe34
83% - #f4771e
68% - #d94015
44% - #148b3e
30% - #085a3f
15% - #41332d
0% - #010101

Best Answer

Step 1: Convert HEX to Decimal
>> hex = ['#f8fe34';'#f4771e';'#d94015';'#148b3e';'#085a3f';'#41332d';'#010101']
hex =
#f8fe34
#f4771e
#d94015
#148b3e
#085a3f
#41332d
#010101
>> map = sscanf(hex','#%2x%2x%2x',[3,size(hex,1)]).' / 255
map =
0.97255 0.99608 0.20392
0.95686 0.46667 0.11765
0.85098 0.25098 0.082353
0.078431 0.5451 0.24314
0.031373 0.35294 0.24706
0.2549 0.2 0.17647
0.0039216 0.0039216 0.0039216
>> rgbplot(map) % to view the values
The matrix map is a standard MATLAB colormap, and it can be used anywhere that you need a colormap. You could also interpolate its values (if you need more nodes), or set this as your default colormap, or many other operations.
Step 2: Interpolate
If you want to interpolate the colormap and place the specified nodes at the given "percent" locations:
vec = [ 100; 83; 68; 44; 30; 15; 0];
hex = ['#f8fe34';'#f4771e';'#d94015';'#148b3e';'#085a3f';'#41332d';'#010101'];
raw = sscanf(hex','#%2x%2x%2x',[3,size(hex,1)]).' / 255;
N = 128;
%N = size(get(gcf,'colormap'),1) % size of the current colormap
map = interp1(vec,raw,linspace(100,0,N),'pchip');
which looks like this: