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?
% 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'};
댓글 수: 0
채택된 답변
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!