Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r369 - swig/Core/Color


Chronological Thread 
  • From: bigler@sci.utah.edu
  • To: manta@sci.utah.edu
  • Subject: [MANTA] r369 - swig/Core/Color
  • Date: Tue, 7 Jun 2005 16:35:35 -0600 (MDT)

Author: bigler
Date: Tue Jun  7 16:35:35 2005
New Revision: 369

Modified:
   swig/Core/Color/ColorDB.cc
   swig/Core/Color/ColorDB.h
Log:
Added test function that returns true if the namedColors are sorted
properly and can be found using getNamedColor.

Sorted the namedColors based on the behavior of
std::string::operator<.



Modified: swig/Core/Color/ColorDB.cc
==============================================================================
--- swig/Core/Color/ColorDB.cc  (original)
+++ swig/Core/Color/ColorDB.cc  Tue Jun  7 16:35:35 2005
@@ -1,13 +1,25 @@
 
 #include <Core/Color/ColorDB.h>
 #include <Core/Exceptions/UnknownColor.h>
+
+#include <sgi_stl_warnings_off.h>
+#include <iostream>
+#include <sgi_stl_warnings_on.h>
+
 using namespace Manta;
+using namespace std;
 
 struct RGBData {
   char* name;
   unsigned char r, g, b;
 };
 
+// If you add a color, remember that the emacs's sort doesn't do the
+// right thing.  Emacs places "lavender blush" before "lavender",
+// however the std::string::operator< places it the other way.  After
+// you add a color, run the provided test function to make sure that
+// everything is sorted properly.
+
 static RGBData namedColors[] = {
   {"AliceBlue", 240, 248, 255},
   {"AntiqueWhite", 250, 235, 215},
@@ -292,8 +304,8 @@
   {"bisque4", 139, 125, 107},
   {"black", 0, 0, 0},
   {"blanched almond", 255, 235, 205},
-  {"blue violet", 138, 43, 226},
   {"blue", 0, 0, 255},
+  {"blue violet", 138, 43, 226},
   {"blue1", 0, 0, 255},
   {"blue2", 0, 0, 238},
   {"blue3", 0, 0, 205},
@@ -480,8 +492,8 @@
   {"gray97", 247, 247, 247},
   {"gray98", 250, 250, 250},
   {"gray99", 252, 252, 252},
-  {"green yellow", 173, 255, 47},
   {"green", 0, 255, 0},
+  {"green yellow", 173, 255, 47},
   {"green1", 0, 255, 0},
   {"green2", 0, 238, 0},
   {"green3", 0, 205, 0},
@@ -605,15 +617,15 @@
   {"khaki2", 238, 230, 133},
   {"khaki3", 205, 198, 115},
   {"khaki4", 139, 134, 78},
-  {"lavender blush", 255, 240, 245},
   {"lavender", 230, 230, 250},
+  {"lavender blush", 255, 240, 245},
   {"lawn green", 124, 252, 0},
   {"lemon chiffon", 255, 250, 205},
   {"light blue", 173, 216, 230},
   {"light coral", 240, 128, 128},
   {"light cyan", 224, 255, 255},
-  {"light goldenrod yellow", 250, 250, 210},
   {"light goldenrod", 238, 221, 130},
+  {"light goldenrod yellow", 250, 250, 210},
   {"light gray", 211, 211, 211},
   {"light green", 144, 238, 144},
   {"light grey", 211, 211, 211},
@@ -652,12 +664,12 @@
   {"misty rose", 255, 228, 225},
   {"moccasin", 255, 228, 181},
   {"navajo white", 255, 222, 173},
-  {"navy blue", 0, 0, 128},
   {"navy", 0, 0, 128},
+  {"navy blue", 0, 0, 128},
   {"old lace", 253, 245, 230},
   {"olive drab", 107, 142, 35},
-  {"orange red", 255, 69, 0},
   {"orange", 255, 165, 0},
+  {"orange red", 255, 69, 0},
   {"orange1", 255, 165, 0},
   {"orange2", 238, 154, 0},
   {"orange3", 205, 133, 0},
@@ -746,17 +758,17 @@
   {"turquoise2", 0, 229, 238},
   {"turquoise3", 0, 197, 205},
   {"turquoise4", 0, 134, 139},
-  {"violet red", 208, 32, 144},
   {"violet", 238, 130, 238},
+  {"violet red", 208, 32, 144},
   {"wheat", 245, 222, 179},
   {"wheat1", 255, 231, 186},
   {"wheat2", 238, 216, 174},
   {"wheat3", 205, 186, 150},
   {"wheat4", 139, 126, 102},
-  {"white smoke", 245, 245, 245},
   {"white", 255, 255, 255},
-  {"yellow green", 154, 205, 50},
+  {"white smoke", 245, 245, 245},
   {"yellow", 255, 255, 0},
+  {"yellow green", 154, 205, 50},
   {"yellow1", 255, 255, 0},
   {"yellow2", 238, 238, 0},
   {"yellow3", 205, 205, 0},
@@ -765,14 +777,16 @@
 
 Color ColorDB::getNamedColor(const std::string& name)
 {
+  // A simple binary search of our namedColors array.
   int l = 0;
-  int h = sizeof(namedColors)/sizeof(RGBData)-1;
+  int h = sizeof(namedColors)/sizeof(RGBData);
   while(h > l+1){
     int m = (h+l)/2;
-    if(name < namedColors[m].name)
+    if(name < namedColors[m].name) {
       h = m;
-    else
+    } else {
       l = m;
+    }
   }
   if(name != namedColors[l].name)
     throw UnknownColor(name);
@@ -780,4 +794,30 @@
   float scale = 1.f/255.f;
   RGBData& nc = namedColors[l];
   return Color(RGBColor(nc.r*scale, nc.g*scale, nc.b*scale));
+}
+
+bool ColorDB::test() {
+  bool passed_all = true;
+  size_t num_entries = sizeof(namedColors)/sizeof(RGBData);
+  //  cerr << "num_entries = "<<num_entries<<"\n";
+
+  // Test to make sure the array is ordered.
+  for(size_t i = 0; i < num_entries-1; i++) {
+    if (string(namedColors[i+1].name) < string(namedColors[i].name)) {
+      cerr << "namedColors[i="<<i<<"].name("<<namedColors[i].name<<") is not 
< namedColors[i+1="<<i+1<<"].name("<<namedColors[i+1].name<<")\n";
+      passed_all = false;
+    }
+  }
+
+  // Test to make sure that we can find all the entries using the
+  // getNamedColor fuction.
+  for(size_t i = 0; i < num_entries; i++) {
+    try {
+      Color t = getNamedColor(string(namedColors[i].name));
+    } catch (UnknownColor& e) {
+      cerr << "Didn't find color \""<<e.message() << "\"\n";
+      passed_all = false;
+    }
+  }
+  return passed_all;
 }

Modified: swig/Core/Color/ColorDB.h
==============================================================================
--- swig/Core/Color/ColorDB.h   (original)
+++ swig/Core/Color/ColorDB.h   Tue Jun  7 16:35:35 2005
@@ -11,6 +11,11 @@
   class ColorDB {
   public:
     static Color getNamedColor(const std::string& name);
+
+    // Tests to make sure the namedColors array is sorted properly and
+    // that you can find all the entries.  Return true if everything
+    // is in order, false otherwise with some errors going to cerr.
+    static bool  test();
   private:
     // Prevent instantiation
     ColorDB(const ColorDB&);




  • [MANTA] r369 - swig/Core/Color, bigler, 06/07/2005

Archive powered by MHonArc 2.6.16.

Top of page