ome-xml  5.2.2
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 <limits>
43 #include <string>
44 
45 #include <ome/compat/cstdint.h>
46 
47 #ifdef _MSC_VER
48 #pragma push_macro("min")
49 #undef min
50 #pragma push_macro("max")
51 #undef max
52 #endif
53 
54 namespace ome
55 {
56  namespace xml
57  {
58  namespace model
59  {
66  namespace primitives
67  {
68 
84  class Color
85  {
86  public:
88  typedef uint8_t component_type;
90  typedef uint32_t composed_type;
92  typedef int32_t signed_type;
93 
97  inline
98  Color ():
99  value(0x000000FFU)
100  {
101  }
102 
108  inline
109  Color (composed_type value):
110  value(value)
111  {
112  }
113 
119  inline
120  Color (signed_type value):
121  value(static_cast<composed_type>(value))
122  {
123  }
124 
134  inline
135  Color (component_type r,
136  component_type g,
137  component_type b,
138  component_type a = std::numeric_limits<component_type>::max()):
139  value(static_cast<composed_type>(r) << 24U |
140  static_cast<composed_type>(g) << 16U |
141  static_cast<composed_type>(b) << 8U |
142  static_cast<composed_type>(a) << 0U)
143  {
144  }
145 
155  Color (const std::string& str,
156  bool sign = true);
157 
158  inline
159  ~Color ()
160  {
161  }
162 
168  inline component_type
169  getRed () const
170  {
171  return static_cast<component_type>((this->value >> 24U) & 0xffU);
172  }
173 
179  inline void
180  setRed (component_type red)
181  {
182  this->value &= ~(0xFFU << 24U);
183  this->value |= static_cast<composed_type>(red) << 24U;
184  }
185 
191  inline component_type
192  getGreen () const
193  {
194  return static_cast<component_type>((this->value >> 16U) & 0xffU);
195  }
196 
202  inline void
203  setGreen (component_type green)
204  {
205  this->value &= ~(0xffU << 16U);
206  this->value |= static_cast<composed_type>(green) << 16U;
207  }
208 
214  inline component_type
215  getBlue () const
216  {
217  return static_cast<component_type>((this->value >> 8U) & 0xffU);
218  }
219 
225  inline void
226  setBlue (component_type blue)
227  {
228  this->value &= ~(0xffU << 8U);
229  this->value |= static_cast<composed_type>(blue) << 8U;
230  }
231 
237  inline component_type
238  getAlpha () const
239  {
240  return static_cast<component_type>((this->value >> 0) & 0xffU);
241  }
242 
248  inline void
249  setAlpha (component_type alpha)
250  {
251  this->value &= ~(0xffU << 0);
252  this->value |= static_cast<composed_type>(alpha) << 0;
253  }
254 
260  inline signed_type
261  getValue () const
262  {
263  return static_cast<signed_type>(this->value);
264  }
265 
271  inline void
272  setValue (composed_type value)
273  {
274  this->value = value;
275  }
276 
282  inline void
283  setValue (signed_type value)
284  {
285  this->value = static_cast<composed_type>(value);
286  }
287 
293  inline
294  operator composed_type () const
295  {
296  return this->value;
297  }
298 
304  inline
305  operator signed_type () const
306  {
307  return static_cast<signed_type>(this->value);
308  }
309 
310  private:
312  composed_type value;
313  };
314 
322  inline bool
323  operator== (const Color& lhs,
324  const Color& rhs)
325  {
326  return static_cast<Color::composed_type>(lhs) == static_cast<Color::composed_type>(rhs);
327  }
328 
336  inline bool
337  operator== (const Color& lhs,
339  {
340  return static_cast<Color::composed_type>(lhs) == rhs;
341  }
342 
350  inline bool
352  const Color& rhs)
353  {
354  return lhs == static_cast<Color::composed_type>(rhs);
355  }
356 
364  inline bool
365  operator== (const Color& lhs,
366  Color::signed_type rhs)
367  {
368  return static_cast<Color::signed_type>(lhs) == rhs;
369  }
370 
378  inline bool
380  const Color& rhs)
381  {
382  return lhs == static_cast<Color::signed_type>(rhs);
383  }
384 
392  inline bool
393  operator!= (const Color& lhs,
394  const Color& rhs)
395  {
396  return static_cast<Color::composed_type>(lhs) != static_cast<Color::composed_type>(rhs);
397  }
398 
406  inline bool
407  operator!= (const Color& lhs,
409  {
410  return static_cast<Color::composed_type>(lhs) != rhs;
411  }
412 
420  inline bool
422  const Color& rhs)
423  {
424  return lhs != static_cast<Color::composed_type>(rhs);
425  }
426 
434  inline bool
435  operator!= (const Color& lhs,
436  Color::signed_type rhs)
437  {
438  return static_cast<Color::signed_type>(lhs) != rhs;
439  }
440 
448  inline bool
450  const Color& rhs)
451  {
452  return lhs != static_cast<Color::signed_type>(rhs);
453  }
454 
462  template<class charT, class traits>
463  inline std::basic_ostream<charT,traits>&
464  operator<< (std::basic_ostream<charT,traits>& os,
465  const Color& color)
466  {
467  return os << static_cast<Color::signed_type>(color);
468  }
469 
477  template<class charT, class traits>
478  inline std::basic_istream<charT,traits>&
479  operator>> (std::basic_istream<charT,traits>& is,
480  Color& color)
481  {
483  if (is >> c)
484  color.setValue(c);
485  return is;
486  }
487 
488  }
489  }
490  }
491 }
492 
493 #ifdef _MSC_VER
494 #pragma pop_macro("min")
495 #pragma pop_macro("max")
496 #endif
497 
498 #endif // OME_XML_MODEL_PRIMITIVES_COLOR_H
499 
500 /*
501  * Local Variables:
502  * mode:C++
503  * End:
504  */
uint8_t component_type
The type of an individual color component (R, G, B, A).
Definition: Color.h:88
void setAlpha(component_type alpha)
Set the alpha component of this color.
Definition: Color.h:249
bool operator!=(const Color &lhs, const Color &rhs)
Compare two Color objects for non-equality.
Definition: Color.h:393
int32_t signed_type
The type of all components composed as a single RGBA value (signed).
Definition: Color.h:92
Color(signed_type value)
Construct a Color from a signed integer value.
Definition: Color.h:120
void setValue(composed_type value)
Set the integer value of this color from an unsigned integer.
Definition: Color.h:272
component_type getGreen() const
Get the green component of this color.
Definition: Color.h:192
void setValue(signed_type value)
Set the integer value of this color from a signed integer.
Definition: Color.h:283
Color(composed_type value)
Construct a Color from an unsigned integer value.
Definition: Color.h:109
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:135
signed_type getValue() const
Get the signed integer value of this color.
Definition: Color.h:261
Open Microscopy Environment C++ implementation.
void setGreen(component_type green)
Set the green component of this color.
Definition: Color.h:203
bool operator==(const Color &lhs, const Color &rhs)
Compare two Color objects for equality.
Definition: Color.h:323
void setBlue(component_type blue)
Set the blue component of this color.
Definition: Color.h:226
uint32_t composed_type
The type of all components composed as a single RGBA value (unsigned).
Definition: Color.h:90
Color()
Construct a Color.
Definition: Color.h:98
component_type getBlue() const
Get the blue component of this color.
Definition: Color.h:215
std::basic_istream< charT, traits > & operator>>(std::basic_istream< charT, traits > &is, Color &color)
Set Color from input stream.
Definition: Color.h:479
component_type getAlpha() const
Get the alpha component of this color.
Definition: Color.h:238
An RGBA color value.
Definition: Color.h:84
component_type getRed() const
Get the red component of this color.
Definition: Color.h:169
void setRed(component_type red)
Set the red component of this color.
Definition: Color.h:180
composed_type value
The color value.
Definition: Color.h:312