如何从两种颜色创建插值颜色图或调色板?
How to create an interpolated colormap or color palette from two colors?
我想在两种颜色之间创建一个调色板。例如在 Blue 和 Red 之间有 20 或 50 个实例。
如何在 Matlab R2014b 中实现这一点?
您可以使用任何类型的插值(例如 interp1
) to create your own custom colormap between two colors or multiple colors. A colormap is basically a 3-column matrix with RGB-values. In your case its pretty simple, as you just need red with [1 0 0]
and blue [0 0 1]
and linearly interpolated in between. linspace
因此是最佳选择。
n = 50; %// number of colors
R = linspace(1,0,n); %// Red from 1 to 0
B = linspace(0,1,n); %// Blue from 0 to 1
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
请注意,您还可以通过键入 colormapeditor
.
使用颜色图 GUI
您也可以使用 2D-interpolation:
n = 50; %// number of colors
cmap(1,:) = [1 0 0]; %// color first row - red
cmap(2,:) = [0 1 0]; %// color 25th row - green
cmap(3,:) = [0 0 1]; %// color 50th row - blue
[X,Y] = meshgrid([1:3],[1:50]); %// mesh of indices
cmap = interp2(X([1,25,50],:),Y([1,25,50],:),cmap,X,Y); %// interpolate colormap
colormap(cmap) %// set color map
%// some example figure
figure(1)
surf(peaks)
colorbar
这只是另一个使用 样条插值 获得更宽的蓝色和红色区域的示例:
n = 50; %// number of colors
v = [0,0,0.1,0.5,0.9,1,1];
x = [-5*n,0, 0.45*n, 0.5*n, 0.55*n, n, 5*n];
xq = linspace(1,n,n);
vq = interp1(x,v,xq,'spline');
vq = vq - min(vq);
vq = vq./max(vq);
B = vq; %// Blue from 0 to 1 with spline shape
R = fliplr(B); %// Red as Blue but mirrored
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
或者使用任何你想要的数学函数:
n = 50; %// number of colors
t = linspace(0,4*pi,50);
B = sin(t)*0.5 + 0.5; %// Blue from 0 to 1 as sine
R = cos(t)*0.5 + 0.5; %// Red from 0 to 1 as cosine
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
我想在两种颜色之间创建一个调色板。例如在 Blue 和 Red 之间有 20 或 50 个实例。
如何在 Matlab R2014b 中实现这一点?
您可以使用任何类型的插值(例如 interp1
) to create your own custom colormap between two colors or multiple colors. A colormap is basically a 3-column matrix with RGB-values. In your case its pretty simple, as you just need red with [1 0 0]
and blue [0 0 1]
and linearly interpolated in between. linspace
因此是最佳选择。
n = 50; %// number of colors
R = linspace(1,0,n); %// Red from 1 to 0
B = linspace(0,1,n); %// Blue from 0 to 1
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
请注意,您还可以通过键入 colormapeditor
.
您也可以使用 2D-interpolation:
n = 50; %// number of colors
cmap(1,:) = [1 0 0]; %// color first row - red
cmap(2,:) = [0 1 0]; %// color 25th row - green
cmap(3,:) = [0 0 1]; %// color 50th row - blue
[X,Y] = meshgrid([1:3],[1:50]); %// mesh of indices
cmap = interp2(X([1,25,50],:),Y([1,25,50],:),cmap,X,Y); %// interpolate colormap
colormap(cmap) %// set color map
%// some example figure
figure(1)
surf(peaks)
colorbar
这只是另一个使用 样条插值 获得更宽的蓝色和红色区域的示例:
n = 50; %// number of colors
v = [0,0,0.1,0.5,0.9,1,1];
x = [-5*n,0, 0.45*n, 0.5*n, 0.55*n, n, 5*n];
xq = linspace(1,n,n);
vq = interp1(x,v,xq,'spline');
vq = vq - min(vq);
vq = vq./max(vq);
B = vq; %// Blue from 0 to 1 with spline shape
R = fliplr(B); %// Red as Blue but mirrored
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar
或者使用任何你想要的数学函数:
n = 50; %// number of colors
t = linspace(0,4*pi,50);
B = sin(t)*0.5 + 0.5; %// Blue from 0 to 1 as sine
R = cos(t)*0.5 + 0.5; %// Red from 0 to 1 as cosine
G = zeros(size(R)); %// Green all zero
colormap( [R(:), G(:), B(:)] ); %// create colormap
%// some example figure
figure(1)
surf(peaks)
colorbar