How to get shades and tints of a color hex with Matlab?

35 次查看(过去 30 天)
How can I get shades and tints of a color hex in an automatic way (maybe still represented as color hex, or also color rgb)? Is there any in-built function?
For example shades and tints of strong pink (#f907ff) ?
% input
strong_pink = '#f907ff';
% output (as in https://www.color-hex.com/color/f907ff)
shades = {
'#f907ff', % <-- input
'#e006e5',
'#c705cc',
'#ae04b2',
'#950499',
'#7c037f',
'#630266',
'#4a024c',
'#310133',
'#180019',
'#000000'};
tints = {
'#f907ff' % < -- input
'#f91fff'
'#fa38ff'
'#fa51ff'
'#fb6aff'
'#fc83ff'
'#fc9bff'
'#fdb4ff'
'#fdcdff'
'#fee6ff'
'#ffffff'};

采纳的回答

DGM
DGM 2021-5-11
There are a number of ways you could do it. The simple way would be to just interpolate between that color tuple and white (or black) in RGB.
incolor = '#f907ff';
nsteps = 11;
% convert to a usable RGB tuple
incolor = incolor(incolor~='#');
incolor = hex2dec(reshape(incolor,2,3).').';
% calculate output colors
scale = linspace(1,0,nsteps);
shades = uint8(incolor.*scale.')
tints = uint8(incolor + (255-incolor).*fliplr(scale).')
% convert outputs to char hex tuples
shadeshex = [repmat('#',[nsteps 1]) reshape(dec2hex(reshape(shades.',[],1)).',6,[]).']
tintshex = [repmat('#',[nsteps 1]) reshape(dec2hex(reshape(tints.',[],1)).',6,[]).']
This is essentially the same method used by these tools:
You could also linearize the RGB values in some way prior to interpolating, or you could do the interpolation using a different color model. You could make it as complicated as you like. I didn't bother to see how exactly that website chose to do it.

更多回答(1 个)

Sim
Sim 2021-5-11
@DGM thanks a lot for your answer!!
However, the code you wrote here does not work for me, but I changed it and it is now working (I used just one file exchange to convert outputs to char hex tuples):
% input
incolor = '#f907ff' ;
nsteps = 11;
% convert to a usable RGB tuple
% https://ch.mathworks.com/matlabcentral/answers/458086-how-to-specify-line-color-using-a-hexadecimal-color-code
color = sscanf(incolor(2:end),'%2x%2x%2x',[1 3])/255;
% calculate output colors
scale = linspace(1,0,nsteps);
shades = scale .* color(:);
color_inv = (1-color);
tints = fliplr(scale) .* color_inv(:) + color(:);
% plot shades
figure
for i = 0 : nsteps-1
patch([0 10 10 0],[i i (i+1) (i+1)],shades(:,i+1)','edgecolor','none')
end
axis tight
% plot tints
figure
for i = 0 : nsteps-1
patch([0 10 10 0],[i i (i+1) (i+1)],tints(:,i+1)','edgecolor','none')
end
axis tight
% convert outputs to char hex tuples
% https://ch.mathworks.com/matlabcentral/fileexchange/46289-rgb2hex-and-hex2rgb
shades_hex = rgb2hex(shades')
tints_hex = rgb2hex(tints')
The result is the following:
shades_hex =
11×7 char array
'#F907FF'
'#E006E6'
'#C706CC'
'#AE05B3'
'#950499'
'#7D0480'
'#640366'
'#4B024D'
'#320133'
'#190119'
'#000000'
tints_hex =
11×7 char array
'#F907FF'
'#FA20FF'
'#FA39FF'
'#FB51FF'
'#FB6AFF'
'#FC83FF'
'#FD9CFF'
'#FDB5FF'
'#FECDFF'
'#FEE6FF'
'#FFFFFF'
  2 个评论
DGM
DGM 2021-5-11
Hm. I wonder what was going wrong. FWIW, it worked for me in R2019b, but maybe there was some version dependence or array orientation issue. Either way, using the FEX tool certainly simplifies the syntax. Glad you got it working.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by