Code covered by the BSD License  

Highlights from
GETRGB (v1.0)

GETRGB (v1.0)

by

 

04 Apr 2008 (Updated )

Get RGB triplet of matlab color name

getrgb(colorname)
function rgb = getrgb(colorname)
% GETRGB - RGB triplet for matlab color string
%
%   RGB = GETRGB(S) returns the 3-element vector RGB which is the
%   (Red,Green,Blue) triplet of the colorname S. S is a string holding a
%   color name that can be used by PLOT or by setting the color property of
%   graphic objects. If an invalid color name is specified, [NaN NaN NaN]
%   is returned. 
%
%   Examples:
%      getrgb('yellow') %--> [1 1 0]
%      getrgb('m')      %--> [1 0 1]  % magenta
%
%   See also PLOT, GRAPHIC HANDLES, SET

% Algorithm: try to set the default color of figures. If this works the
% color will be retrieved from this setting as RGB triplet. If it doesn't
% work, return a default answer. Note that this does not create a graphics
% object. 

% for Matlab R13+
% version 1.0 (apr 2008)
% (c) Jos van der Geest
% email: jos@jasen.nl
%
% History
% 1.0 (apr 2008) created as an alternative to str2rgb (FEX #19432) by
%                  Kenneth Eaton)

% input should be a string
if ~ischar(colorname) 
    error('getrgb:StringRequired', 'Color name should be a string') ;
end

% get default color
defaultcolor = get(0,'defaultfigurecolor') ;
try
    % try to apply the color
    set(0,'defaultfigurecolor',colorname) ;
    % ok, retrieve the rgb triplet
    rgb = get(0,'defaultfigurecolor') ;    
catch
    % oops, invalid matlab color
    warning('getRGB:InvalidName','Color name is not a valid matlab color') ;
    rgb = [NaN NaN NaN] ;
end

% be nice! restore default figure color
set(0,'defaultfigurecolor',defaultcolor) ;

Contact us