makergb creates a numeric RGB color array from different (mixed) input elements, including 3- and 6-digit hexadecimal color codes and pre-defined color names.
makergb can handle multidimensional inputs and returns the RGB triplet between 0 and 1 along the highest dimension. It returns a (customizable) default value for missing or non-interpretable input elements.
Pre-defined color names comprise short and long names from Matlab's ColorSpec plus a few extensions including the current and previous Matlab default colors (overall 32 different colors, see image). The emphasis is not on exhaustive color name conversion; for that purpose see various other file exchange submissions, most notably https://de.mathworks.com/matlabcentral/fileexchange/48155-convert-between-rgb-and-color-names
Examples:
string input
>> rgb = makergb("orange")
rgb =
1.0000 0.5000 0
>> rgb = makergb("m"+(1:4)) % Matlab's first four default colors
rgb =
0 0.4470 0.7410
0.8500 0.3250 0.0980
0.9290 0.6940 0.1250
0.4940 0.1840 0.5560
char or cell array input
>> rgb = makergb(char('red','green','blue'))
rgb =
1 0 0
0 1 0
0 0 1
>> rgb = makergb({'red','green','blue','purple'})
rgb =
1 0 0
0 1 0
0 0 1
0.5 0 0.5
multidimensional input
>> rgb = makergb(["r","g";"y","k"])
rgb(:,:,1) = rgb(:,:,2) = rgb(:,:,3) =
1 0 0 1 0 0
1 0 1 0 0 0
mixed input color representations with a missing value, default set to white
>> [rgb,tfi] = makergb({'r',[0 1 0],'0000ff',"#F0F",[]}, 'w')
rgb =
1 0 0
0 1 0
0 0 1
1 0 1
1 1 1
tfi =
1×5 logical array
1 1 1 1 0
引用格式
Andres (2024). makergb (https://www.mathworks.com/matlabcentral/fileexchange/95168-makergb), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
创建方式
R2021a
兼容任何版本
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!