bioformats  5.1.3
Color.h
1 /*
2  * #%L
3  * OME-XML C++ library for working with OME-XML metadata structures.
4  * %%
5  * Copyright © 2006 - 2015 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
39 #ifndef OME_XML_MODEL_PRIMITIVES_COLOR_H
40 #define OME_XML_MODEL_PRIMITIVES_COLOR_H
41 
42 #include <limits>
43 #include <string>
44 
45 #include <ome/compat/cstdint.h>
46 
47 namespace ome
48 {
49  namespace xml
50  {
51  namespace model
52  {
59  namespace primitives
60  {
61 
77  class Color
78  {
79  public:
81  typedef uint8_t component_type;
83  typedef uint32_t composed_type;
85  typedef int32_t signed_type;
86 
90  inline
91  Color ():
92  value(0x000000FFU)
93  {
94  }
95 
101  inline
102  Color (composed_type value):
103  value(value)
104  {
105  }
106 
112  inline
113  Color (signed_type value):
114  value(static_cast<composed_type>(value))
115  {
116  }
117 
127  inline
128  Color (component_type r,
129  component_type g,
130  component_type b,
131  component_type a = std::numeric_limits<component_type>::max()):
132  value(static_cast<composed_type>(r) << 24U |
133  static_cast<composed_type>(g) << 16U |
134  static_cast<composed_type>(b) << 8U |
135  static_cast<composed_type>(a) << 0U)
136  {
137  }
138 
148  Color (const std::string& str,
149  bool sign = true);
150 
151  inline
152  ~Color ()
153  {
154  }
155 
161  inline component_type
162  getRed () const
163  {
164  return static_cast<component_type>((this->value >> 24U) & 0xffU);
165  }
166 
172  inline void
173  setRed (component_type red)
174  {
175  this->value &= ~(0xFFU << 24U);
176  this->value |= static_cast<composed_type>(red) << 24U;
177  }
178 
184  inline component_type
185  getGreen () const
186  {
187  return static_cast<component_type>((this->value >> 16U) & 0xffU);
188  }
189 
195  inline void
196  setGreen (component_type green)
197  {
198  this->value &= ~(0xffU << 16U);
199  this->value |= static_cast<composed_type>(green) << 16U;
200  }
201 
207  inline component_type
208  getBlue () const
209  {
210  return static_cast<component_type>((this->value >> 8U) & 0xffU);
211  }
212 
218  inline void
219  setBlue (component_type blue)
220  {
221  this->value &= ~(0xffU << 8U);
222  this->value |= static_cast<composed_type>(blue) << 8U;
223  }
224 
230  inline component_type
231  getAlpha () const
232  {
233  return static_cast<component_type>((this->value >> 0) & 0xffU);
234  }
235 
241  inline void
242  setAlpha (component_type alpha)
243  {
244  this->value &= ~(0xffU << 0);
245  this->value |= static_cast<composed_type>(alpha) << 0;
246  }
247 
253  inline signed_type
254  getValue () const
255  {
256  return static_cast<signed_type>(this->value);
257  }
258 
264  inline void
265  setValue (composed_type value)
266  {
267  this->value = value;
268  }
269 
275  inline void
276  setValue (signed_type value)
277  {
278  this->value = static_cast<composed_type>(value);
279  }
280 
286  inline
287  operator composed_type () const
288  {
289  return this->value;
290  }
291 
297  inline
298  operator signed_type () const
299  {
300  return static_cast<signed_type>(this->value);
301  }
302 
303  private:
305  composed_type value;
306  };
307 
315  inline bool
316  operator== (const Color& lhs,
317  const Color& rhs)
318  {
319  return static_cast<Color::composed_type>(lhs) == static_cast<Color::composed_type>(rhs);
320  }
321 
329  inline bool
330  operator== (const Color& lhs,
332  {
333  return static_cast<Color::composed_type>(lhs) == rhs;
334  }
335 
343  inline bool
345  const Color& rhs)
346  {
347  return lhs == static_cast<Color::composed_type>(rhs);
348  }
349 
357  inline bool
358  operator== (const Color& lhs,
359  Color::signed_type rhs)
360  {
361  return static_cast<Color::signed_type>(lhs) == rhs;
362  }
363 
371  inline bool
373  const Color& rhs)
374  {
375  return lhs == static_cast<Color::signed_type>(rhs);
376  }
377 
385  inline bool
386  operator!= (const Color& lhs,
387  const Color& rhs)
388  {
389  return static_cast<Color::composed_type>(lhs) != static_cast<Color::composed_type>(rhs);
390  }
391 
399  inline bool
400  operator!= (const Color& lhs,
402  {
403  return static_cast<Color::composed_type>(lhs) != rhs;
404  }
405 
413  inline bool
415  const Color& rhs)
416  {
417  return lhs != static_cast<Color::composed_type>(rhs);
418  }
419 
427  inline bool
428  operator!= (const Color& lhs,
429  Color::signed_type rhs)
430  {
431  return static_cast<Color::signed_type>(lhs) != rhs;
432  }
433 
441  inline bool
443  const Color& rhs)
444  {
445  return lhs != static_cast<Color::signed_type>(rhs);
446  }
447 
455  template<class charT, class traits>
456  inline std::basic_ostream<charT,traits>&
457  operator<< (std::basic_ostream<charT,traits>& os,
458  const Color& color)
459  {
460  return os << static_cast<Color::signed_type>(color);
461  }
462 
470  template<class charT, class traits>
471  inline std::basic_istream<charT,traits>&
472  operator>> (std::basic_istream<charT,traits>& is,
473  Color& color)
474  {
476  if (is >> c)
477  color.setValue(c);
478  return is;
479  }
480 
481  }
482  }
483  }
484 }
485 
486 #endif // OME_XML_MODEL_PRIMITIVES_COLOR_H
487 
488 /*
489  * Local Variables:
490  * mode:C++
491  * End:
492  */
uint8_t component_type
The type of an individual color component (R, G, B, A).
Definition: Color.h:81
void setAlpha(component_type alpha)
Set the alpha component of this color.
Definition: Color.h:242
bool operator!=(const Color &lhs, const Color &rhs)
Compare two Color objects for non-equality.
Definition: Color.h:386
int32_t signed_type
The type of all components composed as a single RGBA value (signed).
Definition: Color.h:85
Color(signed_type value)
Construct a Color from a signed integer value.
Definition: Color.h:113
void setValue(composed_type value)
Set the integer value of this color from an unsigned integer.
Definition: Color.h:265
component_type getGreen() const
Get the green component of this color.
Definition: Color.h:185
void setValue(signed_type value)
Set the integer value of this color from a signed integer.
Definition: Color.h:276
Color(composed_type value)
Construct a Color from an unsigned integer value.
Definition: Color.h:102
Color(component_type r, component_type g, component_type b, component_type a=std::numeric_limits< component_type >::max())
Construct a Color from separate red, green, blue and alpha components.
Definition: Color.h:128
signed_type getValue() const
Get the signed integer value of this color.
Definition: Color.h:254
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
void setGreen(component_type green)
Set the green component of this color.
Definition: Color.h:196
bool operator==(const Color &lhs, const Color &rhs)
Compare two Color objects for equality.
Definition: Color.h:316
void setBlue(component_type blue)
Set the blue component of this color.
Definition: Color.h:219
uint32_t composed_type
The type of all components composed as a single RGBA value (unsigned).
Definition: Color.h:83
Color()
Construct a Color.
Definition: Color.h:91
Standard integer types.
component_type getBlue() const
Get the blue component of this color.
Definition: Color.h:208
std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &is, Color &color)
Set Color from input stream.
Definition: Color.h:472
component_type getAlpha() const
Get the alpha component of this color.
Definition: Color.h:231
An RGBA color value.
Definition: Color.h:77
component_type getRed() const
Get the red component of this color.
Definition: Color.h:162
void setRed(component_type red)
Set the red component of this color.
Definition: Color.h:173
composed_type value
The color value.
Definition: Color.h:305
Xerces-C modern C++ wrapper.
Definition: Base.h:53