[4] | 1 | function col = interpretColor(str) |
---|
| 2 | %interpretColor Interpret a color as an RGB triple |
---|
| 3 | % |
---|
| 4 | % rgb = uiextras.interpretColor(col) interprets the input color COL and |
---|
| 5 | % returns the equivalent RGB triple. COL can be one of: |
---|
| 6 | % * RGB triple of floating point numbers in the range 0 to 1 |
---|
| 7 | % * RGB triple of UINT8 numbers in the range 0 to 255 |
---|
| 8 | % * single character: 'r','g','b','m','y','c','k','w' |
---|
| 9 | % * string: one of 'red','green','blue','magenta','yellow','cyan','black' |
---|
| 10 | % 'white' |
---|
| 11 | % * HTML-style string (e.g. '#FF23E0') |
---|
| 12 | % |
---|
| 13 | % Examples: |
---|
| 14 | % >> uiextras.interpretColor( 'r' ) |
---|
| 15 | % ans = |
---|
| 16 | % 1 0 0 |
---|
| 17 | % >> uiextras.interpretColor( 'cyan' ) |
---|
| 18 | % ans = |
---|
| 19 | % 0 1 1 |
---|
| 20 | % >> uiextras.interpretColor( '#FF23E0' ) |
---|
| 21 | % ans = |
---|
| 22 | % 1.0000 0.1373 0.8784 |
---|
| 23 | % |
---|
| 24 | % See also: ColorSpec |
---|
| 25 | |
---|
| 26 | % Copyright 2005-2010 The MathWorks Ltd. |
---|
| 27 | % 1.1 |
---|
| 28 | % 2012/05/08 08:02:58 |
---|
| 29 | |
---|
| 30 | if ischar( str ) |
---|
| 31 | str = strtrim(str); |
---|
| 32 | str = dequote(str); |
---|
| 33 | if str(1)=='#' |
---|
| 34 | % HTML-style string |
---|
| 35 | if numel(str)==4 |
---|
| 36 | col = [hex2dec( str(2) ), hex2dec( str(3) ), hex2dec( str(4) )]/15; |
---|
| 37 | elseif numel(str)==7 |
---|
| 38 | col = [hex2dec( str(2:3) ), hex2dec( str(4:5) ), hex2dec( str(6:7) )]/255; |
---|
| 39 | else |
---|
| 40 | error( 'UIExtras:interpretColor:BadColor', 'Invalid HTML color %s', str ); |
---|
| 41 | end |
---|
| 42 | elseif all( ismember( str, '1234567890.,; []' ) ) |
---|
| 43 | % Try the '[0 0 1]' thing first |
---|
| 44 | col = str2num( str ); %#ok<ST2NM> |
---|
| 45 | if numel(col) == 3 |
---|
| 46 | % Conversion worked, so just check for silly values |
---|
| 47 | col(col<0) = 0; |
---|
| 48 | col(col>1) = 1; |
---|
| 49 | end |
---|
| 50 | else |
---|
| 51 | % that didn't work, so try the name |
---|
| 52 | switch upper(str) |
---|
| 53 | case {'R','RED'} |
---|
| 54 | col = [1 0 0]; |
---|
| 55 | case {'G','GREEN'} |
---|
| 56 | col = [0 1 0]; |
---|
| 57 | case {'B','BLUE'} |
---|
| 58 | col = [0 0 1]; |
---|
| 59 | case {'C','CYAN'} |
---|
| 60 | col = [0 1 1]; |
---|
| 61 | case {'Y','YELLOW'} |
---|
| 62 | col = [1 1 0]; |
---|
| 63 | case {'M','MAGENTA'} |
---|
| 64 | col = [1 0 1]; |
---|
| 65 | case {'K','BLACK'} |
---|
| 66 | col = [0 0 0]; |
---|
| 67 | case {'W','WHITE'} |
---|
| 68 | col = [1 1 1]; |
---|
| 69 | case {'N','NONE'} |
---|
| 70 | col = [nan nan nan]; |
---|
| 71 | otherwise |
---|
| 72 | % Failed |
---|
| 73 | error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) ); |
---|
| 74 | end |
---|
| 75 | end |
---|
| 76 | elseif isfloat(str) || isdouble(str) |
---|
| 77 | % Floating point, so should be a triple in range 0 to 1 |
---|
| 78 | if numel(str)==3 |
---|
| 79 | col = double( str ); |
---|
| 80 | col(col<0) = 0; |
---|
| 81 | col(col>1) = 1; |
---|
| 82 | else |
---|
| 83 | error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) ); |
---|
| 84 | end |
---|
| 85 | elseif isa(str,'uint8') |
---|
| 86 | % UINT8, so range is implicit |
---|
| 87 | if numel(str)==3 |
---|
| 88 | col = double( str )/255; |
---|
| 89 | col(col<0) = 0; |
---|
| 90 | col(col>1) = 1; |
---|
| 91 | else |
---|
| 92 | error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) ); |
---|
| 93 | end |
---|
| 94 | else |
---|
| 95 | error( 'UIExtras:interpretColor:BadColor', 'Could not interpret color %s', num2str( str ) ); |
---|
| 96 | end |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | function str = dequote(str) |
---|
| 100 | str(str=='''') = []; |
---|
| 101 | str(str=='"') = []; |
---|
| 102 | str(str=='[') = []; |
---|
| 103 | str(str==']') = []; |
---|