Difference between revisions of "VTK/Examples/Cxx/Visualization/NamedColorPatches"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
Line 13: Line 13:
 
Three helper classes are also used, these are defined and instantiated in an anonymous namespace in [[#NamedColorPatches.cxx|NamedColorPatches.cxx]].
 
Three helper classes are also used, these are defined and instantiated in an anonymous namespace in [[#NamedColorPatches.cxx|NamedColorPatches.cxx]].
  
==NamedColorPatchesDriver.cxx==
+
==NamedColorPatches.cxx==
 
<source lang="cpp">
 
<source lang="cpp">
#include <fstream>
 
#include <iostream>
 
#include <cstdlib>
 
 
#include "NamedColorPatches.h"
 
 
int main()
 
{
 
  NamedColorPatches ncp;
 
 
  std::ofstream outputFile;
 
  outputFile.open("VTKNamedColorPatches.html",
 
                    ios::out | ios::trunc | ios::binary);
 
  outputFile << ncp.MakeCombinedColorPage();
 
  outputFile.close();
 
  
  return EXIT_SUCCESS;
 
}
 
</source>
 
 
==NamedColorPatches.h==
 
<source lang="cpp">
 
 
#include <string>
 
#include <vector>
 
 
#include "vtkColor.h" // Needed for vtkColor[34]ub
 
 
#include <vtkNamedColors.h>
 
#include <vtkNamedColors.h>
 
#include <vtkSmartPointer.h>
 
#include <vtkSmartPointer.h>
 
class vtkNamedColors;
 
 
/*!
 
  This class creates HTML Tables displaying all the colors in
 
  the class vtkNamedColors grouped by various categories.
 
*/
 
class NamedColorPatches
 
{
 
 
  public:
 
    NamedColorPatches();
 
    virtual ~NamedColorPatches();
 
 
  public:
 
    std::string MakeHTMLHeader();
 
    std::string MakeHTMLIndex();
 
    std::string MakeTableHeader();
 
    std::string MakeSynonymTableHeader();
 
    std::string MakeTD(std::string const & name);
 
    std::string FormatRGBForHTML(vtkColor3ub const & rgb);
 
    std::string MakeTR(const std::string & name, vtkColor3ub const & rgb,
 
                      const std::string & textColor);
 
    std::string MakeSynonymTR(const std::string & name,
 
                      const std::string & nameColor,
 
                      vtkColor3ub const & rgb, const std::string & textColor);
 
 
    std::string MakeWebColorTables();
 
    std::string MakeVTKColorTables();
 
    std::string MakeSynonymColorTable();
 
    std::string MakeWebColorPage();
 
    std::string MakeVTKColorPage();
 
    std::string MakeSynonymColorPage();
 
    std::string MakeCombinedColorPage();
 
 
    std::vector<vtkStdString> ParseColorNames(const vtkStdString & colorNames);
 
    std::vector<std::vector<vtkStdString> > ParseSynonyms(
 
                                              const vtkStdString & synonyms);
 
 
  private:
 
    vtkSmartPointer<vtkNamedColors> nc;
 
};
 
</source>
 
 
==NamedColorPatches.cxx==
 
<source lang="cpp">
 
 
#include "NamedColorPatches.h"
 
  
 
#include <string>
 
#include <string>
Line 99: Line 25:
 
#include <functional>
 
#include <functional>
 
#include <iterator>
 
#include <iterator>
 +
#include <fstream>
 +
#include <iostream>
 +
#include <cstdlib>
 
#include <cctype>
 
#include <cctype>
 +
  
 
#ifdef WIN32
 
#ifdef WIN32
Line 495: Line 425:
 
}
 
}
  
//-----------------------------------------------------------------------------
+
/*!
NamedColorPatches::NamedColorPatches()
+
  This class creates HTML Tables displaying all the colors in
 +
  the class vtkNamedColors grouped by various categories.
 +
*/
 +
class NamedColorPatches
 
{
 
{
  this->nc = vtkSmartPointer<vtkNamedColors>::New();
 
}
 
  
//-----------------------------------------------------------------------------
+
  public:
NamedColorPatches::~NamedColorPatches()
+
    //-----------------------------------------------------------------------------
{
+
    NamedColorPatches()
}
+
    {
 +
      this->nc = vtkSmartPointer<vtkNamedColors>::New();
 +
    }
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::vector<vtkStdString> NamedColorPatches::ParseColorNames(
+
    ~NamedColorPatches()
                                                const vtkStdString & colorNames)
 
{
 
  // The delimiter for a color.
 
  const std::string colorDelimiter = "\n";
 
  std::vector<vtkStdString> cn;
 
  size_t start = 0;
 
  size_t end = colorNames.find(colorDelimiter);
 
  while (end != std::string::npos)
 
 
     {
 
     {
    cn.push_back(colorNames.substr(start, end - start));
 
    start = end + 1;
 
    end = colorNames.find(colorDelimiter, start);
 
 
     }
 
     }
   // Get the last color.
+
 
  if (!colorNames.empty())
+
   //-----------------------------------------------------------------------------
 +
    std::string MakeWebColorPage()
 
     {
 
     {
    cn.push_back(colorNames.substr(start, colorNames.size() - start));
+
      std::string res = this->MakeHTMLHeader();
 +
      res += "<body>\n";
 +
      res += "<h1>Colors available in vtkNamedColors</h1>\n";
 +
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 +
      res += "<caption style=\"background:lightgrey\">Web Color Names</caption>\n";
 +
      res += "<tr valign=\"top\">\n";
 +
      res += this->MakeWebColorTables();
 +
      res += "</table>\n";
 +
      res += "</body>\n";
 +
      return res;
 
     }
 
     }
  return cn;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::vector<std::vector<vtkStdString> > NamedColorPatches::ParseSynonyms(
+
    std::string MakeVTKColorPage()
                                                  const vtkStdString & synonyms)
 
{
 
  // The delimiter for a string of synonyms.
 
  const vtkStdString synonymDelimiter = "\n\n";
 
  size_t start = 0;
 
  size_t end = synonyms.find(synonymDelimiter);
 
  std::vector<vtkStdString> cn;
 
  std::vector<std::vector<vtkStdString> > syn;
 
  vtkStdString str;
 
  while (end != std::string::npos)
 
 
     {
 
     {
    str = synonyms.substr(start, end - start);
+
      std::string res = this->MakeHTMLHeader();
    cn = ParseColorNames(str);
+
      res += "<body>\n";
    syn.push_back(cn);
+
      res += "<h1>Colors available in vtkNamedColors</h1>\n";
    start = end + 2;
+
      res += "<p>The web colors take precedence over colors of the same";
    end = synonyms.find(synonymDelimiter, start);
+
      res += " name in VTK Color Names.</p>\n";
 +
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 +
      res += "<caption style=\"background:lightgrey\">VTK Color Names</caption>\n";
 +
      res += "<tr valign=\"top\">\n";
 +
      res += this->MakeVTKColorTables();
 +
      res += "</table>\n";
 +
      res += "</body>\n";
 +
      return res;
 
     }
 
     }
  // Get the last set of synonyms.
+
 
  if (!synonyms.empty())
+
    //-----------------------------------------------------------------------------
 +
    std::string MakeSynonymColorPage()
 
     {
 
     {
    str = synonyms.substr(start, end - start);
+
      std::string res = this->MakeHTMLHeader();
    cn = ParseColorNames(str);
+
      res += "<body>\n";
    syn.push_back(cn);
+
      res += "<h1>Synonyms in vtkNamedColors</h1>\n";
 +
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 +
      res += "<caption style=\"background:lightgrey\"></caption>\n";
 +
      res += "<tr valign=\"top\">\n";
 +
      res += this->MakeSynonymColorTable();
 +
      res += "</table>\n";
 +
      res += "</body>\n";
 +
      return res;
 
     }
 
     }
  return syn;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::MakeHTMLHeader()
+
    std::string MakeCombinedColorPage()
{
+
    {
  std::string s = "<!DOCTYPE html>\n";
+
      std::string res = this->MakeHTMLHeader();
  s += "<html lang=\"en\" dir=\"ltr\" class=\"client-nojs\">\n";
+
      res += "<body>\n";
  s += "<head>\n";
+
      res += "<h1>Colors available in vtkNamedColors</h1>\n";
  s += "<title>vtkNamedColors</title>\n";
+
      res += "<p>The class vtkNamedColors provides color names and their";
  s += "<meta charset=\"UTF-8\" />\n";
+
      res += " values for the convenience of the user.</p>\n";
  s += "</head>\n";
+
      res += "<p>The following tables show the available colors along with";
  return s;
+
      res += " their red, green and blue values.</p>\n";
}
+
      res += this->MakeHTMLIndex();
 +
      res += "<h2><a id=\"WebColorNames\">Web color Names</h2>";
 +
      res += "These colors correspond to those in";
 +
      res += " <a href=\"http://en.wikipedia.org/wiki/Web_colors\"";
 +
      res += " title=\"Web Colors\">Web Colors</a>.\n";
 +
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 +
      res += "<tr valign=\"top\">\n";
 +
      res += this->MakeWebColorTables();
 +
      res += "</table>\n";
 +
      res += "<h2><a id=\"VTKColorNames\">VTK color Names</h2>";
 +
      res += "<p>The colors mainly correspond to those in vtkColors.txt.\n</p>\n";
 +
      res += "<p>The web colors (above) take precedence over colors of the";
 +
      res += " same name in vtkColors.txt.</p>\n";
 +
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 +
      res += "<tr valign=\"top\">\n";
 +
      res += this->MakeVTKColorTables();
 +
      res += "</table>\n";
 +
      res += "<h2><a id=\"Synonyms\">Synonyms</h2>";
 +
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 +
      res += "<tr valign=\"top\">\n";
 +
      res += this->MakeSynonymColorTable();
 +
      res += "</table>\n";
 +
      res += "</body>\n";
 +
      return res;
 +
    }
  
//-----------------------------------------------------------------------------
+
  private:
std::string NamedColorPatches::MakeHTMLIndex()
+
    //-----------------------------------------------------------------------------
{
+
    std::vector<vtkStdString> ParseColorNames(
  std::string s = "<h2>Index</h2>\n";
+
                                              const vtkStdString & colorNames)
  s += "<ul>\n";
+
    {
  s += "\t<li><a href=\"#WebColorNames\">Web color Names</li>\n";
+
      // The delimiter for a color.
  s += "\t<li><a href=\"#VTKColorNames\">VTK color Names</li>\n";
+
      const std::string colorDelimiter = "\n";
  s += "\t<li><a href=\"#Synonyms\">Synonyms</li>\n";
+
      std::vector<vtkStdString> cn;
  s += "</ul>\n";
+
      size_t start = 0;
  return s;
+
      size_t end = colorNames.find(colorDelimiter);
}
+
      while (end != std::string::npos)
 +
        {
 +
        cn.push_back(colorNames.substr(start, end - start));
 +
        start = end + 1;
 +
        end = colorNames.find(colorDelimiter, start);
 +
        }
 +
      // Get the last color.
 +
      if (!colorNames.empty())
 +
        {
 +
        cn.push_back(colorNames.substr(start, colorNames.size() - start));
 +
        }
 +
      return cn;
 +
    }
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::MakeTableHeader()
+
    std::vector<std::vector<vtkStdString> > ParseSynonyms(
{
+
                                                      const vtkStdString & synonyms)
  std::string s = "<tr>\n";
+
    {
  s += "<th style=\"background:lightgrey\">HTML name</th>\n";
+
      // The delimiter for a string of synonyms.
  s += "<th style=\"background:lightgrey\">Decimal code<br />\n";
+
      const vtkStdString synonymDelimiter = "\n\n";
  s += "&#160;&#160;R&#160;&#160;&#160;&#160;&#160;&#160;";
+
      size_t start = 0;
  s += "G&#160;&#160;&#160;&#160;&#160;&#160;B</th>\n";
+
      size_t end = synonyms.find(synonymDelimiter);
  s += "</tr>\n";
+
      std::vector<vtkStdString> cn;
  return s;
+
      std::vector<std::vector<vtkStdString> > syn;
}
+
      vtkStdString str;
 +
      while (end != std::string::npos)
 +
        {
 +
        str = synonyms.substr(start, end - start);
 +
        cn = ParseColorNames(str);
 +
        syn.push_back(cn);
 +
        start = end + 2;
 +
        end = synonyms.find(synonymDelimiter, start);
 +
        }
 +
      // Get the last set of synonyms.
 +
      if (!synonyms.empty())
 +
        {
 +
        str = synonyms.substr(start, end - start);
 +
        cn = ParseColorNames(str);
 +
        syn.push_back(cn);
 +
        }
 +
      return syn;
 +
    }
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::MakeSynonymTableHeader()
+
    std::string MakeHTMLHeader()
{
+
    {
  std::string s = "<tr>\n";
+
      std::string s = "<!DOCTYPE html>\n";
  s += "<th style=\"background:lightgrey\">Synonyms</th>\n";
+
      s += "<html lang=\"en\" dir=\"ltr\" class=\"client-nojs\">\n";
  s += "<th style=\"background:lightgrey\">Decimal code<br />\n";
+
      s += "<head>\n";
  s += "&#160;&#160;R&#160;&#160;&#160;&#160;&#160;&#160;";
+
      s += "<title>vtkNamedColors</title>\n";
  s += "G&#160;&#160;&#160;&#160;&#160;&#160;B</th>\n";
+
      s += "<meta charset=\"UTF-8\" />\n";
  s += "</tr>\n";
+
      s += "</head>\n";
  return s;
+
      return s;
}
+
    }
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::MakeTD(std::string const & name)
+
    std::string MakeHTMLIndex()
{
+
    {
  std::string s = "<tr>\n";
+
      std::string s = "<h2>Index</h2>\n";
  s += "<td colspan=\"2\" style=\"background:whitesmoke;";
+
      s += "<ul>\n";
  s += "color:slategray;text-align:left\"><big><b>" + name + "</b></big></td>\n";
+
      s += "\t<li><a href=\"#WebColorNames\">Web color Names</li>\n";
  s += "</tr>\n";
+
      s += "\t<li><a href=\"#VTKColorNames\">VTK color Names</li>\n";
  return s;
+
      s += "\t<li><a href=\"#Synonyms\">Synonyms</li>\n";
}
+
      s += "</ul>\n";
 +
      return s;
 +
    }
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::FormatRGBForHTML(vtkColor3ub const & rgb)
+
    std::string MakeTableHeader()
{
+
    {
  std::string s;
+
      std::string s = "<tr>\n";
  char buff[9];
+
      s += "<th style=\"background:lightgrey\">HTML name</th>\n";
  snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetRed());
+
      s += "<th style=\"background:lightgrey\">Decimal code<br />\n";
  s += buff;
+
      s += "&#160;&#160;R&#160;&#160;&#160;&#160;&#160;&#160;";
  s += +",";
+
      s += "G&#160;&#160;&#160;&#160;&#160;&#160;B</th>\n";
  snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetGreen());
+
      s += "</tr>\n";
  s += buff;
+
      return s;
  s += +",";
+
    }
  snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetBlue());
 
  s += buff;
 
  s = stlHelpers.ReplaceAll(s, " ", "&#160;");
 
  s = stlHelpers.ReplaceAll(s, ",", "&#160;&#160;");
 
  return s;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
/*!
+
    std::string MakeSynonymTableHeader()
  Use when the name is a color name known to the web browser.
+
    {
*/
+
      std::string s = "<tr>\n";
std::string NamedColorPatches::MakeTR(const std::string & name,
+
      s += "<th style=\"background:lightgrey\">Synonyms</th>\n";
                        vtkColor3ub const & rgb, const std::string & textColor)
+
      s += "<th style=\"background:lightgrey\">Decimal code<br />\n";
{
+
      s += "&#160;&#160;R&#160;&#160;&#160;&#160;&#160;&#160;";
  std::string s = "<tr>\n";
+
      s += "G&#160;&#160;&#160;&#160;&#160;&#160;B</th>\n";
  s += "<td style=\"background:" + name + ";color:" + textColor;
+
      s += "</tr>\n";
  s += "\">" + name + "</td>\n";
+
      return s;
  s += "<td style=\"background:" + name + ";color:" + textColor;
+
    }
  s += "\"><tt>" + this->FormatRGBForHTML(rgb) + "</tt></td>\n";
 
  s += "</tr>\n";
 
  return s;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
/*!
+
    std::string MakeTD(std::string const & name)
  Use when the name is not a color name known to the web browser.
+
    {
*/
+
      std::string s = "<tr>\n";
std::string NamedColorPatches::MakeSynonymTR(const std::string & name,
+
      s += "<td colspan=\"2\" style=\"background:whitesmoke;";
                        const std::string & nameColor, vtkColor3ub const & rgb,
+
      s += "color:slategray;text-align:left\"><big><b>" + name + "</b></big></td>\n";
                        const std::string & textColor)
+
      s += "</tr>\n";
{
+
      return s;
  std::string s = "<tr>\n";
+
    }
  s += "<td style=\"background:" + nameColor + ";color:" + textColor;
 
  s += "\">" + name + "</td>\n";
 
  s += "<td style=\"background:" + nameColor + ";color:" + textColor;
 
  s += "\"><tt>" + this->FormatRGBForHTML(rgb) + "</tt></td>\n";
 
  s += "</tr>\n";
 
  return s;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::MakeWebColorTables()
+
    std::string FormatRGBForHTML(vtkColor3ub const & rgb)
{
 
  std::string res;
 
  for (std::vector<std::string>::const_iterator
 
    p = cs.cnOrder.begin(); p != cs.cnOrder.end(); ++p)
 
 
     {
 
     {
    if (std::find(
+
      std::string s;
       cs.cnStartTable.begin(), cs.cnStartTable.end(), *p)
+
      char buff[9];
      != cs.cnStartTable.end())
+
       snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetRed());
       {
+
       s += buff;
      res += "<td>\n";
+
       s += +",";
       res += "<table>\n";
+
       snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetGreen());
       res += this->MakeTableHeader();
+
       s += buff;
      }
+
       s += +",";
    // Add in the name of the group in the color table.
+
       snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetBlue());
    res += this->MakeTD(*p + " colors");
+
       s += buff;
    std::vector<std::string> values = cs.cn[*p];
+
       s = stlHelpers.ReplaceAll(s, " ", "&#160;");
    for (std::vector<std::string>::const_iterator
+
       s = stlHelpers.ReplaceAll(s, ",", "&#160;&#160;");
      q = values.begin(); q != values.end(); ++q)
+
       return s;
       {
+
    }
      vtkColor3ub rgb = this->nc->GetColor3ub(*q);
 
       double y = htmlRGBA.RGBToLumaCCIR601(rgb);
 
      std::string textColor = "#000000"; // Black
 
       if (y < 255 / 2.0)
 
        {
 
        textColor = "#ffffff"; // White
 
        }
 
      // Make the row for each color in the group.
 
      // Here we use the name to set the background color
 
      //  as it is known to the web browser.
 
       res += this->MakeTR(*q, rgb, textColor);
 
       }
 
    if (std::find(
 
      cs.cnEndTable.begin(), cs.cnEndTable.end(), *p)
 
       != cs.cnEndTable.end())
 
      {
 
      res += "</table>\n";
 
      res += "</td>\n";
 
       }
 
  }
 
  return res;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string  NamedColorPatches::MakeVTKColorTables()
+
    /*!
{
+
      Use when the name is a color name known to the web browser.
  std::string res;
+
    */
 +
    std::string  MakeTR(const std::string & name,
 +
                            vtkColor3ub const & rgb, const std::string & textColor)
 +
    {
 +
      std::string s = "<tr>\n";
 +
      s += "<td style=\"background:" + name + ";color:" + textColor;
 +
      s += "\">" + name + "</td>\n";
 +
      s += "<td style=\"background:" + name + ";color:" + textColor;
 +
      s += "\"><tt>" + this->FormatRGBForHTML(rgb) + "</tt></td>\n";
 +
      s += "</tr>\n";
 +
      return s;
 +
    }
  
  for (std::vector<std::string>::const_iterator
+
    //-----------------------------------------------------------------------------
    p = cs.vtkcnOrder.begin(); p != cs.vtkcnOrder.end(); ++p)
+
    /*!
 +
      Use when the name is not a color name known to the web browser.
 +
    */
 +
    std::string  MakeSynonymTR(const std::string & name,
 +
                            const std::string & nameColor, vtkColor3ub const & rgb,
 +
                            const std::string & textColor)
 
     {
 
     {
    if (std::find(
+
      std::string s = "<tr>\n";
      cs.vtkcnStartTable.begin(), cs.vtkcnStartTable.end(), *p)
+
       s += "<td style=\"background:" + nameColor + ";color:" + textColor;
      != cs.vtkcnStartTable.end())
+
       s += "\">" + name + "</td>\n";
      {
+
       s += "<td style=\"background:" + nameColor + ";color:" + textColor;
      res += "<td>\n";
+
       s += "\"><tt>" + this->FormatRGBForHTML(rgb) + "</tt></td>\n";
       res += "<table>\n";
+
       s += "</tr>\n";
       res += this->MakeTableHeader();
+
       return s;
      }
 
    // Add in the name of the group in the color table.
 
    res += this->MakeTD(*p);
 
    std::vector<std::string> values = cs.vtkcn[*p];
 
    for (std::vector<std::string>::const_iterator
 
       q = values.begin(); q != values.end(); ++q)
 
       {
 
      vtkColor3ub rgb = this->nc->GetColor3ub(*q);
 
      double y = htmlRGBA.RGBToLumaCCIR601(rgb);
 
      std::string textColor = "#000000"; // Black
 
      if (y < 255 / 2.0)
 
        {
 
        textColor = "#ffffff"; // White
 
        }
 
       // We must set the background color to a specific
 
      // HTML color as the color name may not be a standard
 
      // name known to the web browser.
 
      std::string nameColor = htmlRGBA.RGBToHTMLColor(rgb);
 
       // Make the row for each color in the group.
 
      res += this->MakeSynonymTR(*q, nameColor, rgb, textColor);
 
 
     }
 
     }
    if (std::find(
 
      cs.vtkcnEndTable.begin(), cs.vtkcnEndTable.end(), *p)
 
      != cs.vtkcnEndTable.end())
 
      {
 
      res += "</table>\n";
 
      res += "</td>\n";
 
      }
 
  }
 
  return res;
 
}
 
  
//-----------------------------------------------------------------------------
+
    //-----------------------------------------------------------------------------
std::string NamedColorPatches::MakeSynonymColorTable()
+
     std::string MakeWebColorTables()
{
 
  std::vector<std::vector<vtkStdString > > synonyms
 
     = this->ParseSynonyms(this->nc->GetSynonyms());
 
  std::vector<std::string> cn;
 
  for (std::map<std::string, std::vector<std::string> >::const_iterator
 
    p = cs.cn.begin(); p != cs.cn.end(); ++p)
 
 
     {
 
     {
    for (std::vector<std::string>::const_iterator
+
      std::string res;
      q = p->second.begin(); q != p->second.end(); ++q)
+
      for (std::vector<std::string>::const_iterator
      {
+
        p = cs.cnOrder.begin(); p != cs.cnOrder.end(); ++p)
      cn.push_back(*q);
+
        {
 +
        if (std::find(
 +
          cs.cnStartTable.begin(),  cs.cnStartTable.end(), *p)
 +
          != cs.cnStartTable.end())
 +
          {
 +
          res += "<td>\n";
 +
          res += "<table>\n";
 +
          res += this->MakeTableHeader();
 +
          }
 +
        // Add in the name of the group in the color table.
 +
        res += this->MakeTD(*p + " colors");
 +
        std::vector<std::string> values = cs.cn[*p];
 +
        for (std::vector<std::string>::const_iterator
 +
          q = values.begin(); q != values.end(); ++q)
 +
          {
 +
          vtkColor3ub rgb = this->nc->GetColor3ub(*q);
 +
          double y = htmlRGBA.RGBToLumaCCIR601(rgb);
 +
          std::string textColor = "#000000"; // Black
 +
          if (y < 255 / 2.0)
 +
            {
 +
            textColor = "#ffffff"; // White
 +
            }
 +
          // Make the row for each color in the group.
 +
          // Here we use the name to set the background color
 +
          //  as it is known to the web browser.
 +
          res += this->MakeTR(*q, rgb, textColor);
 +
          }
 +
        if (std::find(
 +
          cs.cnEndTable.begin(), cs.cnEndTable.end(), *p)
 +
          != cs.cnEndTable.end())
 +
          {
 +
          res += "</table>\n";
 +
          res += "</td>\n";
 +
          }
 
       }
 
       }
 +
      return res;
 
     }
 
     }
  std::map<std::string, std::string> d;
+
 
  for (std::vector<std::string>::const_iterator
+
    //-----------------------------------------------------------------------------
    p = cn.begin(); p != cn.end(); ++p)
+
    std::string MakeVTKColorTables()
 
     {
 
     {
    d[stlHelpers.ToLowercase(*p)] = *p;
+
      std::string res;
 +
 
 +
      for (std::vector<std::string>::const_iterator
 +
        p = cs.vtkcnOrder.begin(); p != cs.vtkcnOrder.end(); ++p)
 +
        {
 +
        if (std::find(
 +
          cs.vtkcnStartTable.begin(), cs.vtkcnStartTable.end(), *p)
 +
          != cs.vtkcnStartTable.end())
 +
          {
 +
          res += "<td>\n";
 +
          res += "<table>\n";
 +
          res += this->MakeTableHeader();
 +
          }
 +
        // Add in the name of the group in the color table.
 +
        res += this->MakeTD(*p);
 +
        std::vector<std::string> values = cs.vtkcn[*p];
 +
        for (std::vector<std::string>::const_iterator
 +
          q = values.begin(); q != values.end(); ++q)
 +
          {
 +
          vtkColor3ub rgb = this->nc->GetColor3ub(*q);
 +
          double y = htmlRGBA.RGBToLumaCCIR601(rgb);
 +
          std::string textColor = "#000000"; // Black
 +
          if (y < 255 / 2.0)
 +
            {
 +
            textColor = "#ffffff"; // White
 +
            }
 +
          // We must set the background color to a specific
 +
          // HTML color as the color name may not be a standard
 +
          // name known to the web browser.
 +
          std::string nameColor = htmlRGBA.RGBToHTMLColor(rgb);
 +
          // Make the row for each color in the group.
 +
          res += this->MakeSynonymTR(*q, nameColor, rgb, textColor);
 +
        }
 +
        if (std::find(
 +
          cs.vtkcnEndTable.begin(), cs.vtkcnEndTable.end(), *p)
 +
          != cs.vtkcnEndTable.end())
 +
          {
 +
          res += "</table>\n";
 +
          res += "</td>\n";
 +
          }
 +
      }
 +
      return res;
 
     }
 
     }
  std::string res;
+
 
  res += "<td>\n";
+
    //-----------------------------------------------------------------------------
  res += "<table>\n";
+
    std::string MakeSynonymColorTable()
  res += this->MakeSynonymTableHeader();
 
  for (std::vector<std::vector<vtkStdString > >::iterator
 
    p = synonyms.begin(); p != synonyms.end(); ++p)
 
 
     {
 
     {
    int idx = 0;
+
      std::vector<std::vector<vtkStdString > > synonyms
    for (std::vector<vtkStdString >::iterator
+
        = this->ParseSynonyms(this->nc->GetSynonyms());
      q = p->begin(); q != p->end(); ++q)
+
       std::vector<std::string> cn;
       {
+
       for (std::map<std::string, std::vector<std::string> >::const_iterator
       // Replace the lowercase name with the proper name.
+
        p = cs.cn.begin(); p != cs.cn.end(); ++p)
      if (d.find(*q) != d.end())
 
 
         {
 
         {
         *q = d[*q];
+
         for (std::vector<std::string>::const_iterator
 +
          q = p->second.begin(); q != p->second.end(); ++q)
 +
          {
 +
          cn.push_back(*q);
 +
          }
 
         }
 
         }
       }
+
       std::map<std::string, std::string> d;
    std::sort(p->begin(), p->end());
+
      for (std::vector<std::string>::const_iterator
    std::string names;
+
        p = cn.begin(); p != cn.end(); ++p)
    for (std::vector<vtkStdString >::iterator
 
      q = p->begin(); q != p->end(); ++q)
 
      {
 
      names += *q;
 
      if (!stlHelpers.IsLast(q, *p))
 
 
         {
 
         {
         names += ", ";
+
         d[stlHelpers.ToLowercase(*p)] = *p;
 
         }
 
         }
 +
      std::string res;
 +
      res += "<td>\n";
 +
      res += "<table>\n";
 +
      res += this->MakeSynonymTableHeader();
 +
      for (std::vector<std::vector<vtkStdString > >::iterator
 +
        p = synonyms.begin(); p != synonyms.end(); ++p)
 +
        {
 +
        int idx = 0;
 +
        for (std::vector<vtkStdString >::iterator
 +
          q = p->begin(); q != p->end(); ++q)
 +
          {
 +
          // Replace the lowercase name with the proper name.
 +
          if (d.find(*q) != d.end())
 +
            {
 +
            *q = d[*q];
 +
            }
 +
          }
 +
        std::sort(p->begin(), p->end());
 +
        std::string names;
 +
        for (std::vector<vtkStdString >::iterator
 +
          q = p->begin(); q != p->end(); ++q)
 +
          {
 +
          names += *q;
 +
          if (!stlHelpers.IsLast(q, *p))
 +
            {
 +
            names += ", ";
 +
            }
 +
          }
 +
        vtkColor3ub rgb = this->nc->GetColor3ub(*(p->begin()));
 +
        double y = htmlRGBA.RGBToLumaCCIR601(rgb);
 +
        std::string textColor = "#000000"; // Black
 +
        if (y < 255 / 2.0)
 +
          {
 +
          textColor = "#ffffff"; // White
 +
          }
 +
        // We must set the background color to a specific
 +
        // HTML color because names is just a list of
 +
        // synonyms for that particular color.
 +
        std::string nameColor = htmlRGBA.RGBToHTMLColor(rgb);
 +
        // Make the row for each color in the group.
 +
        res += this->MakeSynonymTR(names, nameColor, rgb, textColor);
 
       }
 
       }
    vtkColor3ub rgb = this->nc->GetColor3ub(*(p->begin()));
+
       res += "</table>\n";
    double y = htmlRGBA.RGBToLumaCCIR601(rgb);
+
      res += "</td>\n";
    std::string textColor = "#000000"; // Black
+
      return res;
    if (y < 255 / 2.0)
+
    }
      {
+
 
      textColor = "#ffffff"; // White
+
  private:
       }
+
    vtkSmartPointer<vtkNamedColors> nc;
    // We must set the background color to a specific
+
};
    // HTML color because names is just a list of
 
    // synonyms for that particular color.
 
    std::string nameColor = htmlRGBA.RGBToHTMLColor(rgb);
 
    // Make the row for each color in the group.
 
    res += this->MakeSynonymTR(names, nameColor, rgb, textColor);
 
  }
 
  res += "</table>\n";
 
  res += "</td>\n";
 
  return res;
 
}
 
  
//-----------------------------------------------------------------------------
+
int main()
std::string NamedColorPatches::MakeWebColorPage()
 
 
{
 
{
   std::string res = this->MakeHTMLHeader();
+
   NamedColorPatches ncp;
  res += "<body>\n";
 
  res += "<h1>Colors available in vtkNamedColors</h1>\n";
 
  res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 
  res += "<caption style=\"background:lightgrey\">Web Color Names</caption>\n";
 
  res += "<tr valign=\"top\">\n";
 
  res += this->MakeWebColorTables();
 
  res += "</table>\n";
 
  res += "</body>\n";
 
  return res;
 
}
 
  
//-----------------------------------------------------------------------------
+
   std::ofstream outputFile;
std::string NamedColorPatches::MakeVTKColorPage()
+
   outputFile.open("VTKNamedColorPatches.html",
{
+
                    ios::out | ios::trunc | ios::binary);
   std::string res = this->MakeHTMLHeader();
+
   outputFile << ncp.MakeCombinedColorPage();
   res += "<body>\n";
+
   outputFile.close();
  res += "<h1>Colors available in vtkNamedColors</h1>\n";
 
  res += "<p>The web colors take precedence over colors of the same";
 
  res += " name in VTK Color Names.</p>\n";
 
  res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 
   res += "<caption style=\"background:lightgrey\">VTK Color Names</caption>\n";
 
   res += "<tr valign=\"top\">\n";
 
  res += this->MakeVTKColorTables();
 
  res += "</table>\n";
 
  res += "</body>\n";
 
  return res;
 
}
 
  
//-----------------------------------------------------------------------------
+
   return EXIT_SUCCESS;
std::string NamedColorPatches::MakeSynonymColorPage()
 
{
 
  std::string res = this->MakeHTMLHeader();
 
  res += "<body>\n";
 
  res += "<h1>Synonyms in vtkNamedColors</h1>\n";
 
  res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 
  res += "<caption style=\"background:lightgrey\"></caption>\n";
 
  res += "<tr valign=\"top\">\n";
 
  res += this->MakeSynonymColorTable();
 
  res += "</table>\n";
 
  res += "</body>\n";
 
   return res;
 
 
}
 
}
  
//-----------------------------------------------------------------------------
 
std::string NamedColorPatches::MakeCombinedColorPage()
 
{
 
  std::string res = this->MakeHTMLHeader();
 
  res += "<body>\n";
 
  res += "<h1>Colors available in vtkNamedColors</h1>\n";
 
  res += "<p>The class vtkNamedColors provides color names and their";
 
  res += " values for the convenience of the user.</p>\n";
 
  res += "<p>The following tables show the available colors along with";
 
  res += " their red, green and blue values.</p>\n";
 
  res += this->MakeHTMLIndex();
 
  res += "<h2><a id=\"WebColorNames\">Web color Names</h2>";
 
  res += "These colors correspond to those in";
 
  res += " <a href=\"http://en.wikipedia.org/wiki/Web_colors\"";
 
  res += " title=\"Web Colors\">Web Colors</a>.\n";
 
  res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 
  res += "<tr valign=\"top\">\n";
 
  res += this->MakeWebColorTables();
 
  res += "</table>\n";
 
  res += "<h2><a id=\"VTKColorNames\">VTK color Names</h2>";
 
  res += "<p>The colors mainly correspond to those in vtkColors.txt.\n</p>\n";
 
  res += "<p>The web colors (above) take precedence over colors of the";
 
  res += " same name in vtkColors.txt.</p>\n";
 
  res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 
  res += "<tr valign=\"top\">\n";
 
  res += this->MakeVTKColorTables();
 
  res += "</table>\n";
 
  res += "<h2><a id=\"Synonyms\">Synonyms</h2>";
 
  res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
 
  res += "<tr valign=\"top\">\n";
 
  res += this->MakeSynonymColorTable();
 
  res += "</table>\n";
 
  res += "</body>\n";
 
  return res;
 
}
 
 
</source>
 
</source>
  
==CMakeLists.txt==
 
<source lang="cmake">
 
cmake_minimum_required(VERSION 2.8.7 FATAL_ERROR)
 
  
PROJECT(NamedColorPatches)
+
{{VTKCMakeLists|{{SUBPAGENAME}}}}
 
 
find_package(VTK REQUIRED)
 
if(${VTK_VERSION} VERSION_LESS "6")
 
  message(FATAL_ERROR "You need VTK 6.0 or greater.")
 
endif()
 
 
 
find_package(VTK COMPONENTS
 
  vtkCommonColor
 
  vtkCommonCore
 
)
 
 
 
include(${VTK_USE_FILE})
 
 
 
set ( SRCS
 
    NamedColorPatches.cxx
 
    NamedColorPatchesDriver.cxx
 
)
 
 
 
set ( INCS
 
    NamedColorPatches.h
 
)
 
 
 
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${SRCS} ${INCS})
 
 
 
target_link_libraries(${PROJECT_NAME} ${VTK_LIBRARIES})
 
</source>
 

Revision as of 18:00, 3 July 2014

Description

This example shows how to produce a HTML page called VTKNamedColorPatches.html showing the available colors in vtkNamedColors.

It also shows how to select the text color based on luminance. In this case Digital CCIR601 is used which gives less weight to the red and blue components of a color.

This program produces identical output to NamedColorPatches.py.

The logic is largely similar to that in NamedColorPatches.py, however the Python dictionaries and lists defining the various color names by type and the starting and ending conditions are stored as strings and then parsed into the corresponding C++ maps and vectors. This is done in one of the helper classes. Other helper classes extend the STL and provide conversions between RGB colors and HTML color strings.

Three helper classes are also used, these are defined and instantiated in an anonymous namespace in NamedColorPatches.cxx.

NamedColorPatches.cxx

#include <vtkNamedColors.h>
#include <vtkSmartPointer.h>

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <iterator>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cctype>


#ifdef WIN32
#define snprintf _snprintf_s
#endif

namespace {
  //! Some STL Utilities.
  class STLHelpers
  {
  public:
    //---------------------------------------------------------------------------
    STLHelpers()
    {
    }

    //---------------------------------------------------------------------------
    virtual ~STLHelpers()
    {
    }

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

    //-----------------------------------------------------------------------------
    // Replace all occurrences of old_value in a string with new_value.
    std::string ReplaceAll(std::string & str,
      const std::string & old_value, const std::string & new_value)
    {
      size_t start_pos = 0;
      while ((start_pos = str.find(old_value, start_pos)) != std::string::npos)
      {
        str.replace(start_pos, old_value.length(), new_value);
        // It could be that 'new_value' is a substring of 'old_value'.
        start_pos += new_value.length();
      }
      return str;
    }

    //-----------------------------------------------------------------------------
    // An implementation of the C++11 next(iter,n) found in the header <iterator>.
    // ForwardIt must meet the requirements of ForwardIterator.
    // Return the nth successor of iterator it.
    template <typename ForwardIt>
    ForwardIt Next(ForwardIt iter,
      typename std::iterator_traits<ForwardIt>::difference_type n = 1)
    {
      std::advance(iter, n);
      return iter;
    }

    //-----------------------------------------------------------------------------
    // Return true if the iterator points to the last element.
    template <typename Iter, typename Cont>
    bool IsLast(Iter iter, const Cont & cont)
    {
      return (iter != cont.end()) && (Next(iter) == cont.end());
    }

  };

  // Convert to and from HTML color strings.
  class HTMLToFromRGBAColor
  {
  public:
    //---------------------------------------------------------------------------
    HTMLToFromRGBAColor()
    {
    }

    //---------------------------------------------------------------------------
    virtual ~HTMLToFromRGBAColor()
    {
    }

    //-----------------------------------------------------------------------------
    bool IsValidHTMLColorString(std::string const & s)
    {
      if (s.size() == 7 || s.size() == 9) // #rrggbb or #rrggbbaa
      {
        if (s.compare(0, 1, "#") == 0
          && s.find_first_not_of("0123456789abcdefABCDEF", 1) == std::string::npos)
        {
          return true;
        }
      }
      return false;
    }

    //-----------------------------------------------------------------------------
    std::string RGBToHTMLColor(vtkColor3ub const & rgb)
    {
      std::string s = "#";
      char buff[9];
      snprintf(buff, sizeof(buff) - 1, "%02x", rgb.GetRed());
      s += buff;
      snprintf(buff, sizeof(buff) - 1, "%02x", rgb.GetGreen());
      s += buff;
      snprintf(buff, sizeof(buff) - 1, "%02x", rgb.GetBlue());
      s += buff;
      return s;
    }

    //-----------------------------------------------------------------------------
    vtkColor3ub HTMLColorToRGB(std::string const & colorString)
    {
      vtkColor3ub c(0, 0, 0);
      if (IsValidHTMLColorString(colorString))
      {
        std::string t = colorString.substr(1);
        std::string v = t.substr(0, 2);
        c.SetRed(static_cast<unsigned char>(
          strtoul(t.substr(0, 2).c_str(), NULL, 16)));
        c.SetGreen(static_cast<unsigned char>(strtoul(
          t.substr(2, 2).c_str(), NULL, 16)));
        c.SetBlue(static_cast<unsigned char>(strtoul(
          t.substr(4, 2).c_str(), NULL, 16)));
      }
      return c;
    }

    //-----------------------------------------------------------------------------
    double RGBToLumaCCIR601(vtkColor3ub const & rgb)
    {
      return 0.299 * rgb.GetRed() + 0.587 * rgb.GetGreen() + 0.114 * rgb.GetBlue();
    }

  };

  //! Holds the color names and information about table layout and ordering.
  /*!
    This class creates a series of maps and vectors of color names from
    a series of python dictionaries and lists.
    */
  class ColorStructures
  {
  public:
    //---------------------------------------------------------------------------
    ColorStructures()
    {
      this->Init();
    }

    //---------------------------------------------------------------------------
    virtual ~ColorStructures()
    {
    }

  private:
    //---------------------------------------------------------------------------
    void Init()
    {
      // These strings correspond to the maps and lists used in the Python
      // version of this code (NamedColorPatches.py).
      const std::string cnStr = "\
'Red':['IndianRed', 'LightCoral', 'Salmon', 'DarkSalmon',\
'LightSalmon', 'Red', 'Crimson', 'FireBrick', 'DarkRed'],\
'Pink':['Pink', 'LightPink', 'HotPink', 'DeepPink',\
'MediumVioletRed', 'PaleVioletRed'],\
'Orange':['LightSalmon', 'Coral', 'Tomato', 'OrangeRed',\
'DarkOrange', 'Orange'],\
'Yellow':['Gold', 'Yellow', 'LightYellow', 'LemonChiffon',\
'LightGoldenrodYellow', 'PapayaWhip', 'Moccasin',\
'PeachPuff', 'PaleGoldenrod', 'Khaki', 'DarkKhaki'],\
'Purple':['Lavender', 'Thistle', 'Plum', 'Violet', 'Orchid',\
'Fuchsia', 'Magenta', 'MediumOrchid', 'MediumPurple',\
'BlueViolet', 'DarkViolet', 'DarkOrchid', 'DarkMagenta',\
'Purple', 'Indigo', 'DarkSlateBlue', 'SlateBlue',\
'MediumSlateBlue'],\
'Green':['GreenYellow', 'Chartreuse', 'LawnGreen', 'Lime',\
'LimeGreen', 'PaleGreen', 'LightGreen',\
'MediumSpringGreen', 'SpringGreen', 'MediumSeaGreen',\
'SeaGreen', 'ForestGreen', 'Green', 'DarkGreen',\
'YellowGreen', 'OliveDrab', 'Olive', 'DarkOliveGreen',\
'MediumAquamarine', 'DarkSeaGreen', 'LightSeaGreen',\
'DarkCyan', 'Teal'],\
'Blue/Cyan':['Aqua', 'Cyan', 'LightCyan', 'PaleTurquoise',\
'Aquamarine', 'Turquoise', 'MediumTurquoise',\
'DarkTurquoise', 'CadetBlue', 'SteelBlue',\
'LightSteelBlue', 'PowderBlue', 'LightBlue',\
'SkyBlue', 'LightSkyBlue', 'DeepSkyBlue',\
'DodgerBlue', 'CornflowerBlue', 'RoyalBlue', 'Blue',\
'MediumBlue', 'DarkBlue', 'Navy', 'MidnightBlue'],\
'Brown':['Cornsilk', 'BlanchedAlmond', 'Bisque', 'NavajoWhite',\
'Wheat', 'BurlyWood', 'Tan', 'RosyBrown', 'SandyBrown',\
'Goldenrod', 'DarkGoldenrod', 'Peru', 'Chocolate',\
'SaddleBrown', 'Sienna', 'Brown', 'Maroon'],\
'White':['White', 'Snow', 'Honeydew', 'MintCream', 'Azure',\
'AliceBlue', 'GhostWhite', 'WhiteSmoke', 'Seashell',\
'Beige', 'OldLace', 'FloralWhite', 'Ivory',\
'AntiqueWhite', 'Linen',\
'LavenderBlush', 'MistyRose'],\
'Gray':['Gainsboro', 'LightGrey', 'Silver', 'DarkGray', 'Gray',\
'DimGray', 'LightSlateGray', 'SlateGray', 'DarkSlateGray',\
'Black']\
";
const std::string cnOrderStr =
"['Red', 'Pink', 'Orange', 'Yellow', 'Purple', 'Green',\
'Blue/Cyan', 'Brown', 'White', 'Gray']";
const std::string cnStartTableStr =
"['Red', 'Green', 'Brown']";
const std::string cnEndTableStr =
"['Purple', 'Blue/Cyan', 'Gray']";

const std::string vtkcnStr = "\
'Whites':['antique_white', 'azure', 'bisque', 'blanched_almond',\
'cornsilk', 'eggshell', 'floral_white', 'gainsboro',\
'ghost_white', 'honeydew', 'ivory', 'lavender',\
'lavender_blush', 'lemon_chiffon', 'linen', 'mint_cream',\
'misty_rose', 'moccasin', 'navajo_white', 'old_lace',\
'papaya_whip',  'peach_puff', 'seashell', 'snow',\
'thistle', 'titanium_white', 'wheat', 'white',\
'white_smoke', 'zinc_white'],\
'Greys':['cold_grey', 'dim_grey', 'grey', 'light_grey',\
'slate_grey', 'slate_grey_dark', 'slate_grey_light',\
'warm_grey'],\
'Blacks':['black', 'ivory_black', 'lamp_black'],\
'Reds':['alizarin_crimson', 'brick', 'cadmium_red_deep', 'coral',\
'coral_light', 'deep_pink', 'english_red', 'firebrick',\
'geranium_lake', 'hot_pink', 'indian_red', 'light_salmon',\
'madder_lake_deep', 'maroon', 'pink', 'pink_light',\
'raspberry', 'red', 'rose_madder', 'salmon', 'tomato',\
'venetian_red'],\
'Browns':['beige', 'brown', 'brown_madder', 'brown_ochre',\
'burlywood', 'burnt_sienna', 'burnt_umber', 'chocolate',\
'deep_ochre', 'flesh', 'flesh_ochre', 'gold_ochre',\
'greenish_umber', 'khaki', 'khaki_dark', 'light_beige',\
'peru', 'rosy_brown', 'raw_sienna', 'raw_umber', 'sepia',\
'sienna', 'saddle_brown', 'sandy_brown', 'tan',\
'van_dyke_brown'],\
'Oranges':['cadmium_orange', 'cadmium_red_light', 'carrot',\
'dark_orange', 'mars_orange', 'mars_yellow', 'orange',\
'orange_red', 'yellow_ochre'],\
'Yellows':['aureoline_yellow', 'banana', 'cadmium_lemon',\
'cadmium_yellow', 'cadmium_yellow_light', 'gold',\
'goldenrod', 'goldenrod_dark', 'goldenrod_light',\
'goldenrod_pale', 'light_goldenrod', 'melon',\
'naples_yellow_deep', 'yellow', 'yellow_light'],\
'Greens':['chartreuse', 'chrome_oxide_green', 'cinnabar_green',\
'cobalt_green', 'emerald_green', 'forest_green', 'green',\
'green_dark', 'green_pale', 'green_yellow', 'lawn_green',\
'lime_green', 'mint', 'olive', 'olive_drab',\
'olive_green_dark', 'permanent_green', 'sap_green',\
'sea_green', 'sea_green_dark', 'sea_green_medium',\
'sea_green_light', 'spring_green', 'spring_green_medium',\
'terre_verte', 'viridian_light', 'yellow_green'],\
'Cyans':['aquamarine', 'aquamarine_medium', 'cyan', 'cyan_white',\
'turquoise', 'turquoise_dark', 'turquoise_medium',\
'turquoise_pale'],\
'Blues':['alice_blue', 'blue', 'blue_light', 'blue_medium',\
'cadet', 'cobalt', 'cornflower', 'cerulean', 'dodger_blue',\
'indigo', 'manganese_blue', 'midnight_blue', 'navy',\
'peacock',  'powder_blue', 'royal_blue', 'slate_blue',\
'slate_blue_dark', 'slate_blue_light',\
'slate_blue_medium', 'sky_blue', 'sky_blue_deep',\
'sky_blue_light', 'steel_blue', 'steel_blue_light',\
'turquoise_blue', 'ultramarine'],\
'Magentas':['blue_violet', 'cobalt_violet_deep', 'magenta',\
'orchid', 'orchid_dark', 'orchid_medium',\
'permanent_red_violet', 'plum', 'purple',\
'purple_medium', 'ultramarine_violet', 'violet',\
'violet_dark', 'violet_red', 'violet_red_medium',\
'violet_red_pale']\
";
const std::string vtkcnOrderStr =
"['Whites', 'Greys', 'Blacks', 'Reds', 'Oranges',\
'Browns', 'Yellows', 'Greens', 'Cyans', 'Blues',\
'Magentas']";
const std::string vtkcnStartTableStr =
"['Whites', 'Browns', 'Cyans']";
const std::string vtkcnEndTableStr =
"['Oranges', 'Greens', 'Magentas']";

      this->cn = this->ParseDictionary(cnStr);
      this->cnOrder = this->ParseList(cnOrderStr);
      this->cnStartTable = this->ParseList(cnStartTableStr);
      this->cnEndTable = this->ParseList(cnEndTableStr);

      this->vtkcn = this->ParseDictionary(vtkcnStr);
      this->vtkcnOrder = this->ParseList(vtkcnOrderStr);
      this->vtkcnStartTable = this->ParseList(vtkcnStartTableStr);
      this->vtkcnEndTable = this->ParseList(vtkcnEndTableStr);
    }

  private:
    //---------------------------------------------------------------------------
    //! Words are delimited by single quotes.
    std::string ParseWords(std::string const & str)
    {
      size_t idx1 = str.find_first_of("'");
      size_t idx2 = str.find_last_of("'");
      if (idx1 != std::string::npos && idx2 != std::string::npos)
      {
        return str.substr(idx1 + 1, idx2 - idx1 - 1);
      }
      return "";
    }

    //---------------------------------------------------------------------------
    //! Convert a list to a vector.
    std::vector<std::string> ParseList(std::string const & str)
    {
      std::vector<std::string> result;
      size_t idx1 = 0;
      size_t idx2 = 0;
      size_t idx3 = str.size();
      while (idx1 != std::string::npos && idx2 !=
        std::string::npos && idx1 < str.size())
      {
        idx1 = str.find("'", idx1);
        idx2 = str.find("'", idx1 + 1);
        if (idx1 != std::string::npos)
        {
          if (idx2 != std::string::npos)
          {
            result.push_back(str.substr(idx1 + 1, idx2 - idx1 - 1));
          }
          else
          {
            break;
          }
        }
        else
        {
          break;
        }
        idx1 = idx2 + 1;
      }
      return result;
    }

    //---------------------------------------------------------------------------
    //! Convert a dictionary to a map.
    std::map<std::string, std::vector<std::string> >
      ParseDictionary(std::string const & str)
    {
      std::map<std::string, std::vector<std::string> > result;
      size_t idx0 = 0;
      size_t idx1 = 0;
      size_t idx2 = 0;
      std::string key;
      std::vector<std::string> value;
      while (idx1 != std::string::npos && idx2 != std::string::npos)
      {
        idx1 = str.find('[', idx1);
        if (idx1 != std::string::npos)
        {
          idx2 = str.find(']', idx1 + 1);
          key = ParseWords(str.substr(idx0, idx1 - idx0));
          if (idx2 != std::string::npos)
          {
            value = ParseList(str.substr(idx1, idx2 - idx1));
          }
          else
          {
            break;
          }
          idx0 = idx1 = idx2;
        }
        else
        {
          break;
        }
        result[key] = value;
      }
      return result;
    }

  public:
    std::map<std::string, std::vector<std::string> > cn;
    // Ordering of the tables and when to start and end a column of
    // tables in the layout.
    std::vector<std::string> cnOrder;
    std::vector<std::string> cnStartTable;
    std::vector<std::string> cnEndTable;

    std::map<std::string, std::vector<std::string> > vtkcn;
    // Ordering of the tables and when to start and end a column of
    // tables in the layout.
    std::vector<std::string> vtkcnOrder;
    std::vector<std::string> vtkcnStartTable;
    std::vector<std::string> vtkcnEndTable;
  };

  ColorStructures cs = ColorStructures();
  STLHelpers stlHelpers = STLHelpers();
  HTMLToFromRGBAColor htmlRGBA = HTMLToFromRGBAColor();
}

/*!
  This class creates HTML Tables displaying all the colors in
  the class vtkNamedColors grouped by various categories.
*/
class NamedColorPatches
{

  public:
    //-----------------------------------------------------------------------------
    NamedColorPatches()
    {
      this->nc = vtkSmartPointer<vtkNamedColors>::New();
    }

    //-----------------------------------------------------------------------------
    ~NamedColorPatches()
    {
    }

  //-----------------------------------------------------------------------------
    std::string MakeWebColorPage()
    {
      std::string res = this->MakeHTMLHeader();
      res += "<body>\n";
      res += "<h1>Colors available in vtkNamedColors</h1>\n";
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
      res += "<caption style=\"background:lightgrey\">Web Color Names</caption>\n";
      res += "<tr valign=\"top\">\n";
      res += this->MakeWebColorTables();
      res += "</table>\n";
      res += "</body>\n";
      return res;
    }

    //-----------------------------------------------------------------------------
    std::string MakeVTKColorPage()
    {
      std::string res = this->MakeHTMLHeader();
      res += "<body>\n";
      res += "<h1>Colors available in vtkNamedColors</h1>\n";
      res += "<p>The web colors take precedence over colors of the same";
      res += " name in VTK Color Names.</p>\n";
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
      res += "<caption style=\"background:lightgrey\">VTK Color Names</caption>\n";
      res += "<tr valign=\"top\">\n";
      res += this->MakeVTKColorTables();
      res += "</table>\n";
      res += "</body>\n";
      return res;
    }

    //-----------------------------------------------------------------------------
    std::string MakeSynonymColorPage()
    {
      std::string res = this->MakeHTMLHeader();
      res += "<body>\n";
      res += "<h1>Synonyms in vtkNamedColors</h1>\n";
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
      res += "<caption style=\"background:lightgrey\"></caption>\n";
      res += "<tr valign=\"top\">\n";
      res += this->MakeSynonymColorTable();
      res += "</table>\n";
      res += "</body>\n";
      return res;
    }

    //-----------------------------------------------------------------------------
    std::string MakeCombinedColorPage()
    {
      std::string res = this->MakeHTMLHeader();
      res += "<body>\n";
      res += "<h1>Colors available in vtkNamedColors</h1>\n";
      res += "<p>The class vtkNamedColors provides color names and their";
      res += " values for the convenience of the user.</p>\n";
      res += "<p>The following tables show the available colors along with";
      res += " their red, green and blue values.</p>\n";
      res += this->MakeHTMLIndex();
      res += "<h2><a id=\"WebColorNames\">Web color Names</h2>";
      res += "These colors correspond to those in";
      res += " <a href=\"http://en.wikipedia.org/wiki/Web_colors\"";
      res += " title=\"Web Colors\">Web Colors</a>.\n";
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
      res += "<tr valign=\"top\">\n";
      res += this->MakeWebColorTables();
      res += "</table>\n";
      res += "<h2><a id=\"VTKColorNames\">VTK color Names</h2>";
      res += "<p>The colors mainly correspond to those in vtkColors.txt.\n</p>\n";
      res += "<p>The web colors (above) take precedence over colors of the";
      res += " same name in vtkColors.txt.</p>\n";
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
      res += "<tr valign=\"top\">\n";
      res += this->MakeVTKColorTables();
      res += "</table>\n";
      res += "<h2><a id=\"Synonyms\">Synonyms</h2>";
      res += "<table style=\"font-size:90%\" cellpadding=\"4\">\n";
      res += "<tr valign=\"top\">\n";
      res += this->MakeSynonymColorTable();
      res += "</table>\n";
      res += "</body>\n";
      return res;
    }

  private:
    //-----------------------------------------------------------------------------
    std::vector<vtkStdString> ParseColorNames(
                                              const vtkStdString & colorNames)
    {
      // The delimiter for a color.
      const std::string colorDelimiter = "\n";
      std::vector<vtkStdString> cn;
      size_t start = 0;
      size_t end = colorNames.find(colorDelimiter);
      while (end != std::string::npos)
        {
        cn.push_back(colorNames.substr(start, end - start));
        start = end + 1;
        end = colorNames.find(colorDelimiter, start);
        }
      // Get the last color.
      if (!colorNames.empty())
        {
        cn.push_back(colorNames.substr(start, colorNames.size() - start));
        }
      return cn;
    }

    //-----------------------------------------------------------------------------
    std::vector<std::vector<vtkStdString> > ParseSynonyms(
                                                      const vtkStdString & synonyms)
    {
      // The delimiter for a string of synonyms.
      const vtkStdString synonymDelimiter = "\n\n";
      size_t start = 0;
      size_t end = synonyms.find(synonymDelimiter);
      std::vector<vtkStdString> cn;
      std::vector<std::vector<vtkStdString> > syn;
      vtkStdString str;
      while (end != std::string::npos)
        {
        str = synonyms.substr(start, end - start);
        cn = ParseColorNames(str);
        syn.push_back(cn);
        start = end + 2;
        end = synonyms.find(synonymDelimiter, start);
        }
      // Get the last set of synonyms.
      if (!synonyms.empty())
        {
        str = synonyms.substr(start, end - start);
        cn = ParseColorNames(str);
        syn.push_back(cn);
        }
      return syn;
    }

    //-----------------------------------------------------------------------------
    std::string MakeHTMLHeader()
    {
      std::string s = "<!DOCTYPE html>\n";
      s += "<html lang=\"en\" dir=\"ltr\" class=\"client-nojs\">\n";
      s += "<head>\n";
      s += "<title>vtkNamedColors</title>\n";
      s += "<meta charset=\"UTF-8\" />\n";
      s += "</head>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    std::string MakeHTMLIndex()
    {
      std::string s = "<h2>Index</h2>\n";
      s += "<ul>\n";
      s += "\t<li><a href=\"#WebColorNames\">Web color Names</li>\n";
      s += "\t<li><a href=\"#VTKColorNames\">VTK color Names</li>\n";
      s += "\t<li><a href=\"#Synonyms\">Synonyms</li>\n";
      s += "</ul>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    std::string MakeTableHeader()
    {
      std::string s = "<tr>\n";
      s += "<th style=\"background:lightgrey\">HTML name</th>\n";
      s += "<th style=\"background:lightgrey\">Decimal code<br />\n";
      s += "&#160;&#160;R&#160;&#160;&#160;&#160;&#160;&#160;";
      s += "G&#160;&#160;&#160;&#160;&#160;&#160;B</th>\n";
      s += "</tr>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    std::string MakeSynonymTableHeader()
    {
      std::string s = "<tr>\n";
      s += "<th style=\"background:lightgrey\">Synonyms</th>\n";
      s += "<th style=\"background:lightgrey\">Decimal code<br />\n";
      s += "&#160;&#160;R&#160;&#160;&#160;&#160;&#160;&#160;";
      s += "G&#160;&#160;&#160;&#160;&#160;&#160;B</th>\n";
      s += "</tr>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    std::string MakeTD(std::string const & name)
    {
      std::string s = "<tr>\n";
      s += "<td colspan=\"2\" style=\"background:whitesmoke;";
      s += "color:slategray;text-align:left\"><big><b>" + name + "</b></big></td>\n";
      s += "</tr>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    std::string FormatRGBForHTML(vtkColor3ub const & rgb)
    {
      std::string s;
      char buff[9];
      snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetRed());
      s += buff;
      s += +",";
      snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetGreen());
      s += buff;
      s += +",";
      snprintf(buff, sizeof(buff) - 1, "%3d", rgb.GetBlue());
      s += buff;
      s = stlHelpers.ReplaceAll(s, " ", "&#160;");
      s = stlHelpers.ReplaceAll(s, ",", "&#160;&#160;");
      return s;
    }

    //-----------------------------------------------------------------------------
    /*!
      Use when the name is a color name known to the web browser.
    */
    std::string  MakeTR(const std::string & name,
                            vtkColor3ub const & rgb, const std::string & textColor)
    {
      std::string s = "<tr>\n";
      s += "<td style=\"background:" + name + ";color:" + textColor;
      s += "\">" + name + "</td>\n";
      s += "<td style=\"background:" + name + ";color:" + textColor;
      s += "\"><tt>" + this->FormatRGBForHTML(rgb) + "</tt></td>\n";
      s += "</tr>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    /*!
      Use when the name is not a color name known to the web browser.
     */
    std::string  MakeSynonymTR(const std::string & name,
                            const std::string & nameColor, vtkColor3ub const & rgb,
                            const std::string & textColor)
    {
      std::string s = "<tr>\n";
      s += "<td style=\"background:" + nameColor + ";color:" + textColor;
      s += "\">" + name + "</td>\n";
      s += "<td style=\"background:" + nameColor + ";color:" + textColor;
      s += "\"><tt>" + this->FormatRGBForHTML(rgb) + "</tt></td>\n";
      s += "</tr>\n";
      return s;
    }

    //-----------------------------------------------------------------------------
    std::string  MakeWebColorTables()
    {
      std::string res;
      for (std::vector<std::string>::const_iterator
        p = cs.cnOrder.begin(); p != cs.cnOrder.end(); ++p)
        {
        if (std::find(
          cs.cnStartTable.begin(),  cs.cnStartTable.end(), *p)
          != cs.cnStartTable.end())
          {
          res += "<td>\n";
          res += "<table>\n";
          res += this->MakeTableHeader();
          }
        // Add in the name of the group in the color table.
        res += this->MakeTD(*p + " colors");
        std::vector<std::string> values = cs.cn[*p];
        for (std::vector<std::string>::const_iterator
          q = values.begin(); q != values.end(); ++q)
          {
          vtkColor3ub rgb = this->nc->GetColor3ub(*q);
          double y = htmlRGBA.RGBToLumaCCIR601(rgb);
          std::string textColor = "#000000"; // Black
          if (y < 255 / 2.0)
            {
            textColor = "#ffffff"; // White
            }
          // Make the row for each color in the group.
          // Here we use the name to set the background color
          //  as it is known to the web browser.
          res += this->MakeTR(*q, rgb, textColor);
          }
        if (std::find(
          cs.cnEndTable.begin(), cs.cnEndTable.end(), *p)
          != cs.cnEndTable.end())
          {
          res += "</table>\n";
          res += "</td>\n";
          }
      }
      return res;
    }

    //-----------------------------------------------------------------------------
    std::string  MakeVTKColorTables()
    {
      std::string res;

      for (std::vector<std::string>::const_iterator
        p = cs.vtkcnOrder.begin(); p != cs.vtkcnOrder.end(); ++p)
        {
        if (std::find(
          cs.vtkcnStartTable.begin(), cs.vtkcnStartTable.end(), *p)
          != cs.vtkcnStartTable.end())
          {
          res += "<td>\n";
          res += "<table>\n";
          res += this->MakeTableHeader();
          }
        // Add in the name of the group in the color table.
        res += this->MakeTD(*p);
        std::vector<std::string> values = cs.vtkcn[*p];
        for (std::vector<std::string>::const_iterator
          q = values.begin(); q != values.end(); ++q)
          {
          vtkColor3ub rgb = this->nc->GetColor3ub(*q);
          double y = htmlRGBA.RGBToLumaCCIR601(rgb);
          std::string textColor = "#000000"; // Black
          if (y < 255 / 2.0)
            {
            textColor = "#ffffff"; // White
            }
          // We must set the background color to a specific
          // HTML color as the color name may not be a standard
          // name known to the web browser.
          std::string nameColor = htmlRGBA.RGBToHTMLColor(rgb);
          // Make the row for each color in the group.
          res += this->MakeSynonymTR(*q, nameColor, rgb, textColor);
        }
        if (std::find(
          cs.vtkcnEndTable.begin(), cs.vtkcnEndTable.end(), *p)
          != cs.vtkcnEndTable.end())
          {
          res += "</table>\n";
          res += "</td>\n";
          }
      }
      return res;
    }

    //-----------------------------------------------------------------------------
    std::string MakeSynonymColorTable()
    {
      std::vector<std::vector<vtkStdString > > synonyms
        = this->ParseSynonyms(this->nc->GetSynonyms());
      std::vector<std::string> cn;
      for (std::map<std::string, std::vector<std::string> >::const_iterator
        p = cs.cn.begin(); p != cs.cn.end(); ++p)
        {
        for (std::vector<std::string>::const_iterator
          q = p->second.begin(); q != p->second.end(); ++q)
          {
          cn.push_back(*q);
          }
        }
      std::map<std::string, std::string> d;
      for (std::vector<std::string>::const_iterator
        p = cn.begin(); p != cn.end(); ++p)
        {
        d[stlHelpers.ToLowercase(*p)] = *p;
        }
      std::string res;
      res += "<td>\n";
      res += "<table>\n";
      res += this->MakeSynonymTableHeader();
      for (std::vector<std::vector<vtkStdString > >::iterator
        p = synonyms.begin(); p != synonyms.end(); ++p)
        {
        int idx = 0;
        for (std::vector<vtkStdString >::iterator
          q = p->begin(); q != p->end(); ++q)
          {
          // Replace the lowercase name with the proper name.
          if (d.find(*q) != d.end())
            {
            *q = d[*q];
            }
          }
        std::sort(p->begin(), p->end());
        std::string names;
        for (std::vector<vtkStdString >::iterator
          q = p->begin(); q != p->end(); ++q)
          {
          names += *q;
          if (!stlHelpers.IsLast(q, *p))
            {
            names += ", ";
            }
          }
        vtkColor3ub rgb = this->nc->GetColor3ub(*(p->begin()));
        double y = htmlRGBA.RGBToLumaCCIR601(rgb);
        std::string textColor = "#000000"; // Black
        if (y < 255 / 2.0)
          {
          textColor = "#ffffff"; // White
          }
        // We must set the background color to a specific
        // HTML color because names is just a list of
        // synonyms for that particular color.
        std::string nameColor = htmlRGBA.RGBToHTMLColor(rgb);
        // Make the row for each color in the group.
        res += this->MakeSynonymTR(names, nameColor, rgb, textColor);
      }
      res += "</table>\n";
      res += "</td>\n";
      return res;
    }

  private:
    vtkSmartPointer<vtkNamedColors> nc;
};

int main()
{
  NamedColorPatches ncp;

  std::ofstream outputFile;
  outputFile.open("VTKNamedColorPatches.html",
                    ios::out | ios::trunc | ios::binary);
  outputFile << ncp.MakeCombinedColorPage();
  outputFile.close();

  return EXIT_SUCCESS;
}


Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(NamedColorPatches)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(NamedColorPatches MACOSX_BUNDLE NamedColorPatches.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(NamedColorPatches ${VTK_LIBRARIES})
else()
  target_link_libraries(NamedColorPatches vtkHybrid vtkWidgets)
endif()

Download and Build NamedColorPatches

Click here to download NamedColorPatches. and its CMakeLists.txt file.

Once the tarball NamedColorPatches.tar has been downloaded and extracted,

cd NamedColorPatches/build 
  • If VTK is installed:
cmake ..
  • If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./NamedColorPatches

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.