color.cc
Go to the documentation of this file.
1 #include "stage.hh"
2 #include "worldfile.hh"
3 using namespace Stg;
4 
5 #include "file_manager.hh"
6 
7 #include <errno.h>
8 
9 Color::Color( double r, double g, double b, double a )
10  : r(r),g(g),b(b),a(a)
11 {}
12 
14  r(1.0), g(0.0), b(0.0), a(1.0)
15 {}
16 
17 bool Color::operator!=( const Color& other ) const
18 {
19  double epsilon = 1e-4; // small
20  return( fabs(r-other.r) > epsilon ||
21  fabs(g-other.g) > epsilon ||
22  fabs(b-other.b) > epsilon ||
23  fabs(a-other.a) > epsilon );
24 }
25 
26 Color::Color( const std::string& name) :
27  r(1), g(0), b(0), a(1)
28 {
29  if( name == "" ) // empty string?
30  return; // red
31 
32  static FILE *file = NULL;
33  static std::map<std::string,Color> table;
34 
35  if( file == NULL )
36  {
37  std::string rgbFile = FileManager::findFile( "rgb.txt" );
38  file = fopen( rgbFile.c_str(), "r" );
39 
40  if( file == NULL )
41  {
42  PRINT_ERR1("unable to open color database: %s "
43  "(try adding rgb.txt's location to your STAGEPATH)",
44  strerror(errno));
45 
46  exit(0);
47  }
48 
49  PRINT_DEBUG( "Success!" );
50 
51  // load the file into the map
52  while(1)
53  {
54  char line[1024];
55  if (!fgets(line, sizeof(line), file))
56  break;
57 
58  // it's a macro or comment line - ignore the line
59  // also ignore empty lines
60  if (line[0] == '!' || line[0] == '#' || line[0] == '%' || line[0] == '\0')
61  continue;
62 
63  // Trim the trailing space
64  while (strchr(" \t\n", line[strlen(line)-1]))
65  line[strlen(line)-1] = 0;
66 
67  // Read the color
68  int r, g, b;
69  int chars_matched = 0;
70  sscanf( line, "%d %d %d %n", &r, &g, &b, &chars_matched );
71 
72  // Read the name
73  const char* colorname = line + chars_matched;
74 
75  // map the name to the color in the table
76  table[colorname] = Color( r/255.0, g/255.0, b/255.0 );
77  }
78  fclose(file);
79  }
80 
81  // look up the colorname in the database
82  Color& found = table[name];
83 
84  this->r = found.r;
85  this->g = found.g;
86  this->b = found.b;
87  this->a = found.a;
88 }
89 
90 bool Color::operator==( const Color& other ) const
91 {
92  return( ! ((*this) != other) );
93 }
94 
96 {
97  return Color( drand48(), drand48(), drand48() );
98 }
99 
100 void Color::Print( const char* prefix ) const
101 {
102  printf( "%s [%.2f %.2f %.2f %.2f]\n", prefix, r,g,b,a );
103 }
104 
105 const Color& Color::Load( Worldfile* wf, const int section )
106 {
107  if( wf->PropertyExists( section, "color" ))
108  {
109  const std::string& colorstr = wf->ReadString( section, "color", "" );
110  if( colorstr != "" )
111  {
112  if( colorstr == "random" )
113  {
114  r = drand48();
115  g = drand48();
116  b = drand48();
117  a = 1.0;
118  }
119  else
120  {
121  Color c = Color( colorstr );
122  r = c.r;
123  g = c.g;
124  b = c.b;
125  a = c.a;
126  }
127  }
128  }
129  else
130  wf->ReadTuple( section, "color_rgba", 0, 4, "ffff", &r, &g, &b, &a );
131 
132  return *this;
133 }
134 
double a
Definition: stage.hh:200
The Stage library uses its own namespace.
Definition: canvas.hh:8
const std::string ReadString(int entity, const char *name, const std::string &value)
Definition: worldfile.cc:1376
static Color RandomColor()
Definition: color.cc:95
int ReadTuple(const int entity, const char *name, const unsigned int first, const unsigned int num, const char *format,...)
Definition: worldfile.cc:1506
const Color & Load(Worldfile *wf, int entity)
Definition: color.cc:105
double r
Definition: stage.hh:200
bool PropertyExists(int section, const char *token)
Definition: worldfile.cc:1324
double b
Definition: stage.hh:200
double g
Definition: stage.hh:200
static std::string findFile(const std::string &filename)
Definition: file_manager.cc:36
Color()
Definition: color.cc:13
bool operator!=(const Color &other) const
Definition: color.cc:17
#define PRINT_ERR1(m, a)
Definition: stage.hh:626
bool operator==(const Color &other) const
Definition: color.cc:90
#define PRINT_DEBUG(m)
Definition: stage.hh:666
void Print(const char *prefix) const
Definition: color.cc:100


stage
Author(s): Richard Vaughan , Brian Gerkey , Reed Hedges , Andrew Howard , Toby Collett , Pooya Karimian , Jeremy Asher , Alex Couture-Beil , Geoff Biggs , Rich Mattes , Abbas Sadat
autogenerated on Mon Jun 10 2019 15:06:09