获取两个颜色之间的渐变色(二)(含Matlab代码)

获取两个颜色之间的渐变色(二)(含Matlab代码)

问题来源

给地形配色时,想要自制配色,用来给地形配置渐变色

改进

获取渐变色时,比上次的写的代码更加简洁、易懂

代码

clear;clc;close all;
% QQ截屏+C,可获取RGB值
C1 = [155,66,43];
C2 = [230,230,230];
GradientColor = make(C1,C2);
surf(peaks)
colormap(GradientColor/255)        % matlab 要求归一化的RGB值
colorbar
function GradientColor = make(C1,C2,~)
sign = C2 - C1;                    % 决定递增还是递减
for i=1:length(sign)               % 归化成正负号
    if sign(i) > 0
        SI(i) = 1;
        SI(i) = -1;
step = 1;                          % 步长
R = [C1(1)];
G = [C1(2)];
B = [C1(3)];
while abs(R(end)-C2(1)) > step
    R = [R;R(end)+step*SI(1)];
    G = [G;G(end)];
    B = [B;B(end)];
fprintf('%d %d %d\n',size(R,1),size(G,1),size(B,1))
while abs(G(end)-C2(2)) > step
    R = [R;R(end)];
    G = [G;G(end)+step*SI(2)];
    B = [B;B(end)];
fprintf('%d %d %d\n',size(R,1),size(G,1),size(B,1))
while abs(B(end)-C2(3)) > step
    R = [R;R(end)];
    G = [G;G(end)];
    B = [B;B(end)+step*SI(3)];