opennurbs_color.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 #if !defined(OPENNURBS_COLOR_INC_)
18 #define OPENNURBS_COLOR_INC_
19 
20 ///////////////////////////////////////////////////////////////////////////////
21 //
22 // Class ON_Color
23 //
24 class ON_CLASS ON_Color
25 {
26 public:
27  ON_Color() = default;
28  ~ON_Color() = default;
29  ON_Color(const ON_Color&) = default;
30  ON_Color& operator=(const ON_Color&) = default;
31 
32  static const ON_Color UnsetColor; // 0xFFFFFFFFu
33  static const ON_Color Black; // 0x00000000u
34  static const ON_Color White; // 0x00FFFFFFu on little endan, 0xFFFFFF00u on big endian
35  static const ON_Color SaturatedRed; // 0x000000FFu on little endan, 0xFF000000u on big endian
36  static const ON_Color SaturatedGreen; // 0x0000FF00u on little endan, 0x00FF0000u on big endian
37  static const ON_Color SaturatedBlue; // 0x00FF0000u on little endan, 0x0000FF00u on big endian
38  static const ON_Color SaturatedYellow; // 0x0000FFFFu on little endan, 0xFFFF0000u on big endian
39  static const ON_Color SaturatedCyan; // 0x00FFFF00u on little endan, 0x00FFFF00u on big endian
40  static const ON_Color SaturatedMagenta; // 0x00FF00FFu on little endan, 0xFF00FF00u on big endian
41  static const ON_Color Gray126; // R = G = B = 128 (medium)
42  static const ON_Color Gray160; // R = G = B = 160 (medium light)
43  static const ON_Color Gray230; // R = G = B = 230 (light)
44 
45  // If you need to use byte indexing to convert RGBA components to and from
46  // an unsigned int ON_Color value and want your code to work on both little
47  // and big endian computers, then use the RGBA_byte_index enum.
48  //
49  // unsigned int u;
50  // unsigned char* rgba = &y;
51  // rbga[ON_Color::kRedByteIndex] = red value 0 to 255.
52  // rbga[ON_Color::kGreenByteIndex] = green value 0 to 255.
53  // rbga[ON_Color::kBlueByteIndex] = blue value 0 to 255.
54  // rbga[ON_Color::kAlphaByteIndex] = alpha value 0 to 255.
55  // ON_Color color = u;
56  enum RGBA_byte_index : unsigned int
57  {
58  // same for both little and big endian computers.
59  kRedByteIndex = 0,
60  kGreenByteIndex = 1,
61  kBlueByteIndex = 2,
62  kAlphaByteIndex = 3
63  };
64 
65  // If you need to use shifting to convert RGBA components to and from
66  // an unsigned int ON_COlor value and you want your code to work
67  // on both little and big endian computers, use the RGBA_shift enum.
68  //
69  // unsigned int u = 0;
70  // u |= ((((unsigned int)red) & 0xFFU) << ON_Color::RGBA_shift::kRedShift);
71  // u |= ((((unsigned int)green) & 0xFFU) << ON_Color::RGBA_shift::kGreenShift);
72  // u |= ((((unsigned int)blue) & 0xFFU) << ON_Color::RGBA_shift::kBlueShift);
73  // u |= ((((unsigned int)alpha) & 0xFFU) << ON_Color::RGBA_shift::kAlphaShift);
74  // ON_Color color = u;
75  enum RGBA_shift : unsigned int
76  {
77 #if defined(ON_LITTLE_ENDIAN)
78  kRedShift = 0,
79  kGreenShift = 8,
80  kBlueShift = 16,
81  kAlphaShift = 24
82 #elif defined(ON_BIG_ENDIAN)
83  kRedShift = 24,
84  kGreenShift = 16,
85  kBlueShift = 8,
86  kAlphaShift = 0
87 #else
88 #error unknown endian
89 #endif
90  };
91 
92  // Sets A = 0
93  ON_Color(
94  int red, // ( 0 to 255 )
95  int green, // ( 0 to 255 )
96  int blue // ( 0 to 255 )
97  );
98 
99  ON_Color(
100  int red, // ( 0 to 255 )
101  int green, // ( 0 to 255 )
102  int blue, // ( 0 to 255 )
103  int alpha // ( 0 to 255 ) (0 = opaque, 255 = transparent)
104  );
105 
106  /*
107  Parameters:
108  colorref - [in]
109  Windows COLORREF in little endian RGBA order.
110  */
111  ON_Color(
112  unsigned int colorref
113  );
114 
115  // Conversion to Windows COLORREF in little endian RGBA order.
116  operator unsigned int() const;
117 
118  /*
119  Description:
120  Call this function when the color is needed in a
121  Windows COLORREF format with alpha = 0;
122  Returns
123  A Windows COLOREF with alpha = 0.
124  */
125  unsigned int WindowsRGB() const;
126 
127  // < 0 if this < arg, 0 ir this==arg, > 0 if this > arg
128  int Compare( const ON_Color& ) const;
129 
130  int Red() const; // ( 0 to 255 )
131  int Green() const; // ( 0 to 255 )
132  int Blue() const; // ( 0 to 255 )
133  int Alpha() const; // ( 0 to 255 ) (0 = opaque, 255 = transparent)
134 
135  double FractionRed() const; // ( 0.0 to 1.0 )
136  double FractionGreen() const; // ( 0.0 to 1.0 )
137  double FractionBlue() const; // ( 0.0 to 1.0 )
138  double FractionAlpha() const; // ( 0.0 to 1.0 ) (0.0 = opaque, 1.0 = transparent)
139 
140  void SetRGB(
141  int red, // red in range 0 to 255
142  int green, // green in range 0 to 255
143  int blue // blue in range 0 to 255
144  );
145 
146  void SetFractionalRGB(
147  double red, // red in range 0.0 to 1.0
148  double green, // green in range 0.0 to 1.0
149  double blue // blue in range 0.0 to 1.0
150  );
151 
152  void SetAlpha(
153  int alpha // alpha in range 0 to 255 (0 = opaque, 255 = transparent)
154  );
155 
156  void SetFractionalAlpha(
157  double alpha // alpha in range 0.0 to 1.0 (0.0 = opaque, 1.0 = transparent)
158  );
159 
160  void SetRGBA(
161  int red, // red in range 0 to 255
162  int green, // green in range 0 to 255
163  int blue, // blue in range 0 to 255
164  int alpha // alpha in range 0 to 255 (0 = opaque, 255 = transparent)
165  );
166 
167  // input args
168  void SetFractionalRGBA(
169  double red, // red in range 0.0 to 1.0
170  double green, // green in range 0.0 to 1.0
171  double blue, // blue in range 0.0 to 1.0
172  double alpha // alpha in range 0.0 to 1.0 (0.0 = opaque, 1.0 = transparent)
173  );
174 
175  // Hue() returns an angle in the range 0 to 2*pi
176  //
177  // 0 = red, pi/3 = yellow, 2*pi/3 = green,
178  // pi = cyan, 4*pi/3 = blue,5*pi/3 = magenta,
179  // 2*pi = red
180  double Hue() const;
181 
182  // Returns 0.0 (gray) to 1.0 (saturated)
183  double Saturation() const;
184 
185  // Returns 0.0 (black) to 1.0 (white)
186  double Value() const;
187 
188  void SetHSV(
189  double h, // hue in radians 0 to 2*pi
190  double s, // satuation 0.0 = gray, 1.0 = saturated
191  double v // value
192  );
193 
194 private:
195  union {
196  // On little endian (Intel) computers, m_color has the same byte order
197  // as Windows COLORREF values.
198  // On little endian computers, m_color = 0xaabbggrr as an unsigned int value.
199  // On big endian computers, m_color = 0xrrggbbaa as an unsigned int value
200  // rr = red component 0-255
201  // gg = grean component 0-255
202  // bb = blue component 0-255
203  // aa = alpha 0-255. 0 means opaque, 255 means transparent.
204  unsigned int m_color = 0;
205 
206  // m_colorComponent is a 4 unsigned byte array in RGBA order
207  // red component = m_RGBA[ON_Color::RGBA_byte::kRed]
208  // grean component = m_RGBA[ON_Color::RGBA_byte::kGreen]
209  // blue component = m_RGBA[ON_Color::RGBA_byte::kBlue]
210  // alpha component = m_RGBA[ON_Color::RGBA_byte::kAlpha]
211  unsigned char m_RGBA[4];
212  };
213 };
214 
215 #endif
RGBA_byte_index
Definition: opennurbs_color.h:56
static const ON_Color SaturatedGreen
Definition: opennurbs_color.h:36
static const ON_Color SaturatedRed
Definition: opennurbs_color.h:35
static const ON_Color UnsetColor
Definition: opennurbs_color.h:32
Definition: opennurbs_color.h:24
RGBA_shift
Definition: opennurbs_color.h:75
static const ON_Color Gray126
Definition: opennurbs_color.h:41
static const ON_Color Gray230
Definition: opennurbs_color.h:43
static const ON_Color SaturatedMagenta
Definition: opennurbs_color.h:40
static const ON_Color SaturatedBlue
Definition: opennurbs_color.h:37
static const ON_Color Gray160
Definition: opennurbs_color.h:42
static const ON_Color White
Definition: opennurbs_color.h:34
static const ON_Color SaturatedYellow
Definition: opennurbs_color.h:38
static const ON_Color SaturatedCyan
Definition: opennurbs_color.h:39
static const ON_Color Black
Definition: opennurbs_color.h:33