COPASI API  4.16.103
CLColorDefinition.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/layout/CLColorDefinition.cpp,v $
3 // $Revision: 1.5 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/04/23 15:44:50 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 #include <sstream>
15 #include <iomanip>
16 #include <assert.h>
17 
18 #include <sbml/packages/render/sbml/ColorDefinition.h>
19 
20 #include "CLColorDefinition.h"
21 
22 #ifndef WIN32
23 # include <inttypes.h>
24 #endif
25 
28 
29 /**
30  * Contructor which sets the CLColorDefinition to completely opaque
31  * black.
32  */
34  : CLBase(),
35  CCopasiObject("ColorDefinition", pParent),
36  mRed(0),
37  mGreen(0),
38  mBlue(0),
39  mAlpha(255),
40  mKey(""),
41  mId("")
42 {
43  this->mKey = CCopasiRootContainer::getKeyFactory()->add("ColorDefinition", this);
44 }
45 
46 /**
47  * Constructor which sets the CLColorDefinition to the given RGBA values.
48  */
49 CLColorDefinition::CLColorDefinition(unsigned char r, unsigned char g, unsigned char b, unsigned char a, CCopasiContainer* pParent)
50  : CLBase(),
51  CCopasiObject("ColorDefinition", pParent),
52  mRed(r),
53  mGreen(g),
54  mBlue(b),
55  mAlpha(a),
56  mKey(""),
57  mId("")
58 {
59  this->mKey = CCopasiRootContainer::getKeyFactory()->add("ColorDefinition", this);
60 }
61 
62 /**
63  * Copy Contructor
64  */
66  CLBase(source),
67  CCopasiObject(source, pParent),
68  mRed(source.mRed),
69  mGreen(source.mGreen),
70  mBlue(source.mBlue),
71  mAlpha(source.mAlpha),
72  mKey(""),
73  mId(source.mId)
74 {
75  this->mKey = CCopasiRootContainer::getKeyFactory()->add("ColorDefinition", this);
76 }
77 
78 /**
79  * Constructor to generate object from the corresponding SBML object.
80  */
81 CLColorDefinition::CLColorDefinition(const ColorDefinition& source, CCopasiContainer* pParent):
82  CLBase(),
83  CCopasiObject("ColorDefinition", pParent),
84  mRed(source.getRed()),
85  mGreen(source.getGreen()),
86  mBlue(source.getBlue()),
87  mAlpha(source.getAlpha()),
88  mKey(""),
89  mId(source.getId())
90 {
91  this->mKey = CCopasiRootContainer::getKeyFactory()->add("ColorDefinition", this);
92 }
93 
94 /**
95  * Dtor.
96  */
98 {
100 }
101 
102 /**
103  * Returns the red color component.
104  */
105 unsigned char CLColorDefinition::getRed() const
106 {
107  return mRed;
108 }
109 
110 /**
111  * Returns the green color component.
112  */
113 unsigned char CLColorDefinition::getGreen() const
114 {
115  return mGreen;
116 }
117 
118 /**
119  * Returns the blue color component.
120  */
121 unsigned char CLColorDefinition::getBlue() const
122 {
123  return mBlue;
124 }
125 
126 /**
127  * Returns the alpha color component.
128  */
129 unsigned char CLColorDefinition::getAlpha() const
130 {
131  return mAlpha;
132 }
133 
134 /**
135  * Sets the red color component.
136  */
137 void CLColorDefinition::setRed(unsigned char c)
138 {
139  this->mRed = c;
140 }
141 
142 /**
143  * Sets the green color component.
144  */
145 void CLColorDefinition::setGreen(unsigned char c)
146 {
147  this->mGreen = c;
148 }
149 
150 /**
151  * Sets the blue color component.
152  */
153 void CLColorDefinition::setBlue(unsigned char c)
154 {
155  this->mBlue = c;
156 }
157 
158 /**
159  * Sets alpha red color component.
160  */
161 void CLColorDefinition::setAlpha(unsigned char c)
162 {
163  this->mAlpha = c;
164 }
165 
166 /**
167  * Sets the red green, blue and alpha color component.
168  * The alpha value is optional and defaults to 255 if not given.
169  */
170 void CLColorDefinition::setRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
171 {
172  this->mRed = r;
173  this->mGreen = g;
174  this->mBlue = b;
175  this->mAlpha = a;
176 }
177 
178 /**
179  * Sets the color value from a given string.
180  * If the string is not a valid value string, the color value is set to
181  * black and false is returned.
182  */
183 bool CLColorDefinition::setColorValue(const std::string& valueString)
184 {
185  bool result = true;
186  size_t first_letter = valueString.find_first_not_of(std::string(" \t\r\n"));
187 
188  if (first_letter == std::string::npos)
189  {
190  result = false;
191  }
192  else
193  {
194  std::string trimmed = valueString.substr(first_letter, valueString.find_last_not_of(" \t\r\n") - first_letter + 1);
195 
196  if (trimmed[0] == '#' && (trimmed.size() == 7 || trimmed.size() == 9) && trimmed.find_first_not_of("0123456789ABCDEFabcdef", 1) == std::string::npos)
197  {
198  this->mRed = (unsigned char)strtol(trimmed.substr(1, 2).c_str(), NULL, 16);
199  this->mGreen = (unsigned char)strtol(trimmed.substr(3, 2).c_str(), NULL, 16);
200  this->mBlue = (unsigned char)strtol(trimmed.substr(5, 2).c_str(), NULL, 16);
201 
202  if (trimmed.size() == 9)
203  {
204  // set the alpha value
205  this->mAlpha = (unsigned char)strtol(trimmed.substr(7, 2).c_str(), NULL, 16);
206  }
207  else
208  {
209  // if no alpha is given, the color is completely opaque
210  this->mAlpha = 255;
211  }
212  }
213  else
214  {
215  result = false;
216  }
217  }
218 
219  if (result == false)
220  {
221  this->mRed = 0;
222  this->mGreen = 0;
223  this->mBlue = 0;
224  this->mAlpha = 255;
225  }
226 
227  return result;
228 }
229 
230 /**
231  * Creates a string the represents the current color value.
232  */
234 {
235  std::ostringstream os;
236  os << "#" << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)mRed << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)mGreen << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)mBlue;
237 
238  if (mAlpha != 255)
239  {
240  os << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)mAlpha;
241  }
242 
243  return os.str();
244 }
245 
246 /**
247  * Returns the key of the color definition.
248  */
249 const std::string& CLColorDefinition::getKey() const
250 {
251  return this->mKey;
252 }
253 
254 /**
255  * Converts this object to the corresponding SBML object.
256  */
257 ColorDefinition* CLColorDefinition::toSBML(unsigned int level, unsigned int version) const
258 {
259  ColorDefinition* pCD = new ColorDefinition(level, version);
260  pCD->setId(this->mId);
261  pCD->setRGBA(this->mRed, this->mGreen, this->mBlue, this->mAlpha);
262  return pCD;
263 }
264 
265 /**
266  * Returns the id of the object.
267  */
268 const std::string& CLColorDefinition::getId() const
269 {
270  return this->mId;
271 }
272 
273 /**
274  * Sets the id of the object.
275  */
276 void CLColorDefinition::setId(const std::string& id)
277 {
278  this->mId = id;
279 }
bool remove(const std::string &key)
void setBlue(unsigned char c)
void setRed(unsigned char c)
unsigned char mAlpha
const std::string & getKey() const
bool setColorValue(const std::string &valueString)
unsigned char getBlue() const
unsigned char mRed
CLColorDefinition(CCopasiContainer *pParent=NULL)
unsigned char mGreen
unsigned char getGreen() const
void setRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Definition: CLBase.h:33
unsigned char mBlue
unsigned char getRed() const
std::string add(const std::string &prefix, CCopasiObject *pObject)
void setId(const std::string &id)
unsigned char getAlpha() const
static CKeyFactory * getKeyFactory()
void setAlpha(unsigned char c)
std::string createValueString() const
const std::string & getId() const
void setGreen(unsigned char c)
ColorDefinition * toSBML(unsigned int level, unsigned int version) const