matlab - How can I convert a color name to a 3 element RGB vector? -


in many matlab plotting functions, can specify color either string or 3 element vector directly lists red, green, , blue values.

for instance, these 2 statements equivalent:

plot(x, y, 'color', 'r'); plot(x, y, 'color', [1 0 0]); 

there 8 colors can specified string value: 'r','g','b','c','m','y','k','w'. there matlab built-in function converts these strings equivalent rgb vector?

i found general alternative on mathworks file exchange handle color strings other default 8 in matlab:

if you're concerned conversions default 8 color strings, here's function wrote myself use convert , forth between rgb triples , short color names (i.e. single characters):

function outcolor = convert_color(incolor)    charvalues = 'rgbcmywk'.';  %#'   rgbvalues = [eye(3); 1-eye(3); 1 1 1; 0 0 0];   assert(~isempty(incolor),'convert_color:badinputsize',...          'input argument must not empty.');    if ischar(incolor)  %# input character string      [iscolor,colorindex] = ismember(incolor(:),charvalues);     assert(all(iscolor),'convert_color:badinputcontents',...            'string input can contain characters ''rgbcmywk''.');     outcolor = rgbvalues(colorindex,:);    elseif isnumeric(incolor) || islogical(incolor)  %# input numeric or                                                    %#   logical array     assert(size(incolor,2) == 3,'convert_color:badinputsize',...            'numeric input must n-by-3 matrix');     incolor = double(incolor);           %# convert input type double     scaleindex = max(incolor,[],2) > 1;  %# find rows values > 1     incolor(scaleindex,:) = incolor(scaleindex,:)./255;  %# scale 255     [iscolor,colorindex] = ismember(incolor,rgbvalues,'rows');     assert(all(iscolor),'convert_color:badinputcontents',...            'rgb input must define 1 of colors ''rgbcmywk''.');     outcolor = charvalues(colorindex(:));    else  %# input invalid type      error('convert_color:badinputtype',...           'input must character or numeric array.');    end 

note function allows input either string of characters or n-by-3 numeric or logical array (with rgb values 0 1 or 0 255) , returns opposite color representation. uses function ismember conversions.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -