Color.cxx
Go to the documentation of this file.
1 
12 #include "Color.h"
13 
14 using std::string;
15 
16 using namespace hippodraw;
17 
18 // Used to generate a new color for composite plotter.
19 int Color::colorIndex=0;
20 
22 std::vector < std::string > Color::s_color_names;
23 
24 Color::Color ( int red, int green, int blue )
25  : m_red( red ), m_green( green ), m_blue( blue )
26 {
27 }
28 
29 Color::
31 {
32  setColor ( value );
33 }
34 
35 void
36 Color::
37 addColor ( const char * name, const Color & color )
38 {
39  string s ( name );
40  s_color_by_name [ s ] = color;
41 }
42 
43 void
44 Color::
46 {
47  switch ( value ) {
48  case Color::red :
49  setColor ( "red" );
50  break;
51  case Color::green :
52  setColor ( "green" );
53  break;
54  case Color::blue :
55  setColor ( "blue" );
56  break;
57  case Color::cyan :
58  setColor ( "cyan" );
59  break;
60  case Color::magenta :
61  setColor ( "magenta" );
62  break;
63  case Color::yellow :
64  setColor ( "yellow" );
65  break;
66  case Color::orange :
67  setColor ( "orange" );
68  break;
69  case Color::black :
70  setColor ( "black" );
71  break;
72  case Color::darkgray :
73  setColor ( "darkgray" );
74  break;
75  case Color::lightgray :
76  setColor ( "lightgray" );
77  break;
78  case Color::white :
79  setColor ( "white" );
80  break;
81  default :
82  setColor ( "black" );
83  break;
84  }
85 }
86 
87 void
88 Color::
90 {
91  addColor ( "red", Color ( 255, 0, 0 ) );
92  addColor ( "green", Color ( 0, 255, 0 ) );
93  addColor ( "blue", Color ( 0, 0, 255 ) );
94  addColor ( "cyan", Color ( 0, 255, 255 ) );
95  addColor ( "magenta", Color ( 255, 0, 255 ) );
96  addColor ( "yellow", Color ( 255, 255, 0 ) );
97  addColor ( "orange", Color ( 255, 165, 0 ) );
98  addColor ( "black", Color ( 0, 0, 0 ) );
99  addColor ( "darkgray", Color ( 152, 152, 152 ) );
100  addColor ( "lightgray", Color ( 211, 211, 211 ) );
101  addColor ( "white", Color ( 255, 255, 255 ) );
102 }
103 
104 Color::
105 Color ( const std::string & name )
106 {
107  setColor ( name );
108 }
109 
110 void
111 Color::
112 setColor ( const std::string & name )
113 {
114  bool yes = isValid ( name );
115  if ( yes ) {
116  ColorMapByName_t :: const_iterator first = s_color_by_name.find ( name );
117  const Color color = first -> second;
118  m_red = color.m_red;
119  m_green = color.m_green;
120  m_blue = color.m_blue;
121  }
122  else { // black
123  m_red = 0;
124  m_green = 0;
125  m_blue = 0;
126  }
127 }
128 
129 bool
130 Color::
131 isValid ( const std::string & name )
132 {
133  if ( s_color_by_name.empty () == true ) {
135  }
136 
137  ColorMapByName_t::const_iterator first = s_color_by_name.find ( name );
138 
139  return first != s_color_by_name.end ();
140 }
141 
142 const std::vector < std::string > &
143 Color::
145 {
146  // Make sure the list is up to date
147  s_color_names.clear ();
148  ColorMapByName_t::const_iterator it = s_color_by_name.begin();
149  while ( it != s_color_by_name.end () ) {
150  const string & name = (it++) -> first;
151  s_color_names.push_back ( name );
152  }
153 
154  return s_color_names;
155 }
156 
157 void Color::setColor ( int red, int green, int blue )
158 {
159  m_red = red;
160  m_green = green;
161  m_blue = blue;
162 }
163 
164 int Color::getRed () const
165 {
166  return m_red;
167 }
168 
169 int Color::getGreen () const
170 {
171  return m_green;
172 }
173 
174 int Color::getBlue () const
175 {
176  return m_blue;
177 }
178 
179 // Get a color from red, green, blue, magenta and orange.
180 // Use to plot multiple curves in a plotter.
182 {
183  Color::Value retColor;
184 
185  switch (colorIndex)
186  {
187  case 0: retColor= red; break;
188  case 1: retColor= green; break;
189  case 2: retColor= blue; break;
190  case 3: retColor= magenta; break;
191  default: retColor= orange;
192  }
193 
194  if (colorIndex==4) colorIndex=colorIndex-4;
195  else colorIndex++;
196 
197  return retColor;
198 }
199 
200 
201 bool
203 {
204  return ((m_red == c.getRed()) &&
205  (m_green == c.getGreen()) &&
206  (m_blue == c.getBlue()));
207 }
208 

Generated for HippoDraw Class Library by doxygen