summaryrefslogtreecommitdiff
path: root/graphics/epix/Color.cc
blob: 1b4d01b71785f2a3c946792850e35e1b6f641475 (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
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
/*
 * Color.cc -- ePiX Color functions
 *
 * This file is part of ePiX, a C++ library for creating high-quality
 * figures in LaTeX
 *
 * Version 1.1.17
 *
 * Last Change: September 13, 2007
 */

/*
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
 * Andrew D. Hwang <rot 13 nujnat at zngupf dot ubylpebff dot rqh>
 * Department of Mathematics and Computer Science
 * College of the Holy Cross
 * Worcester, MA, 01610-2395, USA
 *
 */
 
/*
 * ePiX is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ePiX 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 General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ePiX; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <sstream>
#include <string>
#include <vector>

#include <cmath>

#include "functions.h"

#include "Color_Base.h"
#include "Color_Utils.h"

#include "Color_CMY.h"
#include "Color_CMYK.h"
#include "Color_Gray.h"
#include "Color_Neutral.h"
#include "Color_RGB.h"
#include "Color_Sep.h"

#include "Color.h"

namespace ePiX {

  Color::Color() 
    : m_color(Neutral().m_color), m_alpha(1) { }

  Color::Color(const Color_Base& col)
    : m_color(col.clone()), m_alpha(1) { }

  Color::Color(const Color_Base* col)
    : m_color(col->clone()), m_alpha(1) { }


  Color::Color(const Color& col)
    : m_color(col.m_color->clone()), m_alpha(col.m_alpha) { }


  Color& Color::operator= (const Color& col)
  {
    if (*this != col)
      {
	Color_Base* tmp(col.m_color->clone());
	delete m_color;

	m_color = tmp;
	m_alpha = col.m_alpha;
      }

    return *this;
  }

  Color::~Color()
  {
    delete m_color;
  }

  const Color_Base* Color::operator->() const
  {
    return m_color;
  }

  double Color::alpha() const
  {
    return m_alpha;
  }


  bool Color::operator== (const Color& col) const
  {
    return m_color->name() == col.m_color->name();
  }

  bool Color::operator< (const Color& col) const
  {
    // N.B. add_colors_to() must check for Unset
    return m_color->name() < col.m_color->name(); 
  }

  bool Color::is_unset() const
  {
    // we're Neutral()
    return (name() == "");
  }


  // no effect if we or col are Neutral
  Color Color::filter(const Color& col) const
  {
    if (m_color->name() == "" || col.is_unset())
      return col;

    else
      {
	Color_Base* fil(m_color->clone());
	fil->filter(*(col.m_color));

	Color val(fil);
	delete fil;
	return val;
      }
  }

  Color& Color::operator*= (double c)
  {
    (*m_color) *= c;

    return *this;
  }

  // Blending with Unset has no effect
  Color& Color::blend(const Color& col, double d)
  {
    if (!col.is_unset())
      m_color->blend(*(col.m_color), d);

    return *this;
  }

  Color& Color::superpose(const Color& col)
  {
    if (!col.is_unset())
      m_color->superpose(*(col.m_color));

    return *this;
  }

  Color& Color::invert()
  {
    m_color->invert();

    return *this;
  }

  Color& Color::alpha(double t)
  {
    m_alpha = t;
    return *this;
  }


  std::string Color::model() const
  {
    return m_color->model();
  }

  std::string Color::name() const
  {
    return m_color->name();
  }

  std::vector<double> Color::densities() const
  {
    return m_color->densities();
  }


  // Named constructors
  Color RGB(double r, double g, double b)
  {
    return Color(RGB_Color(r, g, b));
  }

  Color CMY(double c, double m, double y)
  {
    return Color(CMY_Color(c, m, y));
  }

  Color CMYK(double c, double m, double y, double k)
  {
    return Color(CMYK_Color(c, m, y, k));
  }

  Color Gray(double d)
  {
    return Color(Gray_Color(d));
  }


  // Scale operator
  Color operator* (double c, const Color& col)
  {
    Color tmp(col);
    return tmp *= c;
  }


  /* * * Extended primaries in the global namespace * * */
  Color Red(double d)
  {
    return RGB(clippy(d), clippy(-d), clippy(-d));
  }

  Color Green(double d)
  {
    return RGB(clippy(-d), clippy(d), clippy(-d));
  }

  Color Blue(double d)
  {
    return RGB(clippy(-d), clippy(-d), clippy(d));
  }

  Color White(double d)   
  {
    return RGB(clippy(d), clippy(d), clippy(d)); 
  }

  Color Black(double d)
  { 
    return RGB(clippy(1-d), clippy(1-d), clippy(1-d));
  }


  /* * * CMY * * */
  Color Cyan(double d)
  { 
    return CMY_Color(clippy(d), clippy(-d), clippy(-d), true);
  }

  Color Magenta(double d)
  { 
    return CMY_Color(clippy(-d), clippy(d), clippy(-d), true);
  }

  Color Yellow(double d)
  { 
    return CMY_Color(clippy(-d), clippy(-d), clippy(d), true);
  }

  Color CMY_White(double d)
  {
    return CMY_Color(clippy(1-d), clippy(1-d), clippy(1-d), true);
  }

  Color CMY_Black(double d)
  {
    return CMY_Color(clippy(d), clippy(d), clippy(d), true);
  }


  /* * * CMYK * * */
  Color CyanK(double d)
  {
    double c(clippy(d));
    double m(clippy(-d));
    double y(m);        // [sic]
    double k(min(c,m)); // y=m

    return CMYK_Color(c-k, m-k, y-k, k, true);
  }

  Color MagentaK(double d)
  { 
    double c(clippy(-d));
    double m(clippy(d));
    double y(c);
    double k(min(c,m)); // y=c

    return CMYK_Color(c-k, m-k, y-k, k, true);
  }

  Color YellowK(double d)
  { 
    double c(clippy(-d));
    double m(c);
    double y(clippy(d));
    double k(min(c,y)); // m=c

    return CMYK_Color(c-k, m-k, y-k, k, true);
  }

  Color CMYK_White(double d)   
  {
    return CMYK_Color(0, 0, 0, clippy(1-d), true);
  }

  Color CMYK_Black(double d)
  {
    return CMYK_Color(0, 0, 0, clippy(d), true);
  }


  Color C_Process(double d)
  {
    return Cyan_Layer(d);
  }

  Color M_Process(double d)
  {
    return Magenta_Layer(d);
  }

  Color Y_Process(double d)
  {
    return Yellow_Layer(d);
  }

  Color K_Process(double d)
  {
    return Black_Layer(d);
  }


  /* * * Neutral filters * * */
  Color Neutral()
  {
    static Neutral_Color* neutral(new Neutral_Color());
    return Color(*neutral);
  }

  Color RGB_Neutral()
  {
    static RGB_Color* rgb_neutral(new RGB_Color(1, 1, 1));
    return Color(*rgb_neutral);
  }

  Color CMY_Neutral()
  {
    static CMY_Color* cmy_neutral(new CMY_Color(0, 0, 0, true));
    return Color(*cmy_neutral);
  }

  Color CMYK_Neutral()
  {
    static CMYK_Color* cmyk_neutral(new CMYK_Color(0, 0, 0, 0, true));
    return Color(*cmyk_neutral);
  }

  Color Gray_Neutral()
  {
    static Gray_Color* gray_neutral(new Gray_Color(1));
    return Color(*gray_neutral);
  }
} // end of namespace