vtkNamedColors.cxx 39.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkNamedColors.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkNamedColors.h"

#include "vtkObjectFactory.h"
Andrew Maclean's avatar
Andrew Maclean committed
19
#include "vtkSmartPointer.h"
20 21

#include <map> // STL Header for the underlying color map
Andrew Maclean's avatar
Andrew Maclean committed
22
#include <vector> // STL Header for returning/storing color values
23
#include <sstream> // STL Header for parsing character arrays
Andrew Maclean's avatar
Andrew Maclean committed
24 25 26
#include <algorithm> // STL Header for transform
#include <iterator> // STL Header for back inserter
#include <cctype> // Header to ensure std::tolower is present
27 28 29 30 31 32 33 34 35
#include <cstdlib>

class vtkNamedColorsDataStore
{
public:
  vtkNamedColorsDataStore()
  {
    this->Init();
  }
Andrew Maclean's avatar
Andrew Maclean committed
36

37
  virtual ~vtkNamedColorsDataStore() = default;
38 39 40 41 42

public:
  //-----------------------------------------------------------------------------
  // Return a pointer to the internal color map.
  // Use with caution.
43
  // Likely uses could be for searching for colors that have the same
44
  // value but different names e.g. aqua and cyan.
Andrew Maclean's avatar
Andrew Maclean committed
45
  std::map<vtkStdString, vtkColor4ub> * GetColorMap()
46 47 48 49 50 51 52
  {
    return &this->colorMap;
  }

  //-----------------------------------------------------------------------------
  // Get the color by name.
  // The name is treated as being case-insensitive.
Andrew Maclean's avatar
Andrew Maclean committed
53 54 55
  // The color is returned as vtkColor4ub class.
  // Black is returned if the color is not found.
  void GetColor(const vtkStdString & name, vtkColor4ub & color)
56
  {
Andrew Maclean's avatar
Andrew Maclean committed
57
    for(int i = 0; i < 3; ++i)
58
    {
Andrew Maclean's avatar
Andrew Maclean committed
59
      color[i] = 0;
60
    }
Andrew Maclean's avatar
Andrew Maclean committed
61
    color[3] = 255;
62
    if ( !name.empty() )
63
    {
64
      vtkStdString s = this->ToLowercase(name);
Andrew Maclean's avatar
Andrew Maclean committed
65
      std::map<vtkStdString, vtkColor4ub>::iterator p = this->colorMap.find(s);
66
      if ( p != this->colorMap.end() )
67
      {
Andrew Maclean's avatar
Andrew Maclean committed
68
        color = p->second;
69
      }
70
    }
71 72 73 74 75
  }

  //-----------------------------------------------------------------------------
  // Get the color by name.
  // The name is treated as being case-insensitive.
Andrew Maclean's avatar
Andrew Maclean committed
76 77 78
  // The color is returned as vtkColor4ub class.
  // Black is returned if the color is not found.
  void GetColor(const vtkStdString & name, vtkColor4d & color)
79
  {
Andrew Maclean's avatar
Andrew Maclean committed
80 81 82
    vtkColor4ub c;
    this->GetColor(name,c);
    for(int i = 0; i < c.GetSize(); ++i)
83
    {
Andrew Maclean's avatar
Andrew Maclean committed
84
      color[i] = static_cast<double>(c[i] / 255.0);
85
    }
Andrew Maclean's avatar
Andrew Maclean committed
86 87 88 89 90 91 92 93 94 95 96 97
  }

  //-----------------------------------------------------------------------------
  // Get the color by name.
  // The name is treated as being case-insensitive.
  // The color is returned as vtkColor3ub class.
  // Black is returned if the color is not found.
  void GetColor(const vtkStdString & name, vtkColor3ub & color)
  {
    vtkColor4ub c;
    this->GetColor(name,c);
    for(int i = 0; i < color.GetSize(); ++i)
98
    {
Andrew Maclean's avatar
Andrew Maclean committed
99
      color[i] = c[i];
100
    }
Andrew Maclean's avatar
Andrew Maclean committed
101 102 103 104 105 106 107 108 109 110 111 112
  }

  //-----------------------------------------------------------------------------
  // Get the color by name.
  // The name is treated as being case-insensitive.
  // The color is returned as vtkColor3d class.
  // Black is returned if the color is not found.
  void GetColor(const vtkStdString & name, vtkColor3d & color)
  {
    vtkColor4d c;
    this->GetColor(name,c);
    for(int i = 0; i < color.GetSize(); ++i)
113
    {
Andrew Maclean's avatar
Andrew Maclean committed
114
      color[i] = c[i];
115
    }
116 117 118 119 120
  }

  //-----------------------------------------------------------------------------
  // Set the color by name.
  // The name is treated as being case-insensitive.
Andrew Maclean's avatar
Andrew Maclean committed
121
  // The color is a vtkColor4ub class.
122
  // No color is set if the name is empty or the color vector is not of size 4.
Andrew Maclean's avatar
Andrew Maclean committed
123
  void SetColor(const vtkStdString & name, const vtkColor4ub & color)
124
  {
Andrew Maclean's avatar
Andrew Maclean committed
125
    if ( !name.empty() && color.GetSize() == 4 )
126
    {
127 128
      vtkStdString s = this->ToLowercase(name);
      this->colorMap[s] = color;
129
    }
130 131 132 133 134
  }

  //-----------------------------------------------------------------------------
  // Set the color by name.
  // The name is treated as being case-insensitive.
Andrew Maclean's avatar
Andrew Maclean committed
135
  // The color is a vtkColor4d class.
136
  // No color is set if the name is empty or the color vector is not of size 4.
Andrew Maclean's avatar
Andrew Maclean committed
137
  void SetColor(const vtkStdString & name, const vtkColor4d & color)
138
  {
Andrew Maclean's avatar
Andrew Maclean committed
139
    if ( !name.empty() && color.GetSize() == 4 )
140
    {
141
      vtkStdString s = this->ToLowercase(name);
Andrew Maclean's avatar
Andrew Maclean committed
142 143 144 145 146 147
      unsigned char r, g, b, a;
      r = static_cast<unsigned char>(color.GetRed() * 255.0);
      g = static_cast<unsigned char>(color.GetGreen() * 255.0);
      b = static_cast<unsigned char>(color.GetBlue() * 255.0);
      a = static_cast<unsigned char>(color.GetAlpha() * 255.0);
      this->colorMap[s] = vtkColor4ub(r,g,b,a);
148
    }
Andrew Maclean's avatar
Andrew Maclean committed
149 150 151 152 153 154 155 156 157 158 159
  }

  //-----------------------------------------------------------------------------
  // Set the color by name.
  // The name is treated as being case-insensitive.
  // The color is a vtkColor3ub class.
  // No color is set if the name is empty or the color vector is not of size 4.
  void SetColor(const vtkStdString & name, const vtkColor3ub & color)
  {
    vtkColor4ub c;
    for(int i = 0; i < 3; ++i)
160
    {
Andrew Maclean's avatar
Andrew Maclean committed
161
      c[i] = color[i];
162
    }
Andrew Maclean's avatar
Andrew Maclean committed
163 164 165 166 167 168 169 170 171 172 173 174 175
    c[3] = 255;
    this->SetColor(name,c);
  }

  //-----------------------------------------------------------------------------
  // Set the color by name.
  // The name is treated as being case-insensitive.
  // The color is a vtkColor3d class.
  // No color is set if the name is empty or the color vector is not of size 4.
  void SetColor(const vtkStdString & name, const vtkColor3d & color)
  {
    vtkColor4d c;
    for(int i = 0; i < 3; ++i)
176
    {
Andrew Maclean's avatar
Andrew Maclean committed
177
        c[i] = color[i];
178
    }
179
    c[3] = 1.0;
Andrew Maclean's avatar
Andrew Maclean committed
180
    this->SetColor(name,c);
181 182 183 184 185 186 187 188
  }

  //-----------------------------------------------------------------------------
  // Remove the color by name.
  // The name is treated as being case-insensitive.
  void RemoveColor(const vtkStdString & name)
  {
    if (!name.empty())
189
    {
190
      vtkStdString s = this->ToLowercase(name);
Andrew Maclean's avatar
Andrew Maclean committed
191
      std::map<vtkStdString, vtkColor4ub>::iterator p = this->colorMap.find(s);
192
      if ( p != this->colorMap.end() )
193
      {
Andrew Maclean's avatar
Andrew Maclean committed
194
        this->colorMap.erase(p);
195
      }
196
    }
197 198 199 200 201 202 203 204 205 206
  }

  //-----------------------------------------------------------------------------
  // Return true if the color exists.
  bool ColorExists(const vtkStdString & name)
  {
    vtkStdString s = this->ToLowercase(name);
    return this->colorMap.find(s) != this->colorMap.end();
  }

Andrew Maclean's avatar
Andrew Maclean committed
207 208 209 210 211
//-----------------------------------------------------------------------------
// Return a vector of color names.
std::vector<vtkStdString> GetColorNames()
{
  std::vector<vtkStdString> colorNames;
Andrew Maclean's avatar
Andrew Maclean committed
212
  for(std::map<vtkStdString, vtkColor4ub>::const_iterator p =
Andrew Maclean's avatar
Andrew Maclean committed
213 214
    this->GetColorMap()->begin();
    p != this->GetColorMap()->end(); ++p )
215
  {
Andrew Maclean's avatar
Andrew Maclean committed
216
    colorNames.push_back(p->first);
217
  }
Andrew Maclean's avatar
Andrew Maclean committed
218 219 220 221 222 223 224 225 226 227
  return colorNames;
}

//-----------------------------------------------------------------------------
// Return a vector where each element of the vector is a vector of
// synonyms such as cyan/aqua and magenta/fuchsia
// Warning this could take a long time for very large color maps.
std::vector<std::vector<vtkStdString> > GetSynonyms()
{
  std::vector<vtkStdString> cn = this->GetColorNames();
Andrew Maclean's avatar
Andrew Maclean committed
228
  std::map<vtkStdString, std::vector<vtkStdString> > synonyms;
Andrew Maclean's avatar
Andrew Maclean committed
229 230
  for ( std::vector<vtkStdString>::const_iterator p =
    cn.begin(); p != cn.end(); ++p )
231
  {
Andrew Maclean's avatar
Andrew Maclean committed
232 233 234 235 236 237
    vtkColor4ub vu;
    this->GetColor(*p,vu);
    std::vector<vtkStdString> duplicates;
    for ( std::map<vtkStdString, vtkColor4ub>::const_iterator
      q = this->GetColorMap()->begin();
      q != this->GetColorMap()->end(); ++q )
238
    {
Andrew Maclean's avatar
Andrew Maclean committed
239
      if( *p != q->first && vu.Compare( q->second, 1 ) )
240
      {
Andrew Maclean's avatar
Andrew Maclean committed
241
        duplicates.push_back(q->first);
Andrew Maclean's avatar
Andrew Maclean committed
242
      }
243
    }
Andrew Maclean's avatar
Andrew Maclean committed
244
    if ( !duplicates.empty() )
245
    {
Andrew Maclean's avatar
Andrew Maclean committed
246 247 248
      bool alreadyInMap = false;
      for( std::vector<vtkStdString>::const_iterator r =
        duplicates.begin(); r != duplicates.end(); ++r)
249
      {
Andrew Maclean's avatar
Andrew Maclean committed
250
        if( synonyms.find(*r) != synonyms.end() )
251
        {
Andrew Maclean's avatar
Andrew Maclean committed
252 253
          alreadyInMap = true;
          break;
Andrew Maclean's avatar
Andrew Maclean committed
254
        }
255
      }
Andrew Maclean's avatar
Andrew Maclean committed
256
      if ( !alreadyInMap )
257
      {
Andrew Maclean's avatar
Andrew Maclean committed
258 259
        synonyms[*p] = duplicates;
      }
Andrew Maclean's avatar
Andrew Maclean committed
260
    }
261
  }
Andrew Maclean's avatar
Andrew Maclean committed
262 263 264
  std::vector<std::vector<vtkStdString> > retVec;
  for(std::map<vtkStdString,std::vector<vtkStdString> >::const_iterator p =
    synonyms.begin(); p != synonyms.end(); ++p)
265
  {
Andrew Maclean's avatar
Andrew Maclean committed
266 267 268 269
    std::vector<vtkStdString> vstr;
    vstr.push_back(p->first);
    for(std::vector<vtkStdString>::const_iterator q =
      p->second.begin(); q!= p->second.end(); ++q)
270
    {
Andrew Maclean's avatar
Andrew Maclean committed
271
      vstr.push_back(*q);
Andrew Maclean's avatar
Andrew Maclean committed
272
    }
273 274
    retVec.push_back(vstr);
  }
Andrew Maclean's avatar
Andrew Maclean committed
275 276 277
  return retVec;
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
public:
  //-----------------------------------------------------------------------------
  // Initialise the color map by loading the colors from a list.
  void Init()
  {
    // These are the standard names and corresponding colors.
    // This table is loaded into a map where the key is the name
    // and the vector corresponds to the RGBA values.
    // Warning: If you add more colors to this table ensure that
    //          the format exactly matches those below.
    //          If you do not, the parsing into the map will most
    //          likely fail.
    //          The names you add can have spaces in them however
    //          the names MUST be all lower case.
    // Note: The colors in the last part of this table are from
    //       http://en.wikipedia.org/wiki/Web_colors.
    //       The names and values here will tale precedence over
    //       earlier names.
Andrew Maclean's avatar
Andrew Maclean committed
296
static const char *colorTable[] =
297 298 299
    {
    // Name, R, G, B, A
    // These colors should be exactly the same as the ones defined in
300
    // colors.py.
301
    //  Whites
Andrew Maclean's avatar
Andrew Maclean committed
302
    "antique_white,250,235,215,255",
303
    "azure,240,255,255,255",
Andrew Maclean's avatar
Andrew Maclean committed
304 305 306 307
    "bisque,255,228,196,255",
    "blanched_almond,255,235,205,255",
    "cornsilk,255,248,220,255",
    "eggshell,252,230,201,255",
308
    "floral_white,255,250,240,255",
Andrew Maclean's avatar
Andrew Maclean committed
309 310
    "gainsboro,220,220,220,255",
    "ghost_white,248,248,255,255",
311 312 313 314
    "honeydew,240,255,240,255",
    "ivory,255,255,240,255",
    "lavender,230,230,250,255",
    "lavender_blush,255,240,245,255",
Andrew Maclean's avatar
Andrew Maclean committed
315
    "lemon_chiffon,255,250,205,255",
316 317
    "linen,250,240,230,255",
    "mint_cream,245,255,250,255",
Andrew Maclean's avatar
Andrew Maclean committed
318 319 320
    "misty_rose,255,228,225,255",
    "moccasin,255,228,181,255",
    "navajo_white,255,222,173,255",
321 322
    "old_lace,253,245,230,255",
    "papaya_whip,255,239,213,255",
Andrew Maclean's avatar
Andrew Maclean committed
323 324
    "peach_puff,255,218,185,255",
    "seashell,255,245,238,255",
325
    "snow,255,250,250,255",
Andrew Maclean's avatar
Andrew Maclean committed
326 327
    "thistle,216,191,216,255",
    "titanium_white,252,255,240,255",
328 329 330 331 332
    "wheat,245,222,179,255",
    "white,255,255,255,255",
    "white_smoke,245,245,245,255",
    "zinc_white,252,247,255,255",
    //  Greys
Andrew Maclean's avatar
Andrew Maclean committed
333
    "cold_grey,128,138,135,255",
334
    "dim_grey,105,105,105,255",
Andrew Maclean's avatar
Andrew Maclean committed
335
    "grey,192,192,192,255",
336
    "light_grey,211,211,211,255",
Andrew Maclean's avatar
Andrew Maclean committed
337 338 339 340
    "slate_grey,112,128,144,255",
    "slate_grey_dark,47,79,79,255",
    "slate_grey_light,119,136,153,255",
    "warm_grey,128,128,105,255",
341 342
    //  Blacks
    "black,0,0,0,255",
Andrew Maclean's avatar
Andrew Maclean committed
343 344
    "ivory_black,41,36,33,255",
    "lamp_black,46,71,59,255",
345
    //  Reds
Andrew Maclean's avatar
Andrew Maclean committed
346 347 348 349
    "alizarin_crimson,227,38,54,255",
    "brick,156,102,31,255",
    "cadmium_red_deep,227,23,13,255",
    "coral,255,127,80,255",
350
    "coral_light,240,128,128,255",
Andrew Maclean's avatar
Andrew Maclean committed
351 352 353 354
    "deep_pink,255,20,147,255",
    "english_red,212,61,26,255",
    "firebrick,178,34,34,255",
    "geranium_lake,227,18,48,255",
355
    "hot_pink,255,105,180,255",
Andrew Maclean's avatar
Andrew Maclean committed
356 357 358 359 360 361 362
    "indian_red,176,23,31,255",
    "light_salmon,255,160,122,255",
    "madder_lake_deep,227,46,48,255",
    "maroon,176,48,96,255",
    "pink,255,192,203,255",
    "pink_light,255,182,193,255",
    "raspberry,135,38,87,255",
363
    "red,255,0,0,255",
Andrew Maclean's avatar
Andrew Maclean committed
364
    "rose_madder,227,54,56,255",
365
    "salmon,250,128,114,255",
Andrew Maclean's avatar
Andrew Maclean committed
366 367
    "tomato,255,99,71,255",
    "venetian_red,212,26,31,255",
368
    //  Browns
Andrew Maclean's avatar
Andrew Maclean committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    "beige,163,148,128,255",
    "brown,128,42,42,255",
    "brown_madder,219,41,41,255",
    "brown_ochre,135,66,31,255",
    "burlywood,222,184,135,255",
    "burnt_sienna,138,54,15,255",
    "burnt_umber,138,51,36,255",
    "chocolate,210,105,30,255",
    "deep_ochre,115,61,26,255",
    "flesh,255,125,64,255",
    "flesh_ochre,255,87,33,255",
    "gold_ochre,199,120,38,255",
    "greenish_umber,255,61,13,255",
    "khaki,240,230,140,255",
    "khaki_dark,189,183,107,255",
    "light_beige,245,245,220,255",
    "peru,205,133,63,255",
386
    "rosy_brown,188,143,143,255",
Andrew Maclean's avatar
Andrew Maclean committed
387 388 389
    "raw_sienna,199,97,20,255",
    "raw_umber,115,74,18,255",
    "sepia,94,38,18,255",
390
    "sienna,160,82,45,255",
Andrew Maclean's avatar
Andrew Maclean committed
391 392 393
    "saddle_brown,139,69,19,255",
    "sandy_brown,244,164,96,255",
    "tan,210,180,140,255",
394 395
    "van_dyke_brown,94,38,5,255",
    //  Oranges
Andrew Maclean's avatar
Andrew Maclean committed
396 397
    "cadmium_orange,255,97,3,255",
    "cadmium_red_light,255,3,13,255",
398
    "carrot,237,145,33,255",
Andrew Maclean's avatar
Andrew Maclean committed
399 400 401 402
    "dark_orange,255,140,0,255",
    "mars_orange,150,69,20,255",
    "mars_yellow,227,112,26,255",
    "orange,255,128,0,255",
403
    "orange_red,255,69,0,255",
Andrew Maclean's avatar
Andrew Maclean committed
404
    "yellow_ochre,227,130,23,255",
405
    //  Yellows
Andrew Maclean's avatar
Andrew Maclean committed
406 407 408 409 410 411 412 413 414 415 416 417 418
    "aureoline_yellow,255,168,36,255",
    "banana,227,207,87,255",
    "cadmium_lemon,255,227,3,255",
    "cadmium_yellow,255,153,18,255",
    "cadmium_yellow_light,255,176,15,255",
    "gold,255,215,0,255",
    "goldenrod,218,165,32,255",
    "goldenrod_dark,184,134,11,255",
    "goldenrod_light,250,250,210,255",
    "goldenrod_pale,238,232,170,255",
    "light_goldenrod,238,221,130,255",
    "melon,227,168,105,255",
    "naples_yellow_deep,255,168,18,255",
419
    "yellow,255,255,0,255",
Andrew Maclean's avatar
Andrew Maclean committed
420
    "yellow_light,255,255,224,255",
421
    //  Greens
Andrew Maclean's avatar
Andrew Maclean committed
422 423 424 425 426 427
    "chartreuse,127,255,0,255",
    "chrome_oxide_green,102,128,20,255",
    "cinnabar_green,97,179,41,255",
    "cobalt_green,61,145,64,255",
    "emerald_green,0,201,87,255",
    "forest_green,34,139,34,255",
428 429
    "green,0,255,0,255",
    "green_dark,0,100,0,255",
Andrew Maclean's avatar
Andrew Maclean committed
430 431 432 433 434 435 436 437
    "green_pale,152,251,152,255",
    "green_yellow,173,255,47,255",
    "lawn_green,124,252,0,255",
    "lime_green,50,205,50,255",
    "mint,189,252,201,255",
    "olive,59,94,43,255",
    "olive_drab,107,142,35,255",
    "olive_green_dark,85,107,47,255",
438
    "permanent_green,10,201,43,255",
Andrew Maclean's avatar
Andrew Maclean committed
439
    "sap_green,48,128,20,255",
440 441
    "sea_green,46,139,87,255",
    "sea_green_dark,143,188,143,255",
Andrew Maclean's avatar
Andrew Maclean committed
442 443 444 445
    "sea_green_medium,60,179,113,255",
    "sea_green_light,32,178,170,255",
    "spring_green,0,255,127,255",
    "spring_green_medium,0,250,154,255",
446
    "terre_verte,56,94,15,255",
Andrew Maclean's avatar
Andrew Maclean committed
447 448
    "viridian_light,110,255,112,255",
    "yellow_green,154,205,50,255",
449
    //  Cyans
Andrew Maclean's avatar
Andrew Maclean committed
450 451
    "aquamarine,127,255,212,255",
    "aquamarine_medium,102,205,170,255",
452
    "cyan,0,255,255,255",
Andrew Maclean's avatar
Andrew Maclean committed
453 454 455 456 457
    "cyan_white,224,255,255,255",
    "turquoise,64,224,208,255",
    "turquoise_dark,0,206,209,255",
    "turquoise_medium,72,209,204,255",
    "turquoise_pale,175,238,238,255",
458
    //  Blues
Andrew Maclean's avatar
Andrew Maclean committed
459
    "alice_blue,240,248,255,255",
460
    "blue,0,0,255,255",
Andrew Maclean's avatar
Andrew Maclean committed
461 462 463 464 465 466 467 468 469 470
    "blue_light,173,216,230,255",
    "blue_medium,0,0,205,255",
    "cadet,95,158,160,255",
    "cobalt,61,89,171,255",
    "cornflower,100,149,237,255",
    "cerulean,5,184,204,255",
    "dodger_blue,30,144,255,255",
    "indigo,8,46,84,255",
    "manganese_blue,3,168,158,255",
    "midnight_blue,25,25,112,255",
471
    "navy,0,0,128,255",
Andrew Maclean's avatar
Andrew Maclean committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485
    "peacock,51,161,201,255",
    "powder_blue,176,224,230,255",
    "royal_blue,65,105,225,255",
    "slate_blue,106,90,205,255",
    "slate_blue_dark,72,61,139,255",
    "slate_blue_light,132,112,255,255",
    "slate_blue_medium,123,104,238,255",
    "sky_blue,135,206,235,255",
    "sky_blue_deep,0,191,255,255",
    "sky_blue_light,135,206,250,255",
    "steel_blue,70,130,180,255",
    "steel_blue_light,176,196,222,255",
    "turquoise_blue,0,199,140,255",
    "ultramarine,18,10,143,255",
486
    //  Magentas
Andrew Maclean's avatar
Andrew Maclean committed
487
    "blue_violet,138,43,226,255",
488 489
    "cobalt_violet_deep,145,33,158,255",
    "magenta,255,0,255,255",
Andrew Maclean's avatar
Andrew Maclean committed
490
    "orchid,218,112,214,255",
491
    "orchid_dark,153,50,204,255",
Andrew Maclean's avatar
Andrew Maclean committed
492 493
    "orchid_medium,186,85,211,255",
    "permanent_red_violet,219,38,69,255",
494 495
    "plum,221,160,221,255",
    "purple,160,32,240,255",
Andrew Maclean's avatar
Andrew Maclean committed
496 497 498
    "purple_medium,147,112,219,255",
    "ultramarine_violet,92,36,110,255",
    "violet,143,94,153,255",
499
    "violet_dark,148,0,211,255",
Andrew Maclean's avatar
Andrew Maclean committed
500
    "violet_red,208,32,144,255",
501
    "violet_red_medium,199,21,133,255",
Andrew Maclean's avatar
Andrew Maclean committed
502
    "violet_red_pale,219,112,147,255",
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    // See: http://en.wikipedia.org/wiki/Web_colors
    // Red colors
    "IndianRed,205,92,92,255",
    "LightCoral,240,128,128,255",
    "Salmon,250,128,114,255",
    "DarkSalmon,233,150,122,255",
    "LightSalmon,255,160,122,255",
    "Red,255,0,0,255",
    "Crimson,220,20,60,255",
    "FireBrick,178,34,34,255",
    "DarkRed,139,0,0,255",
    // Pink colors
    "Pink,255,192,203,255",
    "LightPink,255,182,193,255",
    "HotPink,255,105,180,255",
    "DeepPink,255,20,147,255",
    "MediumVioletRed,199,21,133,255",
    "PaleVioletRed,219,112,147,255",
    // Orange colors
    "LightSalmon,255,160,122,255",
    "Coral,255,127,80,255",
    "Tomato,255,99,71,255",
    "OrangeRed,255,69,0,255",
    "DarkOrange,255,140,0,255",
    "Orange,255,165,0,255",
    // Yellow colors
    "Gold,255,215,0,255",
    "Yellow,255,255,0,255",
    "LightYellow,255,255,224,255",
    "LemonChiffon,255,250,205,255",
    "LightGoldenrodYellow,250,250,210,255",
    "PapayaWhip,255,239,213,255",
    "Moccasin,255,228,181,255",
    "PeachPuff,255,218,185,255",
    "PaleGoldenrod,238,232,170,255",
    "Khaki,240,230,140,255",
    "DarkKhaki,189,183,107,255",
    // Purple colors
    "Lavender,230,230,250,255",
    "Thistle,216,191,216,255",
    "Plum,221,160,221,255",
    "Violet,238,130,238,255",
    "Orchid,218,112,214,255",
    "Fuchsia,255,0,255,255",
    "Magenta,255,0,255,255",
    "MediumOrchid,186,85,211,255",
    "MediumPurple,147,112,219,255",
    "BlueViolet,138,43,226,255",
    "DarkViolet,148,0,211,255",
    "DarkOrchid,153,50,204,255",
    "DarkMagenta,139,0,139,255",
    "Purple,128,0,128,255",
    "Indigo,75,0,130,255",
    "DarkSlateBlue,72,61,139,255",
    "SlateBlue,106,90,205,255",
    "MediumSlateBlue,123,104,238,255",
    // Green colors
    "GreenYellow,173,255,47,255",
    "Chartreuse,127,255,0,255",
    "LawnGreen,124,252,0,255",
    "Lime,0,255,0,255",
    "LimeGreen,50,205,50,255",
    "PaleGreen,152,251,152,255",
    "LightGreen,144,238,144,255",
    "MediumSpringGreen,0,250,154,255",
    "SpringGreen,0,255,127,255",
    "MediumSeaGreen,60,179,113,255",
    "SeaGreen,46,139,87,255",
    "ForestGreen,34,139,34,255",
    "Green,0,128,0,255",
    "DarkGreen,0,100,0,255",
    "YellowGreen,154,205,50,255",
    "OliveDrab,107,142,35,255",
    "Olive,128,128,0,255",
    "DarkOliveGreen,85,107,47,255",
    "MediumAquamarine,102,205,170,255",
    "DarkSeaGreen,143,188,143,255",
    "LightSeaGreen,32,178,170,255",
    "DarkCyan,0,139,139,255",
    "Teal,0,128,128,255",
    // Blue/Cyan colors
    "Aqua,0,255,255,255",
    "Cyan,0,255,255,255",
    "LightCyan,224,255,255,255",
    "PaleTurquoise,175,238,238,255",
    "Aquamarine,127,255,212,255",
    "Turquoise,64,224,208,255",
    "MediumTurquoise,72,209,204,255",
    "DarkTurquoise,0,206,209,255",
    "CadetBlue,95,158,160,255",
    "SteelBlue,70,130,180,255",
    "LightSteelBlue,176,196,222,255",
    "PowderBlue,176,224,230,255",
    "LightBlue,173,216,230,255",
    "SkyBlue,135,206,235,255",
    "LightSkyBlue,135,206,250,255",
    "DeepSkyBlue,0,191,255,255",
    "DodgerBlue,30,144,255,255",
    "CornflowerBlue,100,149,237,255",
    "RoyalBlue,65,105,225,255",
    "Blue,0,0,255,255",
    "MediumBlue,0,0,205,255",
    "DarkBlue,0,0,139,255",
    "Navy,0,0,128,255",
    "MidnightBlue,25,25,112,255",
    // Brown colors
    "Cornsilk,255,248,220,255",
    "BlanchedAlmond,255,235,205,255",
    "Bisque,255,228,196,255",
    "NavajoWhite,255,222,173,255",
    "Wheat,245,222,179,255",
    "BurlyWood,222,184,135,255",
    "Tan,210,180,140,255",
    "RosyBrown,188,143,143,255",
    "SandyBrown,244,164,96,255",
    "Goldenrod,218,165,32,255",
    "DarkGoldenrod,184,134,11,255",
    "Peru,205,133,63,255",
    "Chocolate,210,105,30,255",
    "SaddleBrown,139,69,19,255",
    "Sienna,160,82,45,255",
    "Brown,165,42,42,255",
    "Maroon,128,0,0,255",
    // White colors
    "White,255,255,255,255",
    "Snow,255,250,250,255",
    "Honeydew,240,255,240,255",
    "MintCream,245,255,250,255",
    "Azure,240,255,255,255",
    "AliceBlue,240,248,255,255",
    "GhostWhite,248,248,255,255",
    "WhiteSmoke,245,245,245,255",
    "Seashell,255,245,238,255",
    "Beige,245,245,220,255",
    "OldLace,253,245,230,255",
    "FloralWhite,255,250,240,255",
    "Ivory,255,255,240,255",
    "AntiqueWhite,250,235,215,255",
    "Linen,250,240,230,255",
    "LavenderBlush,255,240,245,255",
    "MistyRose,255,228,225,255",
    // Gray colors
    "Gainsboro,220,220,220,255",
    "LightGrey,211,211,211,255",
    "Silver,192,192,192,255",
    "DarkGray,169,169,169,255",
    "Gray,128,128,128,255",
    "DimGray,105,105,105,255",
    "LightSlateGray,119,136,153,255",
    "SlateGray,112,128,144,255",
    "DarkSlateGray,47,79,79,255",
    "Black,0,0,0,255",
    };

Andrew Maclean's avatar
Andrew Maclean committed
657
    // Here we fill the color map.
658 659
    size_t colorTableSz = sizeof(colorTable)/sizeof(colorTable[0]);
    for (size_t i = 0; i < colorTableSz; ++i)
660
    {
Andrew Maclean's avatar
Andrew Maclean committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
      std::vector<unsigned char> color;
      vtkStdString s = colorTable[i];
      size_t idx = s.find(',');
      vtkStdString name = s.substr(0,idx);
      vtkStdString t = this->ToLowercase(name);
      name = t;
      size_t start = idx + 1;
      idx = s.find(',',start);
      t = s.substr(start,idx - start); // Red
      color.push_back(static_cast<unsigned char>(atoi(t.c_str())));
      start = idx + 1;
      idx = s.find(',',start);
      t = s.substr(start,idx - start); // Green
      color.push_back(static_cast<unsigned char>(atoi(t.c_str())));
      start = idx + 1;
      idx = s.find(',',start);
      t = s.substr(start,idx - start); // Blue
      color.push_back(static_cast<unsigned char>(atoi(t.c_str())));
      start = idx + 1;
      t = s.substr(start,s.size() - start); // Alpha
      color.push_back(static_cast<unsigned char>(atoi(t.c_str())));
      this->colorMap[name] = vtkColor4ub(color[0],color[1],color[2],color[3]);
683
    }
684 685 686 687 688 689 690
  }

private:
  //-----------------------------------------------------------------------------
  // Convert a string to lowercase.
  vtkStdString ToLowercase(const vtkStdString & str)
  {
Andrew Maclean's avatar
Andrew Maclean committed
691 692 693 694
    vtkStdString s;
    std::transform(str.begin(),str.end(),std::back_inserter(s),
                   (int (*)(int))std::tolower);
    return s;
695 696 697
  }

  // The names and color values.
Andrew Maclean's avatar
Andrew Maclean committed
698
  std::map<vtkStdString, vtkColor4ub> colorMap;
699 700
};

701 702 703

//------------------------------------------------------------------------------
// Helper class for parsing a string which defines a RGB or RGBA color.
704
class vtkColorStringParser
705 706
{
public:
707
  vtkColorStringParser(vtkNamedColors* namedColors)
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
  {
    this->Color.Set(0, 0, 0, 255);
    this->StateGood = true;
    this->NamedColors = namedColors;
  }

  bool Parse(const vtkStdString& colorString);

  const vtkColor4ub& GetColor() const
  {
    return this->Color;
  }

private:

  void RGBAFuncStringToRGBA(vtkStdString color,
                            vtkStdString::size_type pos,
                            unsigned int argCount);

  void HexStringToRGBA(vtkStdString color);

  // Convert a string to lowercase.
  vtkStdString ToLowercase(const vtkStdString & str)
  {
    vtkStdString s;
    std::transform(str.begin(),str.end(),std::back_inserter(s),
                   (int (*)(int))std::tolower);
    return s;
  }

private:
  vtkColor4ub Color;
  bool StateGood;
  vtkNamedColors* NamedColors;
};

//------------------------------------------------------------------------------
// Parse a color string in any of the following format:
// - #RGB
// - #RGBA
// - #RRGGBB
// - rgb(r, g, b)
// - rgba(r, g, b, a)
// - a CSS3 color name, e.g. "steelblue"
// If the passed string defines a color using one of the above formats returns
// true else returns false.
754
bool vtkColorStringParser::Parse(const vtkStdString& colorString)
755 756 757 758 759 760 761 762
{
  vtkStdString color = this->ToLowercase(colorString);

  // Skip heading spaces.
  vtkStdString::size_type pos = color.find_first_not_of(" \t");
  this->StateGood = (pos != vtkStdString::npos);

  if (this->StateGood)
763
  {
764
    if (color[pos] == '#')
765
    {
766 767 768
      // Trim trailing spaces.
      vtkStdString::size_type last = color.find_last_not_of(" \t");
      this->HexStringToRGBA(color.substr(pos + 1, last - pos));
769
    }
770
    else if (color.compare(pos, 4, "rgba") == 0)
771
    {
772
      this->RGBAFuncStringToRGBA(color, pos+4, 4);
773
    }
774
    else if (color.compare(pos, 3, "rgb") == 0)
775
    {
776 777
      this->RGBAFuncStringToRGBA(color, pos+3, 3);
      this->Color[3] = 255;
778
    }
779
    else
780
    {
781 782
      this->StateGood = this->NamedColors->ColorExists(color);
      if (this->StateGood)
783
      {
784 785 786
          this->NamedColors->GetColor(color, this->Color);
      }
    }
787
  }
788 789

  if (!this->StateGood)
790
  {
791
    this->Color.Set(0, 0, 0, 0);
792
  }
793 794 795 796 797 798 799
  return this->StateGood;
}

//------------------------------------------------------------------------------
unsigned char clip(long int value)
{
  if (value < 0)
800
  {
801
    value = 0;
802
  }
803
  else if (value > 255)
804
  {
805
    value = 255;
806
  }
807 808 809 810 811 812 813
  return static_cast<unsigned char>(value);
}

//------------------------------------------------------------------------------
double clip(double value)
{
  if (value < 0.0)
814
  {
815
    value = 0.0;
816
  }
817
  else if (value > 1.0)
818
  {
819
    value = 1.0;
820
  }
821 822 823 824 825 826 827 828 829 830
  return value;
}

//------------------------------------------------------------------------------
double clipPercentage(double value)
{
  return clip(value / 100.0);
}

//------------------------------------------------------------------------------
831
// Parse a string of type "#RRGGBB".
832 833
// Heading and trailing spaces must be already trimmed.
// If the parsed string is not valid set `StateGood` to false.
834
void vtkColorStringParser::HexStringToRGBA(vtkStdString color)
835 836 837 838 839 840
{
  // Check if it is valid hexadecimal representation.
  vtkStdString::size_type pos = color.find_first_not_of("0123456789abcdefABCDEF");
  this->StateGood = (pos == vtkStdString::npos);

  if (this->StateGood)
841
  {
842
    if (color.size() == 3) // #RGB -> #RRGGBB
843
    {
844
      color.resize(6);
845 846
      vtkStdString::size_type i = 3, j = 6;
      do
847
      {
848 849
        color[--j] = color[--i];
        color[--j] = color[i];
850 851
      } while (i != 0);
    }
852
    else if (color.size() != 6)
853
    {
854
      this->StateGood = false;
855
    }
856

857
    // Parse hex bytes.
858
    if (this->StateGood)
859
    {
860 861
      std::istringstream istr;
      int c;
862
      for (unsigned int i = 0; i < 3; ++i)
863 864 865 866 867 868
      {
        istr.str(color.substr(i * 2, 2));
        istr >> std::hex >> c;
        this->Color[i] = static_cast<unsigned char>(c);
        istr.clear();
      }
869 870
      this->Color[3] = 255;
    }
871
  }
872 873 874 875 876 877 878 879
}

//------------------------------------------------------------------------------
// Parse a string of type "rgb(r, g, b)" or a "rgba(r, g, b, a)" and return
// the parsed value in `Color`.
// r, g, b must be integral value in 0..255 or percentage values in 0.0%..100.0%
// a is a floating number in 0.0..1.0.
// If the parsed string is not valid set `StateGood` to false.
880
void vtkColorStringParser::RGBAFuncStringToRGBA(vtkStdString color,
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
                                             vtkStdString::size_type pos,
                                             unsigned int argCount)
{
  // Parse '('.
  pos = color.find_first_not_of(" \t", pos);
  this->StateGood = (pos != vtkStdString::npos && color[pos] == '(');

  // Check if percentage values are used.
  bool usePercentage = (color.find_first_of("%", pos+1) != vtkStdString::npos);

  // Count and replace delimiters.
  unsigned int delimiterCount = 0;
  for (vtkStdString::size_type i = pos + 1;
       (delimiterCount < argCount) && (i < color.size());
       ++i)
896
  {
897
    if (color[i] == ',')
898
    {
899 900 901
      color[i] = ' ';
      ++delimiterCount;
    }
902
  }
903 904 905 906 907 908 909 910
  this->StateGood = (delimiterCount + 1 == argCount);

  // Parse arguments.
  const char* start = color.c_str() + pos + 1;
  char* end;
  char** final = &end;

  if (!usePercentage)
911
  {
912 913
    long int value;
    for (unsigned int i = 0; this->StateGood && (i < 3); ++i)
914
    {
915 916 917 918 919
      value = strtol(start, final, 10);
      this->Color[i] = clip(value);
      this->StateGood = (start != end);
      start = end;
    }
920
  }
921
  else
922
  {
923 924
    double value;
    for (unsigned int i = 0; this->StateGood && (i < 3); ++i)
925
    {
926 927 928 929 930
      value = strtod(start, final);
      this->Color[i] = static_cast<unsigned char>(255 * clipPercentage(value));
      this->StateGood = (start != end && *end == '%');
      start = ++end;
    }
931
  }
932 933 934

  // Parse the alpha value if needed.
  if (this->StateGood && argCount == 4)
935
  {
936 937 938
    double value = strtod(start, final);
    this->Color[3] = static_cast<unsigned char>(255 * clip(value));
    this->StateGood = (start != end);
939
  }
940 941 942

  // Parse ')'.
  if (this->StateGood)
943
  {
944 945 946
    pos = end - color.c_str();
    pos = color.find_first_not_of(" \t", pos);
    this->StateGood = (pos != vtkStdString::npos && color[pos] == ')');
947
  }
948 949 950

  // Left characters must be only trailing spaces or the string is not valid.
  if (this->StateGood)
951
  {
952 953
    pos = color.find_first_not_of(" \t", pos + 1);
    this->StateGood = (pos == vtkStdString::npos);
954
  }
955 956 957
}


958 959 960 961 962 963
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkNamedColors);

//----------------------------------------------------------------------------
vtkNamedColors::vtkNamedColors()
{
964
  this->Colors = new vtkNamedColorsDataStore;
965
  this->Parser = new vtkColorStringParser(this);
966 967 968 969 970
}

//----------------------------------------------------------------------------
vtkNamedColors::~vtkNamedColors()
{
971
  delete this->Parser;
972
  delete this->Colors;
973 974 975 976 977 978 979
}

//-----------------------------------------------------------------------------

void vtkNamedColors::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
Andrew Maclean's avatar
Andrew Maclean committed
980
  for ( std::map<vtkStdString, vtkColor4ub >::const_iterator p =
981 982
    this->Colors->GetColorMap()->begin();
    p != this->Colors->GetColorMap()->end(); ++p )
983
  {
984
    os << indent << ": " << p->first << "(";
Andrew Maclean's avatar
Andrew Maclean committed
985
    if (p->second.GetSize() == 4)
986
    {
Andrew Maclean's avatar
Andrew Maclean committed
987 988 989 990 991 992
      int r = p->second.GetRed();
      int g = p->second.GetGreen();
      int b = p->second.GetBlue();
      int a = p->second.GetAlpha();
      os << setw(3) << r << "," << setw(3) << g
        << ","<< setw(3) << b <<"," << setw(3) << a;
993
    }
994 995
      os << ")" << endl;
  }
996 997 998
}

//-----------------------------------------------------------------------------
Andrew Maclean's avatar
Andrew Maclean committed
999
vtkStdString vtkNamedColors::GetColorNames()
1000
{
1001
  std::vector<vtkStdString> cnv = this->Colors->GetColorNames();
Andrew Maclean's avatar
Andrew Maclean committed
1002
  vtkStdString colorNames;
Andrew Maclean's avatar
Andrew Maclean committed
1003 1004 1005 1006 1007 1008
  // Get the last element in the vector.
  std::vector<vtkStdString>::iterator lastItr = cnv.end();
  --lastItr;
  // Now just iterate through the map getting the color names.
  for(std::vector<vtkStdString>::const_iterator p =
    cnv.begin(); p != cnv.end(); ++p )
1009
  {
Andrew Maclean's avatar
Andrew Maclean committed
1010 1011
    colorNames += *p;
    if (p != lastItr)
1012
    {
Andrew Maclean's avatar
Andrew Maclean committed
1013
      colorNames += "\n";
1014
    }
1015
  }
1016 1017 1018 1019
  return colorNames;
}

//-----------------------------------------------------------------------------
Andrew Maclean's avatar
Andrew Maclean committed
1020 1021 1022 1023 1024 1025
void vtkNamedColors::GetColorNames(vtkStringArray * colorNames)
{
  std::vector<vtkStdString> cnv = this->Colors->GetColorNames();
  // Now just iterate through the map getting the color names.
  for(std::vector<vtkStdString>::const_iterator p =
    cnv.begin(); p != cnv.end(); ++p )