summaryrefslogtreecommitdiffstats
path: root/libdimensionxx/dimensionxx/color.hpp
blob: ad28c4dda12c685d2b817ab1877b7aefdfead39f (plain)
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
/*************************************************************************
 * Copyright (C) 2009 Tavian Barnes <tavianator@gmail.com>               *
 *                                                                       *
 * This file is part of The Dimension Library.                           *
 *                                                                       *
 * The Dimension Library is free software; you can redistribute it and/  *
 * or modify it under the terms of the GNU Lesser General Public License *
 * as published by the Free Software Foundation; either version 3 of the *
 * License, or (at your option) any later version.                       *
 *                                                                       *
 * The Dimension Library is distributed in the hope that it will be      *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General Public      *
 * License along with this program.  If not, see                         *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

// Wrappers for libdimension colors.

#ifndef DIMENSIONXX_COLOR_HPP
#define DIMENSIONXX_COLOR_HPP

namespace Dimension
{
  // Forward declarations
  class CIE_XYZ;
  class CIE_xyY;
  class CIE_Lab;
  class CIE_Luv;
  class sRGB;

  // Default whitepoint (D50)
  extern const CIE_XYZ whitepoint;

  // Wrapper for dmnsn_color
  class Color
  {
  public:
    Color() { }
    inline Color(const CIE_XYZ& XYZ);
    inline Color(const CIE_xyY& xyY);
    inline Color(const CIE_Lab& Lab, const CIE_XYZ& white = whitepoint);
    inline Color(const CIE_Luv& Luv, const CIE_XYZ& white = whitepoint);
    inline Color(const sRGB& RGB);
    explicit Color(dmnsn_color c) : m_color(c) { }
    // Color(const Color& c);
    // ~Color();

    // Get and set filtered and unfiltered transparancy
    double filter() const { return m_color.filter; }
    double trans()  const { return m_color.trans; }

    void filter(double f) { m_color.filter = f; }
    void trans(double t)  { m_color.trans = t; }

    // Color& operator=(const Color& c);

    // Add a color to this one in a perceptually correct manner
    Color& operator+=(const Color& c)
      { m_color = dmnsn_color_add(m_color, c.m_color); return *this; }

    // Access the wrapped color
    dmnsn_color dmnsn() const { return m_color; }

  private:
    dmnsn_color m_color;
  };

  // Wrappers for all libdimension color types

  class CIE_XYZ
  {
  public:
    CIE_XYZ() { }
    CIE_XYZ(double X, double Y, double Z)
      { m_XYZ.X = X; m_XYZ.Y = Y; m_XYZ.Z = Z; }
    CIE_XYZ(const Color& c) : m_XYZ(dmnsn_XYZ_from_color(c.dmnsn())) { }
    explicit CIE_XYZ(dmnsn_CIE_XYZ XYZ) : m_XYZ(XYZ) { }
    // CIE_XYZ(const CIE_XYZ& XYZ);
    // ~CIE_XYZ();

    double X() const { return m_XYZ.X; }
    double Y() const { return m_XYZ.Y; }
    double Z() const { return m_XYZ.Z; }

    // CIE_XYZ& operator=(const CIE_XYZ& XYZ);
    CIE_XYZ& operator=(const Color& c)
      { m_XYZ = dmnsn_XYZ_from_color(c.dmnsn()); return *this; }

    dmnsn_CIE_XYZ dmnsn() const { return m_XYZ; }

  private:
    dmnsn_CIE_XYZ m_XYZ;
  };

  class CIE_xyY
  {
  public:
    CIE_xyY() { }
    CIE_xyY(double x, double y, double Y)
      { m_xyY.x = x; m_xyY.y = y; m_xyY.Y = Y; }
    CIE_xyY(const Color& c) : m_xyY(dmnsn_xyY_from_color(c.dmnsn())) { }
    explicit CIE_xyY(dmnsn_CIE_xyY xyY) : m_xyY(xyY) { }
    // CIE_xyY(const CIE_xyY& xyY);
    // ~CIE_xyY();

    double x() const { return m_xyY.x; }
    double y() const { return m_xyY.y; }
    double Y() const { return m_xyY.Y; }

    // CIE_xyY& operator=(const CIE_xyY& xyY);
    CIE_xyY& operator=(const Color& c)
      { m_xyY = dmnsn_xyY_from_color(c.dmnsn()); return *this; }

    dmnsn_CIE_xyY dmnsn() const { return m_xyY; }

  private:
    dmnsn_CIE_xyY m_xyY;
  };

  class CIE_Lab
  {
  public:
    CIE_Lab() { }
    CIE_Lab(double L, double a, double b)
      { m_Lab.L = L; m_Lab.a = a; m_Lab.b = b; }
    CIE_Lab(const Color& c, const CIE_XYZ& white = whitepoint)
      : m_Lab(dmnsn_Lab_from_color(c.dmnsn(), white.dmnsn())) { }
    explicit CIE_Lab(dmnsn_CIE_Lab Lab) : m_Lab(Lab) { }
    // CIE_Lab(const CIE_Lab& Lab);
    // ~CIE_Lab();

    double L() const { return m_Lab.L; }
    double a() const { return m_Lab.a; }
    double b() const { return m_Lab.b; }

    // CIE_Lab& operator=(const CIE_Lab& Lab);
    CIE_Lab& operator=(const Color& c)
      { m_Lab = dmnsn_Lab_from_color(c.dmnsn(), whitepoint.dmnsn());
        return *this; }

    dmnsn_CIE_Lab dmnsn() const { return m_Lab; }

  private:
    dmnsn_CIE_Lab m_Lab;
  };

  class CIE_Luv
  {
  public:
    CIE_Luv() { }
    CIE_Luv(double L, double u, double v)
      { m_Luv.L = L; m_Luv.u = u; m_Luv.v = v; }
    CIE_Luv(const Color& c, const CIE_XYZ& white = whitepoint)
      : m_Luv(dmnsn_Luv_from_color(c.dmnsn(), white.dmnsn())) { }
    explicit CIE_Luv(dmnsn_CIE_Luv Luv) : m_Luv(Luv) { }
    // CIE_Luv(const CIE_Luv& Luv);
    // ~CIE_Luv();

    double L() const { return m_Luv.L; }
    double u() const { return m_Luv.u; }
    double v() const { return m_Luv.v; }

    // CIE_Luv& operator=(const CIE_Luv& Luv);
    CIE_Luv& operator=(const Color& c)
      { m_Luv = dmnsn_Luv_from_color(c.dmnsn(), whitepoint.dmnsn());
        return *this; }

    dmnsn_CIE_Luv dmnsn() const { return m_Luv; }

  private:
    dmnsn_CIE_Luv m_Luv;
  };

  class sRGB
  {
  public:
    sRGB() { }
    sRGB(double R, double G, double B)
      { m_RGB.R = R; m_RGB.G = G; m_RGB.B = B; }
    sRGB(const Color& c) : m_RGB(dmnsn_sRGB_from_color(c.dmnsn())) { }
    explicit sRGB(dmnsn_sRGB RGB) : m_RGB(RGB) { }
    // sRGB(const sRGB& RGB);
    // ~sRGB();

    double R() const { return m_RGB.R; }
    double G() const { return m_RGB.G; }
    double B() const { return m_RGB.B; }

    // sRGB& operator=(const sRGB& RGB);
    sRGB& operator=(const Color& c)
      { m_RGB = dmnsn_sRGB_from_color(c.dmnsn()); return *this; }

    dmnsn_sRGB dmnsn() const { return m_RGB; }

  private:
    dmnsn_sRGB m_RGB;
  };

  // Array_Element specialization
  template <>
  class Array_Element<Color>
    : public By_Value_Array_Element<Color, dmnsn_color>
  {
  public:
    typedef dmnsn_color C_Type;

    Array_Element() { }
    Array_Element(Color& color)
      : By_Value_Array_Element<Color, dmnsn_color>(color) { }
    Array_Element(C_Type c)
      : By_Value_Array_Element<Color, dmnsn_color>(c) { }
    // Array_Element(const Array_Element& ae);
    // ~Array_Element();

    // Array_Element& operator=(const Array_Element& ae);
  };

  // Color inline constructors

  inline Color::Color(const CIE_XYZ& XYZ)
    : m_color(dmnsn_color_from_XYZ(XYZ.dmnsn())) { }

  inline Color::Color(const CIE_xyY& xyY)
    : m_color(dmnsn_color_from_xyY(xyY.dmnsn())) { }

  inline Color::Color(const CIE_Lab& Lab, const CIE_XYZ& white)
    : m_color(dmnsn_color_from_Lab(Lab.dmnsn(), white.dmnsn())) { }

  inline Color::Color(const CIE_Luv& Luv, const CIE_XYZ& white)
    : m_color(dmnsn_color_from_Luv(Luv.dmnsn(), white.dmnsn())) { }

  inline Color::Color(const sRGB& RGB)
    : m_color(dmnsn_color_from_sRGB(RGB.dmnsn())) { }

  // Color operators

  // Perceptually correct color combination
  inline Color
  operator+(const Color& lhs, const Color& rhs)
  {
    Color temp = lhs;
    temp += rhs;
    return temp;
  }

  // Perceptual color difference
  inline double
  operator-(const Color& lhs, const Color& rhs)
  {
    return dmnsn_color_difference(lhs.dmnsn(), rhs.dmnsn());
  }
}

#endif /* DIMENSIONXX_COLOR_HPP */