Magick++  6.9.3
Color.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
4 // Copyright Dirk Lemstra 2014-2015
5 //
6 // Color Implementation
7 //
8 
9 #define MAGICKCORE_IMPLEMENTATION
10 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 
12 #include "Magick++/Include.h"
13 #include <string>
14 
15 using namespace std;
16 
17 #include "Magick++/Color.h"
18 #include "Magick++/Exception.h"
19 
21  const Magick::Color &right_)
22 {
23  return((left_.isValid() == right_.isValid()) &&
24  (left_.redQuantum() == right_.redQuantum()) &&
25  (left_.greenQuantum() == right_.greenQuantum()) &&
26  (left_.blueQuantum() == right_.blueQuantum()));
27 }
28 
30  const Magick::Color &right_)
31 {
32  return(!(left_ == right_));
33 }
34 
36  const Magick::Color &right_)
37 {
38  return(!(left_ < right_ ) && (left_ != right_ ));
39 }
40 
42  const Magick::Color &right_)
43 {
44  if(left_.redQuantum() < right_.redQuantum())
45  return(true);
46  if(left_.redQuantum() > right_.redQuantum())
47  return(false);
48  if(left_.greenQuantum() < right_.greenQuantum())
49  return(true);
50  if(left_.greenQuantum() > right_.greenQuantum())
51  return(false);
52  if(left_.blueQuantum() < right_.blueQuantum())
53  return(true);
54  return(false);
55 }
56 
58  const Magick::Color &right_)
59 {
60  return((left_ > right_) || (left_ == right_));
61 }
62 
64  const Magick::Color &right_)
65 {
66  return((left_ < right_) || (left_ == right_));
67 }
68 
70  : _pixel(new PixelPacket),
71  _isValid(false),
72  _pixelOwn(true),
73  _pixelType(RGBPixel)
74 {
75  initPixel();
76 }
77 
78 Magick::Color::Color(Quantum red_,Quantum green_,Quantum blue_)
79  : _pixel(new PixelPacket),
80  _isValid(true),
81  _pixelOwn(true),
82  _pixelType(RGBPixel)
83 {
84  redQuantum(red_);
85  greenQuantum(green_);
86  blueQuantum(blue_);
87  alphaQuantum(OpaqueOpacity);
88 }
89 
90 Magick::Color::Color(Quantum red_,Quantum green_,Quantum blue_,Quantum alpha_)
91  : _pixel(new PixelPacket),
92  _isValid(true),
93  _pixelOwn(true),
94  _pixelType(RGBAPixel)
95 {
96  redQuantum(red_);
97  greenQuantum(green_);
98  blueQuantum(blue_);
99  alphaQuantum(alpha_);
100 }
101 
102 Magick::Color::Color(const char *x11color_)
103  : _pixel(new PixelPacket),
104  _isValid(true),
105  _pixelOwn(true),
106  _pixelType(RGBPixel)
107 {
108  initPixel();
109 
110  // Use operator = implementation
111  *this=x11color_;
112 }
113 
115  : _pixel(new PixelPacket),
116  _isValid(color_._isValid),
117  _pixelOwn(true),
118  _pixelType(color_._pixelType)
119 {
120  *_pixel=*color_._pixel;
121 }
122 
123 Magick::Color::Color(const PixelPacket &color_)
124  : _pixel(new PixelPacket),
125  _isValid(true),
126  _pixelOwn(true),
127  _pixelType(RGBPixel)
128 {
129  *_pixel=color_;
130 
131  if (color_.opacity != OpaqueOpacity)
132  _pixelType=RGBAPixel;
133 }
134 
135 Magick::Color::Color(const std::string &x11color_)
136  : _pixel(new PixelPacket),
137  _isValid(true),
138  _pixelOwn(true),
139  _pixelType(RGBPixel)
140 {
141  initPixel();
142 
143  // Use operator = implementation
144  *this=x11color_;
145 }
146 
148 {
149  if (_pixelOwn)
150  delete _pixel;
151 
152  _pixel=(PixelPacket *)NULL;
153 }
154 
155 const Magick::Color& Magick::Color::operator=(const char *x11color_)
156 {
157  *this=std::string(x11color_);
158  return(*this);
159 }
160 
162 {
163  // If not being set to ourself
164  if (this != &color_)
165  {
166  // Copy pixel value
167  *_pixel=*color_._pixel;
168 
169  // Validity
170  _isValid=color_._isValid;
171 
172  // Copy pixel type
173  _pixelType=color_._pixelType;
174  }
175  return(*this);
176 }
177 
178 const Magick::Color& Magick::Color::operator=
179  (const MagickCore::PixelPacket &color_)
180 {
181  *_pixel=color_;
182  if (color_.opacity != OpaqueOpacity)
183  _pixelType=RGBAPixel;
184  else
185  _pixelType=RGBPixel;
186 
187  return(*this);
188 }
189 
190 // Set color via X11 color specification string
191 const Magick::Color& Magick::Color::operator=(const std::string &x11color_)
192 {
193  PixelPacket
194  target_color;
195 
196  initPixel();
198  if (QueryColorDatabase(x11color_.c_str(),&target_color,exceptionInfo))
199  {
200  redQuantum( target_color.red );
201  greenQuantum( target_color.green );
202  blueQuantum( target_color.blue );
203  alphaQuantum( target_color.opacity );
204 
205  if (target_color.opacity > OpaqueOpacity)
206  _pixelType=RGBAPixel;
207  else
208  _pixelType=RGBPixel;
209  }
210  else
211  _isValid=false;
212  ThrowPPException(false);
213 
214  return(*this);
215 }
216 
217 Magick::Color::operator std::string() const
218 {
219  char
220  colorbuf[MaxTextExtent];
221 
222  MagickPixelPacket
223  pixel;
224 
225  if (!isValid())
226  return std::string("none");
227 
228  pixel.colorspace=RGBColorspace;
229  pixel.matte=_pixelType == RGBAPixel ? MagickTrue : MagickFalse;
230  pixel.depth=MAGICKCORE_QUANTUM_DEPTH;
231  pixel.red=_pixel->red;
232  pixel.green=_pixel->green;
233  pixel.blue=_pixel->blue;
234  pixel.opacity=_pixel->opacity;
235  GetColorTuple(&pixel,MagickTrue,colorbuf);
236 
237  return(std::string(colorbuf));
238 }
239 
240 bool Magick::Color::isValid(void) const
241 {
242  return(_isValid);
243 }
244 
245 void Magick::Color::isValid(bool valid_)
246 {
247  if ((valid_ && isValid()) || (!valid_ && !isValid()))
248  return;
249 
250  if (!_pixelOwn)
251  {
252  _pixel=new PixelPacket;
253  _pixelOwn=true;
254  }
255 
256  _isValid=valid_;
257 
258  initPixel();
259 }
260 
261 Magick::Color::Color(PixelPacket *rep_,PixelType pixelType_)
262  : _pixel(rep_),
263  _isValid(true),
264  _pixelOwn(false),
265  _pixelType(pixelType_)
266 {
267 }
268 
269 void Magick::Color::pixel(PixelPacket *rep_,PixelType pixelType_)
270 {
271  if (_pixelOwn)
272  delete _pixel;
273 
274  _pixel=rep_;
275  _pixelOwn=false;
276  _isValid=true;
277  _pixelType=pixelType_;
278 }
279 
281  : Color()
282 {
283 }
284 
286  : Color(color_)
287 {
288 }
289 
291  : Color(scaleDoubleToQuantum(shade_),scaleDoubleToQuantum(shade_),
292  scaleDoubleToQuantum(shade_))
293 {
294  alphaQuantum(OpaqueOpacity);
295 }
296 
298 {
299 }
300 
301 void Magick::ColorGray::shade(double shade_)
302 {
303  Quantum gray=scaleDoubleToQuantum(shade_);
304  redQuantum(gray);
305  greenQuantum(gray);
306  blueQuantum(gray);
307 }
308 
309 double Magick::ColorGray::shade(void) const
310 {
311  return(scaleQuantumToDouble(greenQuantum()));
312 }
313 
315 {
316  *static_cast<Magick::Color*>(this)=color_;
317  return(*this);
318 }
319 
321  : Color()
322 {
323 }
324 
326  : Color( color_ )
327 {
328 }
329 
330 Magick::ColorHSL::ColorHSL(double hue_,double saturation_,double luminosity_)
331  : Color()
332 {
333  Quantum
334  blue,
335  green,
336  red;
337 
338  ConvertHSLToRGB(hue_,saturation_,luminosity_,&red,&green,&blue);
339 
340  redQuantum(red);
341  greenQuantum(green);
342  blueQuantum(blue);
343  alphaQuantum(OpaqueOpacity);
344 }
345 
347 {
348 }
349 
351 {
352  *static_cast<Magick::Color*>(this)=color_;
353  return (*this);
354 }
355 
356 void Magick::ColorHSL::hue(double hue_)
357 {
358  double
359  hue,
360  luminosity,
361  saturation;
362 
363  Quantum
364  blue,
365  green,
366  red;
367 
368  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
369  &luminosity);
370 
371  hue=hue_;
372 
373  ConvertHSLToRGB(hue,saturation,luminosity,&red,&green,&blue);
374 
375  redQuantum(red);
376  greenQuantum(green);
377  blueQuantum(blue);
378 }
379 
380 double Magick::ColorHSL::hue(void) const
381 {
382  double
383  hue,
384  luminosity,
385  saturation;
386 
387  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
388  &luminosity);
389 
390  return(hue);
391 }
392 
393 void Magick::ColorHSL::luminosity(double luminosity_)
394 {
395  double
396  hue,
397  luminosity,
398  saturation;
399 
400  Quantum
401  blue,
402  green,
403  red;
404 
405  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
406  &luminosity);
407 
408  luminosity=luminosity_;
409 
410  ConvertHSLToRGB(hue,saturation,luminosity,&red,&green,&blue);
411 
412  redQuantum(red);
413  greenQuantum(green);
414  blueQuantum(blue);
415 }
416 
418 {
419  double
420  hue,
421  saturation,
422  luminosity;
423 
424  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
425  &luminosity);
426 
427  return(luminosity);
428 }
429 
430 void Magick::ColorHSL::saturation(double saturation_)
431 {
432  double
433  hue,
434  luminosity,
435  saturation;
436 
437  Quantum
438  blue,
439  green,
440  red;
441 
442  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
443  &luminosity);
444 
445  saturation=saturation_;
446 
447  ConvertHSLToRGB(hue,saturation,luminosity,&red,&green,&blue);
448 
449  redQuantum(red);
450  greenQuantum(green);
451  blueQuantum(blue);
452 }
453 
455 {
456  double
457  hue,
458  luminosity,
459  saturation;
460 
461  ConvertRGBToHSL(redQuantum(),greenQuantum(),blueQuantum(),&hue,&saturation,
462  &luminosity);
463 
464  return(saturation);
465 }
466 
468  : Color()
469 {
470 }
471 
473  : Color((mono_ ? QuantumRange : 0),
474  (mono_ ? QuantumRange : 0),
475  (mono_ ? QuantumRange : 0))
476 {
477  alphaQuantum(OpaqueOpacity);
478 }
479 
481  : Color(color_)
482 {
483 }
484 
486 {
487 }
488 
490 {
491  *static_cast<Magick::Color*>(this)=color_;
492  return(*this);
493 }
494 
495 void Magick::ColorMono::mono(bool mono_)
496 {
497  redQuantum(mono_ ? QuantumRange : 0);
498  greenQuantum(mono_ ? QuantumRange : 0);
499  blueQuantum(mono_ ? QuantumRange : 0);
500 }
501 
502 bool Magick::ColorMono::mono(void) const
503 {
504  return(greenQuantum() == 0);
505 }
506 
508  : Color()
509 {
510 }
511 
513  : Color(color_)
514 {
515 }
516 
517 Magick::ColorRGB::ColorRGB(double red_,double green_,double blue_)
518  : Color(scaleDoubleToQuantum(red_),scaleDoubleToQuantum(green_),
519  scaleDoubleToQuantum(blue_))
520 {
521  alphaQuantum(OpaqueOpacity);
522 }
523 
525 {
526 }
527 
529 {
530  *static_cast<Magick::Color*>(this)=color_;
531  return(*this);
532 }
533 
535  : Color()
536 {
537 }
538 
540  : Color(color_)
541 {
542 }
543 
544 Magick::ColorYUV::ColorYUV(double y_,double u_,double v_)
545  : Color(scaleDoubleToQuantum(y_ + 1.13980 * v_),
546  scaleDoubleToQuantum(y_ - (0.39380 * u_) - (0.58050 * v_)),
547  scaleDoubleToQuantum(y_ + 2.02790 * u_))
548 {
549  alphaQuantum(OpaqueOpacity);
550 }
551 
553 {
554 }
555 
557 {
558  *static_cast<Magick::Color*>(this)=color_;
559  return(*this);
560 }
561 
562 void Magick::ColorYUV::u(double u_)
563 {
564  double V = v();
565  double Y = y();
566 
567  redQuantum(scaleDoubleToQuantum(Y + 1.13980 * V ));
568  greenQuantum(scaleDoubleToQuantum( Y - (0.39380 * u_) - (0.58050 * V)));
569  blueQuantum(scaleDoubleToQuantum( Y + 2.02790 * u_));
570 }
571 
572 double Magick::ColorYUV::u(void) const
573 {
574  return scaleQuantumToDouble((-0.14740 * redQuantum()) - (0.28950 *
575  greenQuantum()) + (0.43690 * blueQuantum()));
576 }
577 
578 void Magick::ColorYUV::v(double v_)
579 {
580  double U = u();
581  double Y = y();
582 
583  redQuantum(scaleDoubleToQuantum( Y + 1.13980 * v_ ));
584  greenQuantum(scaleDoubleToQuantum( Y - (0.39380 * U) - (0.58050 * v_) ));
585  blueQuantum(scaleDoubleToQuantum( Y + 2.02790 * U ));
586 }
587 
588 double Magick::ColorYUV::v(void) const
589 {
590  return scaleQuantumToDouble((0.61500 * redQuantum()) - (0.51500 *
591  greenQuantum()) - (0.10000 * blueQuantum()));
592 }
593 
594 void Magick::ColorYUV::y(double y_)
595 {
596  double U = u();
597  double V = v();
598 
599  redQuantum(scaleDoubleToQuantum(y_ + 1.13980 * V));
600  greenQuantum(scaleDoubleToQuantum(y_ - (0.39380 * U) - (0.58050 * V)));
601  blueQuantum(scaleDoubleToQuantum(y_ + 2.02790 * U));
602 }
603 
604 double Magick::ColorYUV::y(void) const
605 {
606  return scaleQuantumToDouble((0.29900 * redQuantum()) + (0.58700 *
607  greenQuantum()) + (0.11400 * blueQuantum()));
608 }
ColorYUV & operator=(const Color &color_)
Definition: Color.cpp:556
MagickPPExport int operator!=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:29
void isValid(bool valid_)
Definition: Color.cpp:245
Quantum greenQuantum(void) const
Definition: Color.h:418
Quantum alphaQuantum(void) const
Definition: Color.h:396
~ColorRGB(void)
Definition: Color.cpp:524
ColorYUV(void)
Definition: Color.cpp:534
STL namespace.
void pixel(PixelPacket *rep_, PixelType pixelType_)
Definition: Color.cpp:269
void redQuantum(Quantum red_)
Definition: Color.h:423
MagickPPExport int operator<(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:41
double hue(void) const
Definition: Color.cpp:380
bool isValid(void) const
Definition: Color.cpp:240
Quantum blueQuantum(void) const
Definition: Color.h:407
ColorGray & operator=(const Color &color_)
Definition: Color.cpp:314
MagickPPExport int operator<=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:63
ColorRGB(void)
Definition: Color.cpp:507
MagickPPExport int operator>=(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:57
double u(void) const
Definition: Color.cpp:572
#define MagickPPExport
Definition: Include.h:255
PixelPacket * _pixel
Definition: Color.h:169
ColorRGB & operator=(const Color &color_)
Definition: Color.cpp:528
bool mono(void) const
Definition: Color.cpp:502
~ColorYUV(void)
Definition: Color.cpp:552
#define ThrowPPException(quiet)
Definition: Include.h:1510
ColorHSL(void)
Definition: Color.cpp:320
virtual ~Color(void)
Definition: Color.cpp:147
double v(void) const
Definition: Color.cpp:588
double shade(void) const
Definition: Color.cpp:309
void blueQuantum(Quantum blue_)
Definition: Color.h:401
void greenQuantum(Quantum green_)
Definition: Color.h:412
MagickPPExport int operator>(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:35
MagickPPExport int operator==(const Magick::Color &left_, const Magick::Color &right_)
Definition: Color.cpp:20
Color & operator=(const Color &color_)
Definition: Color.cpp:161
ColorMono & operator=(const Color &color_)
Definition: Color.cpp:489
double y(void) const
Definition: Color.cpp:604
Quantum redQuantum(void) const
Definition: Color.h:429
#define GetPPException
Definition: Include.h:1506
double saturation(void) const
Definition: Color.cpp:454
Color(void)
Definition: Color.cpp:69
double luminosity(void) const
Definition: Color.cpp:417
ColorHSL & operator=(const Color &color_)
Definition: Color.cpp:350