ome-xml  5.5.1
Color.h
1 /*
2  * #%L
3  * OME-XML C++ library for working with OME-XML metadata structures.
4  * %%
5  * Copyright © 2006 - 2016 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 <cstdint>
43 #include <limits>
44 #include <string>
45 
46 #ifdef _MSC_VER
47 #pragma push_macro("min")
48 #undef min
49 #pragma push_macro("max")
50 #undef max
51 #endif
52 
53 namespace ome
54 {
55  namespace xml
56  {
57  namespace model
58  {
65  namespace primitives
66  {
67 
83  class Color
84  {
85  public:
87  typedef uint8_t component_type;
89  typedef uint32_t composed_type;
91  typedef int32_t signed_type;
92 
96  inline
97  Color ():
98  value(0x000000FFU)
99  {
100  }
101 
107  inline
108  Color (composed_type value):
109  value(value)
110  {
111  }
112 
118  inline
119  Color (signed_type value):
120  value(static_cast<composed_type>(value))
121  {
122  }
123 
133  inline
134  Color (component_type r,
135  component_type g,
136  component_type b,
137  component_type a = std::numeric_limits<component_type>::max()):
138  value(static_cast<composed_type>(r) << 24U |
139  static_cast<composed_type>(g) << 16U |
140  static_cast<composed_type>(b) << 8U |
141  static_cast<composed_type>(a) << 0U)
142  {
143  }
144 
154  Color (const std::string& str,
155  bool sign = true);
156 
157  inline
158  ~Color ()
159  {
160  }
161 
167  inline component_type
168  getRed () const
169  {
170  return static_cast<component_type>((this->value >> 24U) & 0xffU);
171  }
172 
178  inline void
179  setRed (component_type red)
180  {
181  this->value &= ~(0xFFU << 24U);
182  this->value |= static_cast<composed_type>(red) << 24U;
183  }
184 
190  inline component_type
191  getGreen () const
192  {
193  return static_cast<component_type>((this->value >> 16U) & 0xffU);
194  }
195 
201  inline void
202  setGreen (component_type green)
203  {
204  this->value &= ~(0xffU << 16U);
205  this->value |= static_cast<composed_type>(green) << 16U;
206  }
207 
213  inline component_type
214  getBlue () const
215  {
216  return static_cast<component_type>((this->value >> 8U) & 0xffU);
217  }
218 
224  inline void
225  setBlue (component_type blue)
226  {
227  this->value &= ~(0xffU << 8U);
228  this->value |= static_cast<composed_type>(blue) << 8U;
229  }
230 
236  inline component_type
237  getAlpha () const
238  {
239  return static_cast<component_type>((this->value >> 0) & 0xffU);
240  }
241 
247  inline void
248  setAlpha (component_type alpha)
249  {
250  this->value &= ~(0xffU << 0);
251  this->value |= static_cast<composed_type>(alpha) << 0;
252  }
253 
259  inline signed_type
260  getValue () const
261  {
262  return static_cast<signed_type>(this->value);
263  }
264 
270  inline void
271  setValue (composed_type value)
272  {
273  this->value = value;
274  }
275 
281  inline void
282  setValue (signed_type value)
283  {
284  this->value = static_cast<composed_type>(value);
285  }
286 
292  inline
293  operator composed_type () const
294  {
295  return this->value;
296  }
297 
303  inline
304  operator signed_type () const
305  {
306  return static_cast<signed_type>(this->value);
307  }
308 
309  private:
311  composed_type value;
312  };
313 
321  inline bool
322  operator== (const Color& lhs,
323  const Color& rhs)
324  {
325  return static_cast<Color::composed_type>(lhs) == static_cast<Color::composed_type>(rhs);
326  }
327 
335  inline bool
336  operator== (const Color& lhs,
338  {
339  return static_cast<Color::composed_type>(lhs) == rhs;
340  }
341 
349  inline bool
351  const Color& rhs)
352  {
353  return lhs == static_cast<Color::composed_type>(rhs);
354  }
355 
363  inline bool
364  operator== (const Color& lhs,
365  Color::signed_type rhs)
366  {
367  return static_cast<Color::signed_type>(lhs) == rhs;
368  }
369 
377  inline bool
379  const Color& rhs)
380  {
381  return lhs == static_cast<Color::signed_type>(rhs);
382  }
383 
391  inline bool
392  operator!= (const Color& lhs,
393  const Color& rhs)
394  {
395  return static_cast<Color::composed_type>(lhs) != static_cast<Color::composed_type>(rhs);
396  }
397 
405  inline bool
406  operator!= (const Color& lhs,
408  {
409  return static_cast<Color::composed_type>(lhs) != rhs;
410  }
411 
419  inline bool
421  const Color& rhs)
422  {
423  return lhs != static_cast<Color::composed_type>(rhs);
424  }
425 
433  inline bool
434  operator!= (const Color& lhs,
435  Color::signed_type rhs)
436  {
437  return static_cast<Color::signed_type>(lhs) != rhs;
438  }
439 
447  inline bool
449  const Color& rhs)
450  {
451  return lhs != static_cast<Color::signed_type>(rhs);
452  }
453 
461  template<class charT, class traits>
462  inline std::basic_ostream<charT,traits>&
463  operator<< (std::basic_ostream<charT,traits>& os,
464  const Color& color)
465  {
466  return os << static_cast<Color::signed_type>(color);
467  }
468 
476  template<class charT, class traits>
477  inline std::basic_istream<charT,traits>&
478  operator>> (std::basic_istream<charT,traits>& is,
479  Color& color)
480  {
482  if (is >> c)
483  color.setValue(c);
484  return is;
485  }
486 
487  }
488  }
489  }
490 }
491 
492 #ifdef _MSC_VER
493 #pragma pop_macro("min")
494 #pragma pop_macro("max")
495 #endif
496 
497 #endif // OME_XML_MODEL_PRIMITIVES_COLOR_H
498 
499 /*
500  * Local Variables:
501  * mode:C++
502  * End:
503  */
uint8_t component_type
The type of an individual color component (R, G, B, A).
Definition: Color.h:87
void setAlpha(component_type alpha)
Set the alpha component of this color.
Definition: Color.h:248
bool operator!=(const Color &lhs, const Color &rhs)
Compare two Color objects for non-equality.
Definition: Color.h:392
int32_t signed_type
The type of all components composed as a single RGBA value (signed).
Definition: Color.h:91
component_type getGreen() const
Get the green component of this color.
Definition: Color.h:191
Color(signed_type value)
Construct a Color from a signed integer value.
Definition: Color.h:119
void setValue(composed_type value)
Set the integer value of this color from an unsigned integer.
Definition: Color.h:271
void setValue(signed_type value)
Set the integer value of this color from a signed integer.
Definition: Color.h:282
signed_type getValue() const
Get the signed integer value of this color.
Definition: Color.h:260
Color(composed_type value)
Construct a Color from an unsigned integer value.
Definition: Color.h:108
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:134
Open Microscopy Environment C++ implementation.
void setGreen(component_type green)
Set the green component of this color.
Definition: Color.h:202
bool operator==(const Color &lhs, const Color &rhs)
Compare two Color objects for equality.
Definition: Color.h:322
void setBlue(component_type blue)
Set the blue component of this color.
Definition: Color.h:225
uint32_t composed_type
The type of all components composed as a single RGBA value (unsigned).
Definition: Color.h:89
Color()
Construct a Color.
Definition: Color.h:97
component_type getBlue() const
Get the blue component of this color.
Definition: Color.h:214
component_type getRed() const
Get the red component of this color.
Definition: Color.h:168
std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &is, Color &color)
Set Color from input stream.
Definition: Color.h:478
An RGBA color value.
Definition: Color.h:83
component_type getAlpha() const
Get the alpha component of this color.
Definition: Color.h:237
void setRed(component_type red)
Set the red component of this color.
Definition: Color.h:179
composed_type value
The color value.
Definition: Color.h:311