File: Color.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (396 lines) | stat: -rw-r--r-- 8,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 1997
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel).  All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Stream_support/include/CGAL/IO/Color.h $
// $Id: include/CGAL/IO/Color.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s)     : Andreas Fabri

#ifndef CGAL_COLOR_H
#define CGAL_COLOR_H

#include <CGAL/config.h>
#include <CGAL/array.h>

#include <boost/functional/hash.hpp>

#include <algorithm>
#include <cstdlib>
#include <cmath>

namespace CGAL {

namespace IO {

/*!
  \ingroup PkgStreamSupportRef

  An object of the class `Color` is a color available for drawing
  operations in many \cgal output streams.  Each color is defined by a
  4 unsigned chars `(r,g,b,a)` with 0 \f$\le\f$ r,g,b,a \f$ \le \f$
  255, the so-called <I>rgba-value</I> of the color.

  The alpha parameter (representing transparency) is often ignored and
  left to its default value (255 = no transparency), which is why we
  often refer to the <I>rgb-value</I> of the color.

*/

class Color
{
private:

  std::array<unsigned char, 4> m_data;

public:

  /// \name Creation
  /// @{

  /*!
    creates a color with rgba-value `(0,0,0,255)`, i.e.\ black.
  */
  Color()
  {
    set_rgb (0, 0, 0, 255);
  }

  /*!
    creates a color with rgba-value `(red,green,blue,alpha)`.
  */
  Color(unsigned char red,
        unsigned char green,
        unsigned char blue,
        unsigned char alpha = 255)
  {
    set_rgb (red, green, blue, alpha);
  }

  /// @}

  /// \name Component Access
  /// @{

  /*!
    returns the red component.
  */
  unsigned char red() const { return m_data[0]; }

  /*!
    returns a reference on the red component.
  */
  unsigned char& red() { return m_data[0]; }

  /*!
    returns the green component.
  */
  unsigned char green() const { return m_data[1]; }

  /*!
    returns a reference on the green component.
  */
  unsigned char& green() { return m_data[1]; }

  /*!
    returns the blue component.
  */
  unsigned char blue() const { return m_data[2]; }

  /*!
    returns a reference on the blue component.
  */
  unsigned char& blue() { return m_data[2]; }

  /*!
    returns the alpha component.
  */
  unsigned char alpha() const { return m_data[3]; }

  /*!
    returns a reference on the alpha component.
  */
  unsigned char& alpha() { return m_data[3]; }

  /// \cond SKIP_IN_MANUAL

  bool operator==(const Color &c) const
  {
    return ( (red() == c.red()) &&
             (green() == c.green()) &&
             (blue() == c.blue()) );
  }

  bool operator!=(const Color &c) const
  {
    return !( (*this) == c);
  }

  bool operator<(const Color& c) const
  {
      return m_data < c.to_rgba();
  }

  unsigned char r() const { return red(); }
  unsigned char g() const { return green(); }
  unsigned char b() const { return blue(); }
  unsigned char a() const { return alpha(); }
  /// \endcond

  /// @}

  /// \name Array Access
  /// @{

  /*!
    returns the \f$i^{th}\f$ component of the rgb color (the
    \f$0^{th}\f$ is red, the \f$1^{st}\f$ is green, the \f$2^{nd}\f$ is blue and the \f$3^{rd}\f$ is alpha).
  */
  unsigned char operator[] (std::size_t i) const { return m_data[i]; }

  /*!
    returns a reference on the \f$i^{th}\f$ component of the rgb color (the
    \f$0^{th}\f$ is red, the \f$1^{st}\f$ is green, the \f$2^{nd}\f$ is blue and the \f$3^{rd}\f$ is alpha).
  */
  unsigned char& operator[] (std::size_t i)  { return m_data[i]; }

  /*!
    returns the array with rgba values.
  */
  const std::array<unsigned char, 4>& to_rgba() const { return m_data; }

  /*!
    returns the array with rgb values.
  */
  const std::array<unsigned char, 3>& to_rgb() const
  {
    return reinterpret_cast<const std::array<unsigned char, 3>&>(m_data);
  }

  /*!
    computes the hsv (hue, saturation, value) values and returns an
    array representing them as float values between 0 and 1.
  */
  std::array<double, 3> to_hsv() const
  {
    double r = (double)(m_data[0]) / 255.;
    double g = (double)(m_data[1]) / 255.;
    double b = (double)(m_data[2]) / 255.;
    double Cmax = (std::max) (r, (std::max) (g, b));
    double Cmin = (std::min) (r, (std::min) (g, b));
    double delta = Cmax - Cmin;
    double H = 0.;

    if (delta != 0.)
    {
      if (Cmax == r)
        H = 60. * ((g - b) / delta);
      else if (Cmax == g)
        H = 60. * (((b - r) / delta) + 2.);
      else
        H = 60. * (((r - g) / delta) + 4.);
    }

    if (H < 0.) H += 360.;

    double S = (Cmax == 0. ? 0. : 100. * (delta / Cmax));
    double V = 100. * Cmax;

    return make_array(H,S,V);
  }
  /// @}
  /// \name Modification
  /// @{

  /*!
    replaces the rgb values of the colors by the one given as parameters.
  */
  Color& set_rgb (unsigned char red,
                unsigned char green,
                unsigned char blue,
                unsigned char alpha = 255)
  {
    m_data[0] = red;
    m_data[1] = green;
    m_data[2] = blue;
    m_data[3] = alpha;

    return *this;
  }

  /*!
    replaces the rgb values of the colors by the conversion to rgb of
    the hsv values given as parameters.
  */
  Color& set_hsv (double hue,
                double saturation,
                double value,
                unsigned char alpha = 255)
  {
    saturation /= 100.;
    value /= 100.;
    double C = value*saturation;
    double hh = (hue/60.);
    double X = C * (1-std::abs(std::fmod(hh, 2) - 1));
    double r = 0, g = 0, b = 0;

    if( hh>=0 && hh<1 )
    {
      r = C;
      g = X;
    }
    else if( hh>=1 && hh<2 )
    {
      r = X;
      g = C;
    }
    else if( hh>=2 && hh<3 )
    {
      g = C;
      b = X;
    }
    else if( hh>=3 && hh<4 )
    {
      g = X;
      b = C;
    }
    else if( hh>=4 && hh<5 )
    {
      r = X;
      b = C;
    }
    else
    {
      r = C;
      b = X;
    }
    double m = value-C;
    r += m;
    g += m;
    b += m;
    r *= 255.0;
    g *= 255.0;
    b *= 255.0;

    m_data[0] = (unsigned char)r;
    m_data[1] = (unsigned char)g;
    m_data[2] = (unsigned char)b;
    m_data[3] = alpha;

    return *this;
  }

  /// @}
};


/*!

  Constructs Color(0,0,0).
  \relates Color
*/
inline Color black() { return Color(0,0,0); }

/*!
  Constructs Color(0,0,255).
  \relates Color
*/
inline Color blue() { return Color(0,0,255); }

/*!
  Constructs Color(10,0,100).
  \relates Color
*/
inline Color deep_blue() { return Color(10,0,100); }

/*!
  Constructs Color(100,100,100).
  \relates Color
*/
inline Color gray() { return Color(100,100,100); }

/*!
  Constructs Color(0,255,0).
  \relates Color
*/
inline Color green() { return Color(0,255,0); }

/*!
  Constructs Color(235,150,0).
  \relates Color
*/
inline Color orange() { return Color(235,150,0); }

/*!
  Constructs Color(100,0,70).
  \relates Color
*/
inline Color purple() { return Color(100,0,70); }

/*!
  Constructs Color(255,0,0).
  \relates Color
*/
inline Color red() { return Color(255,0,0); }

/*!
  Constructs Color(255,0,255).
  \relates Color
*/
inline Color violet() { return Color(255,0,255); }

/*!
  Constructs Color(255,255,255).
  \relates Color
*/
inline Color white() { return Color(255,255,255); }

/*!
  Constructs Color(255,255,0).
  \relates Color
*/
inline Color yellow() { return Color(255,255,0); }

} //namespace IO

#ifndef CGAL_NO_DEPRECATED_CODE
using IO::Color;
using IO::black;
using IO::blue;
using IO::deep_blue;
using IO::gray;
using IO::green;
using IO::orange;
using IO::purple;
using IO::red;
using IO::violet;
using IO::white;
using IO::yellow;
#endif

} // namespace CGAL

namespace std {

template <>
struct hash<CGAL::IO::Color>
{
  std::size_t operator()(const CGAL::IO::Color& c) const
  {
    std::size_t result = boost::hash_value(c[0]);
    for(std::size_t i=1; i<4; ++i)
      boost::hash_combine(result, c[i]);
    return result;
  }
};

} // namespace std

#endif  // CGAL_COLOR_H