File indexing completed on 2024-04-28 15:35:14

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by Tomas Mecir                                     *
0003  *   kmuddy@kmuddy.org                                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU Library General Public License as       *
0007  *   published by the Free Software Foundation; either version 2 of the    *
0008  *   License, or (at your option) any later version.                       *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU Library General Public License for more details.                  *
0014  ***************************************************************************/
0015 #include "cmxpcolors.h"
0016 
0017 #include "colorlist.h"
0018 #include "rgbops.h"
0019 
0020 #include <string>
0021 
0022 #include <ctype.h>
0023 
0024 cMXPColors *cMXPColors::_self = 0;
0025 RGB cMXPColors::nocolor = {0, 0, 0};
0026 
0027 cMXPColors::cMXPColors ()
0028 {
0029   //fill in the colors mapping...
0030   for (int i = 0; i < NUM_MXP_COLORS; i++)
0031     addColor (COLOR_NAMES[i], COLOR_DEF[i]);
0032 }
0033 
0034 cMXPColors::~cMXPColors ()
0035 {
0036   //clear the colors mapping
0037   colors.clear ();
0038   //instance doesn't exist any more
0039   _self = 0;
0040 }
0041 
0042 cMXPColors * cMXPColors::self ()
0043 {
0044   //returns an instance, creating it if it doesn't exist
0045   if (!_self)
0046     _self = new cMXPColors;
0047   return _self;
0048 }
0049 
0050 void cMXPColors::addColor (const string &color, RGB def)
0051 {
0052   //will overwrite existing color, if any
0053   colors[color] = def;
0054 }
0055 
0056 void cMXPColors::removeColor (const string &color)
0057 {
0058   colors.erase (color);
0059 }
0060 
0061 RGB cMXPColors::color (const string &color)
0062 {
0063   string clr = color;
0064   //conversion to lowercase
0065   for (int i = 0; i < clr.length(); i++)
0066     clr[i] = tolower (clr[i]);
0067 
0068   //color in style #rrggbb
0069   if ((clr.length() == 7) && (clr[0] == '#'))
0070   {
0071     //check if all digits are correct
0072     bool okay = true;
0073     for (int i = 1; i <= 6; i++)
0074       if (!isxdigit (clr[i]))
0075         okay = false;
0076     //okay - parse and return the color
0077     if (okay)
0078     {
0079       char r1 = tolower (clr[1]);
0080       char r2 = tolower (clr[2]);
0081       char g1 = tolower (clr[3]);
0082       char g2 = tolower (clr[4]);
0083       char b1 = tolower (clr[5]);
0084       char b2 = tolower (clr[6]);
0085       r1 = (r1 <= '9') ? (r1 - '0') : (10 + (r1 - 'a'));
0086       r2 = (r2 <= '9') ? (r2 - '0') : (10 + (r2 - 'a'));
0087       g1 = (g1 <= '9') ? (g1 - '0') : (10 + (g1 - 'a'));
0088       g2 = (g2 <= '9') ? (g2 - '0') : (10 + (g2 - 'a'));
0089       b1 = (b1 <= '9') ? (b1 - '0') : (10 + (b1 - 'a'));
0090       b2 = (b2 <= '9') ? (b2 - '0') : (10 + (b2 - 'a'));
0091       RGB col;
0092       col.r = r1 * 16 + r2;
0093       col.g = g1 * 16 + g2;
0094       col.b = b1 * 16 + b2;
0095       
0096       return col;
0097     }
0098   }
0099   //one of pre-defined colors
0100   if (colors.count (clr))
0101     return colors[clr];
0102   return nocolor;
0103 }