本文介绍了如何从两种颜色创建插值的色彩图或调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两种颜色之间创建一个调色板。例如蓝色红色之间的20或50个实例。



如何在Matlab R2014b中实现?

解决方案

您可以使用任何类型的插值(例如:

  n = 50; %//颜色数量

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]); %//网格的索引

cmap = interp2(X([1,25,50],:),Y([1,25,50],:),cmap,X,Y) ; %//插值colormap
colormap(cmap)%//设置颜色映射

%//一些示例figure
figure(1)
surf b $ b colorbar





另一个使用 以获得更大范围的蓝色和红色:

  n = 50; %//颜色数量

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; %//蓝色从0到1与样条形状
R = fliplr(B); %// Red as Blue but mirrored
G = zeros(size(R)); %//绿色全零

colormap([R(:),G(:),B(:)]); %//创建colormap

%//一些示例figure
figure(1)
surf(peaks)
colorbar

/ p>




或使用任何您想要的数学函数:

  n = 50; %//颜色数量

t = linspace(0,4 * pi,50);

B = sin(t)* 0.5 + 0.5; %//蓝色从0到1作为正弦
R = cos(t)* 0.5 + 0.5; %//红色从0到1作为余弦
G = zeros(size(R)); %//绿色全零

colormap([R(:),G(:),B(:)]); %//创建colormap

%//一些示例figure
figure(1)
surf(peaks)
colorbar

/ p>

I'd like to create a color palette between two colors. For instance between Blue and Red with 20 or 50 instances.

How can this be achieved in Matlab R2014b?

解决方案

You can use any kind of interpolation (e.g. 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 is therefore the best choice.

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

Note that you could also use the the colormap GUI by typing colormapeditor.


Alternative you can also use 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

And just another example using spline-interpolation to get wider areas of blue and red:

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

Or use any mathematical function you want:

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

这篇关于如何从两种颜色创建插值的色彩图或调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 18:25