function varargout = colorlist(opt, varargin)
%COLORLIST  Reads color list file, retrieves cell array with names of available colors, or retrieve RGB values of specified color  
%
% The list of colors (including RGB values) is stored in global variable availableColors
%
% Options include:
%   
%   load - Loads color list from specified file name (if this command is not used first, the default color list is used)
%     colorlist('load','filename','c:\Delft3D\w32\muppet\settings\defaults\colors.def')
%     
%   getlist - Retrieves cell array with color names
%     colorlist('getlist')
%     colorlist('getlist','includenone',1) - includes 'none' at end of list
%
%   getrgb - Retrieves vector with RGB values of specified color
%     colorlist('getrgb','color','red')

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2012 Deltares
%
%       Maarten.vanOrmondt@deltares.nl
%
%   This library is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published by
%   the Free Software Foundation, either version 3 of the License, or
%   (at your option) any later version.
%
%   This library is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with this library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://www.OpenEarth.eu">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and 
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute 
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 05 Jun 2012
% Created with Matlab version: 7.9.0.529 (R2009b)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%%
global availableColors

includenone=0;
includeauto=0;
filename=[];
colorname=[];

for ii=1:length(varargin)
    if ischar(varargin{ii})
        switch lower(varargin{ii})
            case{'filename'}
                filename=varargin{ii+1};
            case{'includenone'}
                includenone=varargin{ii+1};
                if ischar(includenone)
                    if strcmpi(includenone(1),'y')
                        includenone=1;
                    else
                        includenone=0;
                    end
                end
            case{'includeauto'}
                includeauto=varargin{ii+1};
                if ischar(includeauto)
                    if strcmpi(includeauto(1),'y')
                        includeauto=1;
                    else
                        includeauto=0;
                    end
                end
            case{'color'}
                colorname=varargin{ii+1};
        end
    end
end

switch lower(opt)
    case{'load'}
        if ~isempty(filename)
            error('Error in colorlist. Specify file name.');
        end
        if ~exist(filename,'file')
            error('Error in colorlist. File does not exist.');
        end
        availableColors=[];
        txt=readcolorlist(filename);
        noc=0;
        for ii=1:size(txt,2)
            if isnan(str2double(txt{ii}))
                noc=noc+1;
                availableColors.list{noc}=txt{ii};
                availableColors.val(noc,1)=str2num(txt{ii+1})/255;
                availableColors.val(noc,2)=str2num(txt{ii+2})/255;
                availableColors.val(noc,3)=str2num(txt{ii+3})/255;
            end
        end
    case{'getlist'}
        if isempty(availableColors)
            availableColors=defaultcolorlist;
        end
        lst=availableColors.list;
        if includenone
            lst{end+1}='none';
        end
        if includeauto
            lst{end+1}='automatic';
        end
        varargout{1}=lst;
    case{'getrgb'}
        if isempty(availableColors)
            availableColors=defaultcolorlist;
        end
        if isempty(colorname)
            error('Error in colorlist. Please specify colorname.');
        end
        switch lower(colorname)
            case{'none'}
                rgb=[NaN NaN NaN];
            otherwise
                ii=strmatch(lower(colorname),lower(availableColors.list),'exact');
                if ~isempty(ii)
                    rgb=squeeze(availableColors.val(ii,:));
                else
                    warning(['Color ' colorname ' not found']);
                    rgb=[NaN NaN NaN];
                end
        end        
        if isnan(rgb(1))
            varargout{1}='none';
        else
            varargout{1}=rgb/255;
        end
end

%%
function clrs=defaultcolorlist

clrs.list= { ...
'Black', ...
'Red', ...
'Green', ...
'Blue', ...
'Yellow', ...
'Magenta', ...
'Cyan', ...
'White', ...
'LightBlue', ...
'LightGreen', ...
'Pink', ...
'Orange', ...
'Gray', ...
'DarkRed', ...
'DarkGreen', ...
'DarkBlue', ...
'DarkMagenta', ...
'Gray10', ...
'Gray20', ...
'Gray30', ...
'Gray40', ...
'Gray50', ...
'Gray60', ...
'Gray70', ...
'Gray80', ...
'Gray85', ...
'Gray90', ...
'Gray95', ...
'Gray98', ...
'OrangeRed', ...
'Gold', ...
'aquamarine', ...
'bisque', ...
'blueviolet', ...
'brown', ...
'burlywood', ...
'cadetblue', ...
'chartreuse', ...
'chocolate', ...
'cornsilk', ...
'crimson', ...
'darkcyan', ...
'darkkhaki', ...
'darkolivegreen', ...
'darkorchid', ...
'darksalmon', ...
'darkseagreen', ...
'darkturquoise', ...
'dodgerblue', ...
'forestgreen', ...
'fuchsia', ...
'greenyellow', ...
'hotpink', ...
'ivory', ...
'khaki', ...
'lawngreen', ...
'lemonchiffon', ...
'lightpink', ...
'lightseagreen', ...
'lightskyblue', ...
'lightsteelblue', ...
'lightyellow', ...
'lime', ...
'limegreen', ...
'midnightblue', ...
'moccasin', ...
'navy', ...
'olive', ...
'orchid', ...
'palegreen', ...
'paleturquoise', ...
'palevioletred', ...
'peachpuff', ...
'peru', ...
'plum', ...
'powderblue', ...
'purple', ...
'rosybrown', ...
'royalblue', ...
'saddlebrown', ...
'salmon', ...
'sandybrown', ...
'seagreen', ...
'silver', ...
'skyblue', ...
'springgreen', ...
'steelblue', ...
'tan', ...
'teal', ...
'thistle', ...
'tomato', ...
'turquoise', ...
'violet', ...
'wheat', ...
'whitesmoke', ...
'yellowgreen', ...
'pastelblue', ...
'pastelorange'};

clrs.val = ...
[  0    0    0 ; ...
255    0    0 ; ...
  0  255    0 ; ...
  0    0  255 ; ...
255  255    0 ; ...
255    0  255 ; ...
  0  255  255 ; ...
255  255  255 ; ...
200  200  255 ; ...
200  255  200 ; ...
255  179  255 ; ...
255  140    0 ; ...
128  128  128 ; ...
127    0    0 ; ...
  0  100    0 ; ...
  0    0  127 ; ...
127    0  105 ; ...
 25   25   25 ; ...
 50   50   50 ; ...
 75   75   75 ; ...
118  118  118 ; ...
128  128  128 ; ...
160  160  160 ; ...
175  175  175 ; ...
208  208  208 ; ...
215  215  215 ; ...
225  225  225 ; ...
235  235  235 ; ...
245  245  245 ; ...
255   99   71 ; ...
238  221  130 ; ...
127  255  212 ; ...
255  228  196 ; ...
138   43  226 ; ...
165   42   42 ; ...
222  184  135 ; ...
 95  158  160 ; ...
127  255    0 ; ...
210  105   30 ; ...
255  248  220 ; ...
220   20   60 ; ...
  0  139  139 ; ...
189  183  107 ; ...
 85  107   47 ; ...
153   50  204 ; ...
233  150  122 ; ...
143  188  143 ; ...
  0  206  209 ; ...
 30  144  255 ; ...
 34  139   34 ; ...
255    0  255 ; ...
173  255   47 ; ...
255  105  180 ; ...
255  255  240 ; ...
240  230  140 ; ...
124  252    0 ; ...
255  250  205 ; ...
255  182  193 ; ...
 32  178  170 ; ...
135  206  250 ; ...
176  196  222 ; ...
255  255  224 ; ...
  0  255    0 ; ...
 50  205   50 ; ...
 25   25  112 ; ...
255  228  181 ; ...
  0    0  128 ; ...
128  128    0 ; ...
218  112  214 ; ...
152  251  152 ; ...
175  238  238 ; ...
219  112  147 ; ...
255  218  185 ; ...
205  133   63 ; ...
221  160  221 ; ...
176  224  230 ; ...
128    0  128 ; ...
188  143  143 ; ...
 65  105  225 ; ...
139   69   19 ; ...
250  128  114 ; ...
244  164   96 ; ...
 46  139   87 ; ...
192  192  192 ; ...
135  206  235 ; ...
  0  255  127 ; ...
 70  130  180 ; ...
210  180  140 ; ...
  0  128  128 ; ...
216  191  216 ; ...
255   99   71 ; ...
 64  224  208 ; ...
238  130  238 ; ...
245  222  179 ; ...
245  245  245 ; ...
154  205   50 ; ...
155  205  255 ; ...
255  128   64];

%%
function txt=readcolorlist(FileName)

fid=fopen(FileName);

k=0;
for i=1:1000
    tx0=fgets(fid);
    if and(ischar(tx0), size(tx0>0))
        v0=strread(tx0,'%q');
    else
        v0='';
    end
    if size(v0,1)>0
        if strcmp(tx0(1),'#')==0
            v=strread(tx0,'%q');
            nowords=size(v,1);
            for j=1:nowords
                k=k+1;
                txt{k}=v{j};
            end
            clear v;
        end
    end
end

fclose(fid);

