Qt 4.8
qcolor.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qcolor.h"
43 #include "qcolor_p.h"
44 #include "qnamespace.h"
45 #include "qcolormap.h"
46 #include "qdatastream.h"
47 #include "qvariant.h"
48 #include "qdebug.h"
49 
50 #ifdef Q_WS_X11
51 # include "qapplication.h"
52 # include "qx11info_x11.h"
53 # include "private/qt_x11_p.h"
54 
55 static bool allowX11ColorNames = false;
56 
57 #endif
58 
59 #include <math.h>
60 #include <stdio.h>
61 #include <limits.h>
62 
64 
281 #define QCOLOR_INT_RANGE_CHECK(fn, var) \
282  do { \
283  if (var < 0 || var > 255) { \
284  qWarning(#fn": invalid value %d", var); \
285  var = qMax(0, qMin(var, 255)); \
286  } \
287  } while (0)
288 
289 #define QCOLOR_REAL_RANGE_CHECK(fn, var) \
290  do { \
291  if (var < qreal(0.0) || var > qreal(1.0)) { \
292  qWarning(#fn": invalid value %g", var); \
293  var = qMax(qreal(0.0), qMin(var, qreal(1.0))); \
294  } \
295  } while (0)
296 
297 /*****************************************************************************
298  QColor member functions
299  *****************************************************************************/
300 
356 {
357 #define QRGB(r, g, b) \
358  QRgb(((0xffu << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))
359 #define QRGBA(r, g, b, a) \
360  QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
361 
362  static const QRgb global_colors[] = {
363  QRGB(255, 255, 255), // Qt::color0
364  QRGB( 0, 0, 0), // Qt::color1
365  QRGB( 0, 0, 0), // black
366  QRGB(255, 255, 255), // white
367  /*
368  * From the "The Palette Manager: How and Why" by Ron Gery,
369  * March 23, 1992, archived on MSDN:
370  *
371  * The Windows system palette is broken up into two
372  * sections, one with fixed colors and one with colors
373  * that can be changed by applications. The system palette
374  * predefines 20 entries; these colors are known as the
375  * static or reserved colors and consist of the 16 colors
376  * found in the Windows version 3.0 VGA driver and 4
377  * additional colors chosen for their visual appeal. The
378  * DEFAULT_PALETTE stock object is, as the name implies,
379  * the default palette selected into a device context (DC)
380  * and consists of these static colors. Applications can
381  * set the remaining 236 colors using the Palette Manager.
382  *
383  * The 20 reserved entries have indices in [0,9] and
384  * [246,255]. We reuse 17 of them.
385  */
386  QRGB(128, 128, 128), // index 248 medium gray
387  QRGB(160, 160, 164), // index 247 light gray
388  QRGB(192, 192, 192), // index 7 light gray
389  QRGB(255, 0, 0), // index 249 red
390  QRGB( 0, 255, 0), // index 250 green
391  QRGB( 0, 0, 255), // index 252 blue
392  QRGB( 0, 255, 255), // index 254 cyan
393  QRGB(255, 0, 255), // index 253 magenta
394  QRGB(255, 255, 0), // index 251 yellow
395  QRGB(128, 0, 0), // index 1 dark red
396  QRGB( 0, 128, 0), // index 2 dark green
397  QRGB( 0, 0, 128), // index 4 dark blue
398  QRGB( 0, 128, 128), // index 6 dark cyan
399  QRGB(128, 0, 128), // index 5 dark magenta
400  QRGB(128, 128, 0), // index 3 dark yellow
401  QRGBA(0, 0, 0, 0) // transparent
402  };
403 #undef QRGB
404 #undef QRGBA
405 
406  setRgb(qRed(global_colors[color]),
407  qGreen(global_colors[color]),
408  qBlue(global_colors[color]),
409  qAlpha(global_colors[color]));
410 }
411 
434 {
435  cspec = Rgb;
436  ct.argb.alpha = 0xffff;
437  ct.argb.red = qRed(color) * 0x101;
438  ct.argb.green = qGreen(color) * 0x101;
439  ct.argb.blue = qBlue(color) * 0x101;
440  ct.argb.pad = 0;
441 }
442 
443 
457 {
458  switch (spec) {
459  case Invalid:
460  invalidate();
461  break;
462  case Rgb:
463  setRgb(0, 0, 0);
464  break;
465  case Hsv:
466  setHsv(0, 0, 0);
467  break;
468  case Cmyk:
469  setCmyk(0, 0, 0, 0);
470  break;
471  case Hsl:
472  setHsl(0, 0, 0, 0);
473  break;
474  }
475 }
476 
530 {
531  QString s;
532  s.sprintf("#%02x%02x%02x", red(), green(), blue());
533  return s;
534 }
535 
561 {
562  setColorFromString(name);
563 }
564 
580 {
581  return !name.isEmpty() && QColor().setColorFromString(name);
582 }
583 
585 {
586  if (name.isEmpty()) {
587  invalidate();
588  return true;
589  }
590 
591  if (name.startsWith(QLatin1Char('#'))) {
592  QRgb rgb;
593  if (qt_get_hex_rgb(name.constData(), name.length(), &rgb)) {
594  setRgb(rgb);
595  return true;
596  } else {
597  invalidate();
598  return false;
599  }
600  }
601 
602 #ifndef QT_NO_COLORNAMES
603  QRgb rgb;
604  if (qt_get_named_rgb(name.constData(), name.length(), &rgb)) {
605  setRgba(rgb);
606  return true;
607  } else
608 #endif
609  {
610 #ifdef Q_WS_X11
611  XColor result;
612  if (allowX11ColorNames()
614  && QX11Info::display()
615  && XParseColor(QX11Info::display(), QX11Info::appColormap(), name.toLatin1().constData(), &result)) {
616  setRgb(result.red >> 8, result.green >> 8, result.blue >> 8);
617  return true;
618  } else
619 #endif
620  {
621  invalidate();
622  return false;
623  }
624  }
625 }
626 
633 {
634 #ifndef QT_NO_COLORNAMES
635  return qt_get_colornames();
636 #else
637  return QStringList();
638 #endif
639 }
640 
651 void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const
652 {
653  if (!h || !s || !v)
654  return;
655 
656  if (cspec != Invalid && cspec != Hsv) {
657  toHsv().getHsvF(h, s, v, a);
658  return;
659  }
660 
661  *h = ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
662  *s = ct.ahsv.saturation / qreal(USHRT_MAX);
663  *v = ct.ahsv.value / qreal(USHRT_MAX);
664 
665  if (a)
666  *a = ct.ahsv.alpha / qreal(USHRT_MAX);
667 }
668 
679 void QColor::getHsv(int *h, int *s, int *v, int *a) const
680 {
681  if (!h || !s || !v)
682  return;
683 
684  if (cspec != Invalid && cspec != Hsv) {
685  toHsv().getHsv(h, s, v, a);
686  return;
687  }
688 
689  *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
690  *s = ct.ahsv.saturation >> 8;
691  *v = ct.ahsv.value >> 8;
692 
693  if (a)
694  *a = ct.ahsv.alpha >> 8;
695 }
696 
707 {
708  if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
709  || (s < qreal(0.0) || s > qreal(1.0))
710  || (v < qreal(0.0) || v > qreal(1.0))
711  || (a < qreal(0.0) || a > qreal(1.0))) {
712  qWarning("QColor::setHsvF: HSV parameters out of range");
713  return;
714  }
715 
716  cspec = Hsv;
717  ct.ahsv.alpha = qRound(a * USHRT_MAX);
718  ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
719  ct.ahsv.saturation = qRound(s * USHRT_MAX);
720  ct.ahsv.value = qRound(v * USHRT_MAX);
721  ct.ahsv.pad = 0;
722 }
723 
734 void QColor::setHsv(int h, int s, int v, int a)
735 {
736  if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
737  qWarning("QColor::setHsv: HSV parameters out of range");
738  invalidate();
739  return;
740  }
741 
742  cspec = Hsv;
743  ct.ahsv.alpha = a * 0x101;
744  ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
745  ct.ahsv.saturation = s * 0x101;
746  ct.ahsv.value = v * 0x101;
747  ct.ahsv.pad = 0;
748 }
749 
765 void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const
766 {
767  if (!h || !s || !l)
768  return;
769 
770  if (cspec != Invalid && cspec != Hsl) {
771  toHsl().getHslF(h, s, l, a);
772  return;
773  }
774 
775  *h = ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
776  *s = ct.ahsl.saturation / qreal(USHRT_MAX);
777  *l = ct.ahsl.lightness / qreal(USHRT_MAX);
778 
779  if (a)
780  *a = ct.ahsl.alpha / qreal(USHRT_MAX);
781 }
782 
798 void QColor::getHsl(int *h, int *s, int *l, int *a) const
799 {
800  if (!h || !s || !l)
801  return;
802 
803  if (cspec != Invalid && cspec != Hsl) {
804  toHsl().getHsl(h, s, l, a);
805  return;
806  }
807 
808  *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
809  *s = ct.ahsl.saturation >> 8;
810  *l = ct.ahsl.lightness >> 8;
811 
812  if (a)
813  *a = ct.ahsl.alpha >> 8;
814 }
815 
830 {
831  if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
832  || (s < qreal(0.0) || s > qreal(1.0))
833  || (l < qreal(0.0) || l > qreal(1.0))
834  || (a < qreal(0.0) || a > qreal(1.0))) {
835  qWarning("QColor::setHsvF: HSV parameters out of range");
836  return;
837  }
838 
839  cspec = Hsl;
840  ct.ahsl.alpha = qRound(a * USHRT_MAX);
841  ct.ahsl.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
842  ct.ahsl.saturation = qRound(s * USHRT_MAX);
843  ct.ahsl.lightness = qRound(l * USHRT_MAX);
844  ct.ahsl.pad = 0;
845 }
846 
861 void QColor::setHsl(int h, int s, int l, int a)
862 {
863  if (h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255) {
864  qWarning("QColor::setHsv: HSV parameters out of range");
865  invalidate();
866  return;
867  }
868 
869  cspec = Hsl;
870  ct.ahsl.alpha = a * 0x101;
871  ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
872  ct.ahsl.saturation = s * 0x101;
873  ct.ahsl.lightness = l * 0x101;
874  ct.ahsl.pad = 0;
875 }
876 
887 void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const
888 {
889  if (!r || !g || !b)
890  return;
891 
892  if (cspec != Invalid && cspec != Rgb) {
893  toRgb().getRgbF(r, g, b, a);
894  return;
895  }
896 
897  *r = ct.argb.red / qreal(USHRT_MAX);
898  *g = ct.argb.green / qreal(USHRT_MAX);
899  *b = ct.argb.blue / qreal(USHRT_MAX);
900 
901  if (a)
902  *a = ct.argb.alpha / qreal(USHRT_MAX);
903 
904 }
905 
916 void QColor::getRgb(int *r, int *g, int *b, int *a) const
917 {
918  if (!r || !g || !b)
919  return;
920 
921  if (cspec != Invalid && cspec != Rgb) {
922  toRgb().getRgb(r, g, b, a);
923  return;
924  }
925 
926  *r = ct.argb.red >> 8;
927  *g = ct.argb.green >> 8;
928  *b = ct.argb.blue >> 8;
929 
930  if (a)
931  *a = ct.argb.alpha >> 8;
932 }
933 
955 {
956  if (r < qreal(0.0) || r > qreal(1.0)
957  || g < qreal(0.0) || g > qreal(1.0)
958  || b < qreal(0.0) || b > qreal(1.0)
959  || a < qreal(0.0) || a > qreal(1.0)) {
960  qWarning("QColor::setRgbF: RGB parameters out of range");
961  invalidate();
962  return;
963  }
964 
965  cspec = Rgb;
966  ct.argb.alpha = qRound(a * USHRT_MAX);
967  ct.argb.red = qRound(r * USHRT_MAX);
968  ct.argb.green = qRound(g * USHRT_MAX);
969  ct.argb.blue = qRound(b * USHRT_MAX);
970  ct.argb.pad = 0;
971 }
972 
980 void QColor::setRgb(int r, int g, int b, int a)
981 {
982  if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
983  qWarning("QColor::setRgb: RGB parameters out of range");
984  invalidate();
985  return;
986  }
987 
988  cspec = Rgb;
989  ct.argb.alpha = a * 0x101;
990  ct.argb.red = r * 0x101;
991  ct.argb.green = g * 0x101;
992  ct.argb.blue = b * 0x101;
993  ct.argb.pad = 0;
994 }
995 
1020 {
1021  if (cspec != Invalid && cspec != Rgb)
1022  return toRgb().rgba();
1023  return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8);
1024 }
1025 
1032 {
1033  cspec = Rgb;
1034  ct.argb.alpha = qAlpha(rgba) * 0x101;
1035  ct.argb.red = qRed(rgba) * 0x101;
1036  ct.argb.green = qGreen(rgba) * 0x101;
1037  ct.argb.blue = qBlue(rgba) * 0x101;
1038  ct.argb.pad = 0;
1039 }
1040 
1052 {
1053  if (cspec != Invalid && cspec != Rgb)
1054  return toRgb().rgb();
1055  return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8);
1056 }
1057 
1067 {
1068  cspec = Rgb;
1069  ct.argb.alpha = 0xffff;
1070  ct.argb.red = qRed(rgb) * 0x101;
1071  ct.argb.green = qGreen(rgb) * 0x101;
1072  ct.argb.blue = qBlue(rgb) * 0x101;
1073  ct.argb.pad = 0;
1074 }
1075 
1082 int QColor::alpha() const
1083 { return ct.argb.alpha >> 8; }
1084 
1085 
1095 {
1096  QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha);
1097  ct.argb.alpha = alpha * 0x101;
1098 }
1099 
1107 { return ct.argb.alpha / qreal(USHRT_MAX); }
1108 
1118 {
1119  QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha);
1120  qreal tmp = alpha * USHRT_MAX;
1121  ct.argb.alpha = qRound(tmp);
1122 }
1123 
1124 
1130 int QColor::red() const
1131 {
1132  if (cspec != Invalid && cspec != Rgb)
1133  return toRgb().red();
1134  return ct.argb.red >> 8;
1135 }
1136 
1144 {
1145  QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
1146  if (cspec != Rgb)
1147  setRgb(red, green(), blue(), alpha());
1148  else
1149  ct.argb.red = red * 0x101;
1150 }
1151 
1157 int QColor::green() const
1158 {
1159  if (cspec != Invalid && cspec != Rgb)
1160  return toRgb().green();
1161  return ct.argb.green >> 8;
1162 }
1163 
1171 {
1172  QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
1173  if (cspec != Rgb)
1174  setRgb(red(), green, blue(), alpha());
1175  else
1176  ct.argb.green = green * 0x101;
1177 }
1178 
1179 
1185 int QColor::blue() const
1186 {
1187  if (cspec != Invalid && cspec != Rgb)
1188  return toRgb().blue();
1189  return ct.argb.blue >> 8;
1190 }
1191 
1192 
1200 {
1201  QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
1202  if (cspec != Rgb)
1203  setRgb(red(), green(), blue, alpha());
1204  else
1205  ct.argb.blue = blue * 0x101;
1206 }
1207 
1214 {
1215  if (cspec != Invalid && cspec != Rgb)
1216  return toRgb().redF();
1217  return ct.argb.red / qreal(USHRT_MAX);
1218 }
1219 
1220 
1228 {
1229  QCOLOR_REAL_RANGE_CHECK("QColor::setRedF", red);
1230  if (cspec != Rgb)
1231  setRgbF(red, greenF(), blueF(), alphaF());
1232  else
1233  ct.argb.red = qRound(red * USHRT_MAX);
1234 }
1235 
1242 {
1243  if (cspec != Invalid && cspec != Rgb)
1244  return toRgb().greenF();
1245  return ct.argb.green / qreal(USHRT_MAX);
1246 }
1247 
1248 
1256 {
1257  QCOLOR_REAL_RANGE_CHECK("QColor::setGreenF", green);
1258  if (cspec != Rgb)
1259  setRgbF(redF(), green, blueF(), alphaF());
1260  else
1261  ct.argb.green = qRound(green * USHRT_MAX);
1262 }
1263 
1270 {
1271  if (cspec != Invalid && cspec != Rgb)
1272  return toRgb().blueF();
1273  return ct.argb.blue / qreal(USHRT_MAX);
1274 }
1275 
1283 {
1284  QCOLOR_REAL_RANGE_CHECK("QColor::setBlueF", blue);
1285  if (cspec != Rgb)
1286  setRgbF(redF(), greenF(), blue, alphaF());
1287  else
1288  ct.argb.blue = qRound(blue * USHRT_MAX);
1289 }
1290 
1300 int QColor::hue() const
1301 {
1302  return hsvHue();
1303 }
1304 
1311 int QColor::hsvHue() const
1312 {
1313  if (cspec != Invalid && cspec != Hsv)
1314  return toHsv().hue();
1315  return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1316 }
1317 
1327 int QColor::saturation() const
1328 {
1329  return hsvSaturation();
1330 }
1331 
1339 {
1340  if (cspec != Invalid && cspec != Hsv)
1341  return toHsv().saturation();
1342  return ct.ahsv.saturation >> 8;
1343 }
1344 
1351 int QColor::value() const
1352 {
1353  if (cspec != Invalid && cspec != Hsv)
1354  return toHsv().value();
1355  return ct.ahsv.value >> 8;
1356 }
1357 
1367 {
1368  return hsvHueF();
1369 }
1370 
1378 {
1379  if (cspec != Invalid && cspec != Hsv)
1380  return toHsv().hueF();
1381  return ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
1382 }
1383 
1393 {
1394  return hsvSaturationF();
1395 }
1396 
1404 {
1405  if (cspec != Invalid && cspec != Hsv)
1406  return toHsv().saturationF();
1407  return ct.ahsv.saturation / qreal(USHRT_MAX);
1408 }
1409 
1417 {
1418  if (cspec != Invalid && cspec != Hsv)
1419  return toHsv().valueF();
1420  return ct.ahsv.value / qreal(USHRT_MAX);
1421 }
1422 
1433 int QColor::hslHue() const
1434 {
1435  if (cspec != Invalid && cspec != Hsl)
1436  return toHsl().hslHue();
1437  return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1438 }
1439 
1452 {
1453  if (cspec != Invalid && cspec != Hsl)
1454  return toHsl().hslSaturation();
1455  return ct.ahsl.saturation >> 8;
1456 }
1457 
1468 int QColor::lightness() const
1469 {
1470  if (cspec != Invalid && cspec != Hsl)
1471  return toHsl().lightness();
1472  return ct.ahsl.lightness >> 8;
1473 }
1474 
1486 {
1487  if (cspec != Invalid && cspec != Hsl)
1488  return toHsl().hslHueF();
1489  return ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
1490 }
1491 
1503 {
1504  if (cspec != Invalid && cspec != Hsl)
1505  return toHsl().hslSaturationF();
1506  return ct.ahsl.saturation / qreal(USHRT_MAX);
1507 }
1508 
1520 {
1521  if (cspec != Invalid && cspec != Hsl)
1522  return toHsl().lightnessF();
1523  return ct.ahsl.lightness / qreal(USHRT_MAX);
1524 }
1525 
1532 int QColor::cyan() const
1533 {
1534  if (cspec != Invalid && cspec != Cmyk)
1535  return toCmyk().cyan();
1536  return ct.acmyk.cyan >> 8;
1537 }
1538 
1545 int QColor::magenta() const
1546 {
1547  if (cspec != Invalid && cspec != Cmyk)
1548  return toCmyk().magenta();
1549  return ct.acmyk.magenta >> 8;
1550 }
1551 
1558 int QColor::yellow() const
1559 {
1560  if (cspec != Invalid && cspec != Cmyk)
1561  return toCmyk().yellow();
1562  return ct.acmyk.yellow >> 8;
1563 }
1564 
1572 int QColor::black() const
1573 {
1574  if (cspec != Invalid && cspec != Cmyk)
1575  return toCmyk().black();
1576  return ct.acmyk.black >> 8;
1577 }
1578 
1586 {
1587  if (cspec != Invalid && cspec != Cmyk)
1588  return toCmyk().cyanF();
1589  return ct.acmyk.cyan / qreal(USHRT_MAX);
1590 }
1591 
1599 {
1600  if (cspec != Invalid && cspec != Cmyk)
1601  return toCmyk().magentaF();
1602  return ct.acmyk.magenta / qreal(USHRT_MAX);
1603 }
1604 
1612 {
1613  if (cspec != Invalid && cspec != Cmyk)
1614  return toCmyk().yellowF();
1615  return ct.acmyk.yellow / qreal(USHRT_MAX);
1616 }
1617 
1625 {
1626  if (cspec != Invalid && cspec != Cmyk)
1627  return toCmyk().blackF();
1628  return ct.acmyk.black / qreal(USHRT_MAX);
1629 }
1630 
1637 {
1638  if (!isValid() || cspec == Rgb)
1639  return *this;
1640 
1641  QColor color;
1642  color.cspec = Rgb;
1643  color.ct.argb.alpha = ct.argb.alpha;
1644  color.ct.argb.pad = 0;
1645 
1646  switch (cspec) {
1647  case Hsv:
1648  {
1649  if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
1650  // achromatic case
1651  color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
1652  break;
1653  }
1654 
1655  // chromatic case
1656  const qreal h = ct.ahsv.hue == 36000 ? 0 : ct.ahsv.hue / qreal(6000.);
1657  const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX);
1658  const qreal v = ct.ahsv.value / qreal(USHRT_MAX);
1659  const int i = int(h);
1660  const qreal f = h - i;
1661  const qreal p = v * (qreal(1.0) - s);
1662 
1663  if (i & 1) {
1664  const qreal q = v * (qreal(1.0) - (s * f));
1665 
1666  switch (i) {
1667  case 1:
1668  color.ct.argb.red = qRound(q * USHRT_MAX);
1669  color.ct.argb.green = qRound(v * USHRT_MAX);
1670  color.ct.argb.blue = qRound(p * USHRT_MAX);
1671  break;
1672  case 3:
1673  color.ct.argb.red = qRound(p * USHRT_MAX);
1674  color.ct.argb.green = qRound(q * USHRT_MAX);
1675  color.ct.argb.blue = qRound(v * USHRT_MAX);
1676  break;
1677  case 5:
1678  color.ct.argb.red = qRound(v * USHRT_MAX);
1679  color.ct.argb.green = qRound(p * USHRT_MAX);
1680  color.ct.argb.blue = qRound(q * USHRT_MAX);
1681  break;
1682  }
1683  } else {
1684  const qreal t = v * (qreal(1.0) - (s * (qreal(1.0) - f)));
1685 
1686  switch (i) {
1687  case 0:
1688  color.ct.argb.red = qRound(v * USHRT_MAX);
1689  color.ct.argb.green = qRound(t * USHRT_MAX);
1690  color.ct.argb.blue = qRound(p * USHRT_MAX);
1691  break;
1692  case 2:
1693  color.ct.argb.red = qRound(p * USHRT_MAX);
1694  color.ct.argb.green = qRound(v * USHRT_MAX);
1695  color.ct.argb.blue = qRound(t * USHRT_MAX);
1696  break;
1697  case 4:
1698  color.ct.argb.red = qRound(t * USHRT_MAX);
1699  color.ct.argb.green = qRound(p * USHRT_MAX);
1700  color.ct.argb.blue = qRound(v * USHRT_MAX);
1701  break;
1702  }
1703  }
1704  break;
1705  }
1706  case Hsl:
1707  {
1708  if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
1709  // achromatic case
1710  color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
1711  } else if (ct.ahsl.lightness == 0) {
1712  // lightness 0
1713  color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
1714  } else {
1715  // chromatic case
1716  const qreal h = ct.ahsl.hue == 36000 ? 0 : ct.ahsl.hue / qreal(36000.);
1717  const qreal s = ct.ahsl.saturation / qreal(USHRT_MAX);
1718  const qreal l = ct.ahsl.lightness / qreal(USHRT_MAX);
1719 
1720  qreal temp2;
1721  if (l < qreal(0.5))
1722  temp2 = l * (qreal(1.0) + s);
1723  else
1724  temp2 = l + s - (l * s);
1725 
1726  const qreal temp1 = (qreal(2.0) * l) - temp2;
1727  qreal temp3[3] = { h + (qreal(1.0) / qreal(3.0)),
1728  h,
1729  h - (qreal(1.0) / qreal(3.0)) };
1730 
1731  for (int i = 0; i != 3; ++i) {
1732  if (temp3[i] < qreal(0.0))
1733  temp3[i] += qreal(1.0);
1734  else if (temp3[i] > qreal(1.0))
1735  temp3[i] -= qreal(1.0);
1736 
1737  const qreal sixtemp3 = temp3[i] * qreal(6.0);
1738  if (sixtemp3 < qreal(1.0))
1739  color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
1740  else if ((temp3[i] * qreal(2.0)) < qreal(1.0))
1741  color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
1742  else if ((temp3[i] * qreal(3.0)) < qreal(2.0))
1743  color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (qreal(2.0) /qreal(3.0) - temp3[i]) * qreal(6.0)) * USHRT_MAX);
1744  else
1745  color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
1746  }
1747  color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
1748  color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
1749  color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
1750  }
1751  break;
1752  }
1753  case Cmyk:
1754  {
1755  const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX);
1756  const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX);
1757  const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX);
1758  const qreal k = ct.acmyk.black / qreal(USHRT_MAX);
1759 
1760  color.ct.argb.red = qRound((qreal(1.0) - (c * (qreal(1.0) - k) + k)) * USHRT_MAX);
1761  color.ct.argb.green = qRound((qreal(1.0) - (m * (qreal(1.0) - k) + k)) * USHRT_MAX);
1762  color.ct.argb.blue = qRound((qreal(1.0) - (y * (qreal(1.0) - k) + k)) * USHRT_MAX);
1763  break;
1764  }
1765  default:
1766  break;
1767  }
1768 
1769  return color;
1770 }
1771 
1772 
1773 #define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )
1774 #define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )
1775 
1776 
1784 {
1785  if (!isValid() || cspec == Hsv)
1786  return *this;
1787 
1788  if (cspec != Rgb)
1789  return toRgb().toHsv();
1790 
1791  QColor color;
1792  color.cspec = Hsv;
1793  color.ct.ahsv.alpha = ct.argb.alpha;
1794  color.ct.ahsv.pad = 0;
1795 
1796  const qreal r = ct.argb.red / qreal(USHRT_MAX);
1797  const qreal g = ct.argb.green / qreal(USHRT_MAX);
1798  const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1799  const qreal max = Q_MAX_3(r, g, b);
1800  const qreal min = Q_MIN_3(r, g, b);
1801  const qreal delta = max - min;
1802  color.ct.ahsv.value = qRound(max * USHRT_MAX);
1803  if (qFuzzyIsNull(delta)) {
1804  // achromatic case, hue is undefined
1805  color.ct.ahsv.hue = USHRT_MAX;
1806  color.ct.ahsv.saturation = 0;
1807  } else {
1808  // chromatic case
1809  qreal hue = 0;
1810  color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX);
1811  if (qFuzzyCompare(r, max)) {
1812  hue = ((g - b) /delta);
1813  } else if (qFuzzyCompare(g, max)) {
1814  hue = (qreal(2.0) + (b - r) / delta);
1815  } else if (qFuzzyCompare(b, max)) {
1816  hue = (qreal(4.0) + (r - g) / delta);
1817  } else {
1818  Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1819  }
1820  hue *= qreal(60.0);
1821  if (hue < qreal(0.0))
1822  hue += qreal(360.0);
1823  color.ct.ahsv.hue = qRound(hue * 100);
1824  }
1825 
1826  return color;
1827 }
1828 
1835 {
1836  if (!isValid() || cspec == Hsl)
1837  return *this;
1838 
1839  if (cspec != Rgb)
1840  return toRgb().toHsl();
1841 
1842  QColor color;
1843  color.cspec = Hsl;
1844  color.ct.ahsl.alpha = ct.argb.alpha;
1845  color.ct.ahsl.pad = 0;
1846 
1847  const qreal r = ct.argb.red / qreal(USHRT_MAX);
1848  const qreal g = ct.argb.green / qreal(USHRT_MAX);
1849  const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1850  const qreal max = Q_MAX_3(r, g, b);
1851  const qreal min = Q_MIN_3(r, g, b);
1852  const qreal delta = max - min;
1853  const qreal delta2 = max + min;
1854  const qreal lightness = qreal(0.5) * delta2;
1855  color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX);
1856  if (qFuzzyIsNull(delta)) {
1857  // achromatic case, hue is undefined
1858  color.ct.ahsl.hue = USHRT_MAX;
1859  color.ct.ahsl.saturation = 0;
1860  } else {
1861  // chromatic case
1862  qreal hue = 0;
1863  if (lightness < qreal(0.5))
1864  color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX);
1865  else
1866  color.ct.ahsl.saturation = qRound((delta / (qreal(2.0) - delta2)) * USHRT_MAX);
1867  if (qFuzzyCompare(r, max)) {
1868  hue = ((g - b) /delta);
1869  } else if (qFuzzyCompare(g, max)) {
1870  hue = (qreal(2.0) + (b - r) / delta);
1871  } else if (qFuzzyCompare(b, max)) {
1872  hue = (qreal(4.0) + (r - g) / delta);
1873  } else {
1874  Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1875  }
1876  hue *= qreal(60.0);
1877  if (hue < qreal(0.0))
1878  hue += qreal(360.0);
1879  color.ct.ahsl.hue = qRound(hue * 100);
1880  }
1881 
1882  return color;
1883 }
1884 
1892 {
1893  if (!isValid() || cspec == Cmyk)
1894  return *this;
1895  if (cspec != Rgb)
1896  return toRgb().toCmyk();
1897 
1898  QColor color;
1899  color.cspec = Cmyk;
1900  color.ct.acmyk.alpha = ct.argb.alpha;
1901 
1902  // rgb -> cmy
1903  const qreal r = ct.argb.red / qreal(USHRT_MAX);
1904  const qreal g = ct.argb.green / qreal(USHRT_MAX);
1905  const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1906  qreal c = qreal(1.0) - r;
1907  qreal m = qreal(1.0) - g;
1908  qreal y = qreal(1.0) - b;
1909 
1910  // cmy -> cmyk
1911  const qreal k = qMin(c, qMin(m, y));
1912 
1913  if (!qFuzzyIsNull(k - 1)) {
1914  c = (c - k) / (qreal(1.0) - k);
1915  m = (m - k) / (qreal(1.0) - k);
1916  y = (y - k) / (qreal(1.0) - k);
1917  }
1918 
1919  color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
1920  color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
1921  color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
1922  color.ct.acmyk.black = qRound(k * USHRT_MAX);
1923 
1924  return color;
1925 }
1926 
1928 {
1929  if (colorSpec == cspec)
1930  return *this;
1931  switch (colorSpec) {
1932  case Rgb:
1933  return toRgb();
1934  case Hsv:
1935  return toHsv();
1936  case Cmyk:
1937  return toCmyk();
1938  case Hsl:
1939  return toHsl();
1940  case Invalid:
1941  break;
1942  }
1943  return QColor(); // must be invalid
1944 }
1945 
1946 
1959 {
1960  return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
1961 }
1962 
1963 
1975 {
1976  return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
1977 }
1978 
1988 QColor QColor::fromRgb(int r, int g, int b, int a)
1989 {
1990  if (r < 0 || r > 255
1991  || g < 0 || g > 255
1992  || b < 0 || b > 255
1993  || a < 0 || a > 255) {
1994  qWarning("QColor::fromRgb: RGB parameters out of range");
1995  return QColor();
1996  }
1997 
1998  QColor color;
1999  color.cspec = Rgb;
2000  color.ct.argb.alpha = a * 0x101;
2001  color.ct.argb.red = r * 0x101;
2002  color.ct.argb.green = g * 0x101;
2003  color.ct.argb.blue = b * 0x101;
2004  color.ct.argb.pad = 0;
2005  return color;
2006 }
2007 
2018 {
2019  if (r < qreal(0.0) || r > qreal(1.0)
2020  || g < qreal(0.0) || g > qreal(1.0)
2021  || b < qreal(0.0) || b > qreal(1.0)
2022  || a < qreal(0.0) || a > qreal(1.0)) {
2023  qWarning("QColor::fromRgbF: RGB parameters out of range");
2024  return QColor();
2025  }
2026 
2027  QColor color;
2028  color.cspec = Rgb;
2029  color.ct.argb.alpha = qRound(a * USHRT_MAX);
2030  color.ct.argb.red = qRound(r * USHRT_MAX);
2031  color.ct.argb.green = qRound(g * USHRT_MAX);
2032  color.ct.argb.blue = qRound(b * USHRT_MAX);
2033  color.ct.argb.pad = 0;
2034  return color;
2035 }
2036 
2048 QColor QColor::fromHsv(int h, int s, int v, int a)
2049 {
2050  if (((h < 0 || h >= 360) && h != -1)
2051  || s < 0 || s > 255
2052  || v < 0 || v > 255
2053  || a < 0 || a > 255) {
2054  qWarning("QColor::fromHsv: HSV parameters out of range");
2055  return QColor();
2056  }
2057 
2058  QColor color;
2059  color.cspec = Hsv;
2060  color.ct.ahsv.alpha = a * 0x101;
2061  color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2062  color.ct.ahsv.saturation = s * 0x101;
2063  color.ct.ahsv.value = v * 0x101;
2064  color.ct.ahsv.pad = 0;
2065  return color;
2066 }
2067 
2084 {
2085  if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
2086  || (s < qreal(0.0) || s > qreal(1.0))
2087  || (v < qreal(0.0) || v > qreal(1.0))
2088  || (a < qreal(0.0) || a > qreal(1.0))) {
2089  qWarning("QColor::fromHsvF: HSV parameters out of range");
2090  return QColor();
2091  }
2092 
2093  QColor color;
2094  color.cspec = Hsv;
2095  color.ct.ahsv.alpha = qRound(a * USHRT_MAX);
2096  color.ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
2097  color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
2098  color.ct.ahsv.value = qRound(v * USHRT_MAX);
2099  color.ct.ahsv.pad = 0;
2100  return color;
2101 }
2102 
2118 QColor QColor::fromHsl(int h, int s, int l, int a)
2119 {
2120  if (((h < 0 || h >= 360) && h != -1)
2121  || s < 0 || s > 255
2122  || l < 0 || l > 255
2123  || a < 0 || a > 255) {
2124  qWarning("QColor::fromHsv: HSV parameters out of range");
2125  return QColor();
2126  }
2127 
2128  QColor color;
2129  color.cspec = Hsl;
2130  color.ct.ahsl.alpha = a * 0x101;
2131  color.ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2132  color.ct.ahsl.saturation = s * 0x101;
2133  color.ct.ahsl.lightness = l * 0x101;
2134  color.ct.ahsl.pad = 0;
2135  return color;
2136 }
2137 
2154 {
2155  if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
2156  || (s < qreal(0.0) || s > qreal(1.0))
2157  || (l < qreal(0.0) || l > qreal(1.0))
2158  || (a < qreal(0.0) || a > qreal(1.0))) {
2159  qWarning("QColor::fromHsvF: HSV parameters out of range");
2160  return QColor();
2161  }
2162 
2163  QColor color;
2164  color.cspec = Hsl;
2165  color.ct.ahsl.alpha = qRound(a * USHRT_MAX);
2166  color.ct.ahsl.hue = (h == qreal(-1.0)) ? USHRT_MAX : qRound(h * 36000);
2167  if (color.ct.ahsl.hue == 36000)
2168  color.ct.ahsl.hue = 0;
2169  color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
2170  color.ct.ahsl.lightness = qRound(l * USHRT_MAX);
2171  color.ct.ahsl.pad = 0;
2172  return color;
2173 }
2174 
2175 
2186 void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a)
2187 {
2188  if (!c || !m || !y || !k)
2189  return;
2190 
2191  if (cspec != Invalid && cspec != Cmyk) {
2192  toCmyk().getCmyk(c, m, y, k, a);
2193  return;
2194  }
2195 
2196  *c = ct.acmyk.cyan >> 8;
2197  *m = ct.acmyk.magenta >> 8;
2198  *y = ct.acmyk.yellow >> 8;
2199  *k = ct.acmyk.black >> 8;
2200 
2201  if (a)
2202  *a = ct.acmyk.alpha >> 8;
2203 }
2204 
2216 {
2217  if (!c || !m || !y || !k)
2218  return;
2219 
2220  if (cspec != Invalid && cspec != Cmyk) {
2221  toCmyk().getCmykF(c, m, y, k, a);
2222  return;
2223  }
2224 
2225  *c = ct.acmyk.cyan / qreal(USHRT_MAX);
2226  *m = ct.acmyk.magenta / qreal(USHRT_MAX);
2227  *y = ct.acmyk.yellow / qreal(USHRT_MAX);
2228  *k = ct.acmyk.black / qreal(USHRT_MAX);
2229 
2230  if (a)
2231  *a = ct.acmyk.alpha / qreal(USHRT_MAX);
2232 }
2233 
2243 void QColor::setCmyk(int c, int m, int y, int k, int a)
2244 {
2245  if (c < 0 || c > 255
2246  || m < 0 || m > 255
2247  || y < 0 || y > 255
2248  || k < 0 || k > 255
2249  || a < 0 || a > 255) {
2250  qWarning("QColor::setCmyk: CMYK parameters out of range");
2251  return;
2252  }
2253 
2254  cspec = Cmyk;
2255  ct.acmyk.alpha = a * 0x101;
2256  ct.acmyk.cyan = c * 0x101;
2257  ct.acmyk.magenta = m * 0x101;
2258  ct.acmyk.yellow = y * 0x101;
2259  ct.acmyk.black = k * 0x101;
2260 }
2261 
2277 {
2278  if (c < qreal(0.0) || c > qreal(1.0)
2279  || m < qreal(0.0) || m > qreal(1.0)
2280  || y < qreal(0.0) || y > qreal(1.0)
2281  || k < qreal(0.0) || k > qreal(1.0)
2282  || a < qreal(0.0) || a > qreal(1.0)) {
2283  qWarning("QColor::setCmykF: CMYK parameters out of range");
2284  return;
2285  }
2286 
2287  cspec = Cmyk;
2288  ct.acmyk.alpha = qRound(a * USHRT_MAX);
2289  ct.acmyk.cyan = qRound(c * USHRT_MAX);
2290  ct.acmyk.magenta = qRound(m * USHRT_MAX);
2291  ct.acmyk.yellow = qRound(y * USHRT_MAX);
2292  ct.acmyk.black = qRound(k * USHRT_MAX);
2293 }
2294 
2305 QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
2306 {
2307  if (c < 0 || c > 255
2308  || m < 0 || m > 255
2309  || y < 0 || y > 255
2310  || k < 0 || k > 255
2311  || a < 0 || a > 255) {
2312  qWarning("QColor::fromCmyk: CMYK parameters out of range");
2313  return QColor();
2314  }
2315 
2316  QColor color;
2317  color.cspec = Cmyk;
2318  color.ct.acmyk.alpha = a * 0x101;
2319  color.ct.acmyk.cyan = c * 0x101;
2320  color.ct.acmyk.magenta = m * 0x101;
2321  color.ct.acmyk.yellow = y * 0x101;
2322  color.ct.acmyk.black = k * 0x101;
2323  return color;
2324 }
2325 
2342 {
2343  if (c < qreal(0.0) || c > qreal(1.0)
2344  || m < qreal(0.0) || m > qreal(1.0)
2345  || y < qreal(0.0) || y > qreal(1.0)
2346  || k < qreal(0.0) || k > qreal(1.0)
2347  || a < qreal(0.0) || a > qreal(1.0)) {
2348  qWarning("QColor::fromCmykF: CMYK parameters out of range");
2349  return QColor();
2350  }
2351 
2352  QColor color;
2353  color.cspec = Cmyk;
2354  color.ct.acmyk.alpha = qRound(a * USHRT_MAX);
2355  color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
2356  color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2357  color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
2358  color.ct.acmyk.black = qRound(k * USHRT_MAX);
2359  return color;
2360 }
2361 
2391 QColor QColor::light(int factor) const
2392 {
2393  if (factor <= 0) // invalid lightness factor
2394  return *this;
2395  else if (factor < 100) // makes color darker
2396  return darker(10000 / factor);
2397 
2398  QColor hsv = toHsv();
2399  int s = hsv.ct.ahsv.saturation;
2400  int v = hsv.ct.ahsv.value;
2401 
2402  v = (factor*v)/100;
2403  if (v > USHRT_MAX) {
2404  // overflow... adjust saturation
2405  s -= v - USHRT_MAX;
2406  if (s < 0)
2407  s = 0;
2408  v = USHRT_MAX;
2409  }
2410 
2411  hsv.ct.ahsv.saturation = s;
2412  hsv.ct.ahsv.value = v;
2413 
2414  // convert back to same color spec as original color
2415  return hsv.convertTo(cspec);
2416 }
2417 
2447 QColor QColor::dark(int factor) const
2448 {
2449  if (factor <= 0) // invalid darkness factor
2450  return *this;
2451  else if (factor < 100) // makes color lighter
2452  return lighter(10000 / factor);
2453 
2454  QColor hsv = toHsv();
2455  hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
2456 
2457  // convert back to same color spec as original color
2458  return hsv.convertTo(cspec);
2459 }
2460 
2465 {
2466  cspec = color.cspec;
2467  ct.argb = color.ct.argb;
2468  return *this;
2469 }
2470 
2478 {
2479  return operator=(QColor(color));
2480 }
2481 
2486 bool QColor::operator==(const QColor &color) const
2487 {
2488  if (cspec == Hsl && cspec == color.cspec) {
2489  return (ct.argb.alpha == color.ct.argb.alpha
2490  && ((((ct.ahsl.hue % 36000) == (color.ct.ahsl.hue % 36000)))
2491  || (ct.ahsl.hue == color.ct.ahsl.hue))
2492  && (qAbs(ct.ahsl.saturation - color.ct.ahsl.saturation) < 50
2493  || ct.ahsl.lightness == 0
2494  || color.ct.ahsl.lightness == 0
2495  || ct.ahsl.lightness == USHRT_MAX
2496  || color.ct.ahsl.lightness == USHRT_MAX)
2497  && (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50);
2498  } else {
2499  return (cspec == color.cspec
2500  && ct.argb.alpha == color.ct.argb.alpha
2501  && (((cspec == QColor::Hsv)
2502  && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
2503  || (ct.ahsv.hue == color.ct.ahsv.hue))
2504  && ct.argb.green == color.ct.argb.green
2505  && ct.argb.blue == color.ct.argb.blue
2506  && ct.argb.pad == color.ct.argb.pad);
2507  }
2508 }
2509 
2514 bool QColor::operator!=(const QColor &color) const
2515 { return !operator==(color); }
2516 
2517 
2521 QColor::operator QVariant() const
2522 {
2523  return QVariant(QVariant::Color, this);
2524 }
2525 
2526 #ifdef Q_WS_X11
2527 
2536 {
2538 }
2539 
2550 {
2552 }
2553 #endif
2554 
2564 {
2565  cspec = Invalid;
2566  ct.argb.alpha = USHRT_MAX;
2567  ct.argb.red = 0;
2568  ct.argb.green = 0;
2569  ct.argb.blue = 0;
2570  ct.argb.pad = 0;
2571 }
2572 
2573 #ifdef QT3_SUPPORT
2574 
2589 uint QColor::pixel(int screen) const
2590 {
2592  return cmap.pixel(*this);
2593 }
2594 
2595 #endif // QT3_SUPPORT
2596 
2597 /*****************************************************************************
2598  QColor stream functions
2599  *****************************************************************************/
2600 
2601 #ifndef QT_NO_DEBUG_STREAM
2603 {
2604 #ifndef Q_BROKEN_DEBUG_STREAM
2605  if (!c.isValid())
2606  dbg.nospace() << "QColor(Invalid)";
2607  else if (c.spec() == QColor::Rgb)
2608  dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
2609  else if (c.spec() == QColor::Hsv)
2610  dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
2611  else if (c.spec() == QColor::Cmyk)
2612  dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
2613  << c.blackF()<< ')';
2614  else if (c.spec() == QColor::Hsl)
2615  dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
2616 
2617  return dbg.space();
2618 #else
2619  qWarning("This compiler doesn't support streaming QColor to QDebug");
2620  return dbg;
2621  Q_UNUSED(c);
2622 #endif
2623 }
2624 #endif
2625 
2626 #ifndef QT_NO_DATASTREAM
2627 
2639 {
2640  if (stream.version() < 7) {
2641  if (!color.isValid())
2642  return stream << quint32(0x49000000);
2643  quint32 p = (quint32)color.rgb();
2644  if (stream.version() == 1) // Swap red and blue
2645  p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2646  return stream << p;
2647  }
2648 
2649  qint8 s = color.cspec;
2650  quint16 a = color.ct.argb.alpha;
2651  quint16 r = color.ct.argb.red;
2652  quint16 g = color.ct.argb.green;
2653  quint16 b = color.ct.argb.blue;
2654  quint16 p = color.ct.argb.pad;
2655 
2656  stream << s;
2657  stream << a;
2658  stream << r;
2659  stream << g;
2660  stream << b;
2661  stream << p;
2662 
2663  return stream;
2664 }
2665 
2678 {
2679  if (stream.version() < 7) {
2680  quint32 p;
2681  stream >> p;
2682  if (p == 0x49000000) {
2683  color.invalidate();
2684  return stream;
2685  }
2686  if (stream.version() == 1) // Swap red and blue
2687  p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2688  color.setRgb(p);
2689  return stream;
2690  }
2691 
2692  qint8 s;
2693  quint16 a, r, g, b, p;
2694  stream >> s;
2695  stream >> a;
2696  stream >> r;
2697  stream >> g;
2698  stream >> b;
2699  stream >> p;
2700 
2701  color.cspec = QColor::Spec(s);
2702  color.ct.argb.alpha = a;
2703  color.ct.argb.red = r;
2704  color.ct.argb.green = g;
2705  color.ct.argb.blue = b;
2706  color.ct.argb.pad = p;
2707 
2708  return stream;
2709 }
2710 #endif // QT_NO_DATASTREAM
2711 
2712 
2713 /*****************************************************************************
2714  QColor global functions (documentation only)
2715  *****************************************************************************/
2716 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
void setRed(int red)
Sets the red color component of this color to red.
Definition: qcolor.cpp:1143
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
qreal cyanF() const
Returns the cyan color component of this color.
Definition: qcolor.cpp:1585
void setRgb(int r, int g, int b, int a=255)
Sets the RGB value to r, g, b and the alpha value to a.
Definition: qcolor.cpp:980
#define Q_MAX_3(a, b, c)
Definition: qcolor.cpp:1773
QColor & operator=(const QColor &)
Assigns a copy of color to this color, and returns a reference to it.
Definition: qcolor.cpp:2464
static QColormap instance(int screen=-1)
qreal alphaF() const
Returns the alpha color component of this color.
Definition: qcolor.cpp:1106
QStringList qt_get_colornames()
Definition: qcolor_p.cpp:344
unsigned int QRgb
Definition: qrgb.h:53
QString & sprintf(const char *format,...)
Safely builds a formatted string from the format string cformat and an arbitrary list of arguments...
Definition: qstring.cpp:5567
double qreal
Definition: qglobal.h:1193
qreal hslHueF() const
Returns the hue color component of this color.
Definition: qcolor.cpp:1485
void setNamedColor(const QString &name)
Sets the RGB value of this QColor to name, which may be in one of these formats:
Definition: qcolor.cpp:560
int lightness() const
unsigned char c[8]
Definition: qnumeric_p.h:62
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
QColor toCmyk() const
Creates and returns a CMYK QColor based on this color.
Definition: qcolor.cpp:1891
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
ushort hue
Returns the hue color component of this color.
Definition: qcolor.h:250
union QColor::@220 ct
int red() const
friend Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QColor &)
Writes the color to the stream.
Definition: qcolor.cpp:2638
void getCmyk(int *c, int *m, int *y, int *k, int *a=0)
Sets the contents pointed to by c, m, y, k, and a, to the cyan, magenta, yellow, black, and alpha-channel (transparency) components of the color&#39;s CMYK value.
Definition: qcolor.cpp:2186
struct QColor::@220::@224 ahsl
int green() const
qreal valueF() const
Returns the value color component of this color.
Definition: qcolor.cpp:1416
qreal greenF() const
Returns the green color component of this color.
Definition: qcolor.cpp:1241
static QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a=1.0)
Static convenience function that returns a QColor constructed from the given CMYK color values: c (cy...
Definition: qcolor.cpp:2341
#define QRGB(r, g, b)
int hslHue() const
Returns the hue color component of this color.
Definition: qcolor.cpp:1433
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
int magenta() const
Spec
The type of color specified, either RGB, HSV, CMYK or HSL.
Definition: qcolor.h:70
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QColor fromHsv(int h, int s, int v, int a=255)
Static convenience function that returns a QColor constructed from the HSV color values, h (hue), s (saturation), v (value), and a (alpha-channel, i.e.
Definition: qcolor.cpp:2048
void setRgbF(qreal r, qreal g, qreal b, qreal a=1.0)
Sets the color channels of this color to r (red), g (green), b (blue) and a (alpha, transparency).
Definition: qcolor.cpp:954
Spec cspec
Definition: qcolor.h:239
int alpha() const
ushort lightness
Returns the lightness color component of this color.
Definition: qcolor.h:266
void setGreen(int green)
Sets the green color component of this color to green.
Definition: qcolor.cpp:1170
static QColor cmap[256]
Definition: qgl_mac.mm:760
int hslSaturation() const
Returns the saturation color component of this color.
Definition: qcolor.cpp:1451
qreal blackF() const
Returns the black color component of this color.
Definition: qcolor.cpp:1624
static QColor fromHsvF(qreal h, qreal s, qreal v, qreal a=1.0)
Static convenience function that returns a QColor constructed from the HSV color values, h (hue), s (saturation), v (value), and a (alpha-channel, i.
Definition: qcolor.cpp:2083
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
qreal lightnessF() const
Returns the lightness color component of this color.
Definition: qcolor.cpp:1519
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
long ASN1_INTEGER_get ASN1_INTEGER * a
qreal hslSaturationF() const
Returns the saturation color component of this color.
Definition: qcolor.cpp:1502
struct QColor::@220::@221 argb
qreal hsvHueF() const
Returns the hue color component of this color.
Definition: qcolor.cpp:1377
void invalidate()
Marks the color as invalid and sets all components to zero (alpha is set to fully opaque for compatib...
Definition: qcolor.cpp:2563
int value() const
void setGreenF(qreal green)
Sets the green color component of this color to green.
Definition: qcolor.cpp:1255
qreal saturationF() const
Returns the saturation color component of this color.
Definition: qcolor.cpp:1392
ushort red
Returns the red color component of this color.
Definition: qcolor.h:243
The QString class provides a Unicode character string.
Definition: qstring.h:83
void getHsl(int *h, int *s, int *l, int *a=0) const
Sets the contents pointed to by h, s, l, and a, to the hue, saturation, lightness, and alpha-channel (transparency) components of the color&#39;s HSL value.
Definition: qcolor.cpp:798
QColor toHsv() const
Creates and returns an HSV QColor based on this color.
Definition: qcolor.cpp:1783
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
void setRedF(qreal red)
Sets the red color component of this color to red.
Definition: qcolor.cpp:1227
void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a=0)
Sets the contents pointed to by c, m, y, k, and a, to the cyan, magenta, yellow, black, and alpha-channel (transparency) components of the color&#39;s CMYK value.
Definition: qcolor.cpp:2215
qreal hsvSaturationF() const
Returns the saturation color component of this color.
Definition: qcolor.cpp:1403
int qRed(QRgb rgb)
Returns the red component of the ARGB quadruplet rgb.
Definition: qrgb.h:57
void setHslF(qreal h, qreal s, qreal l, qreal a=1.0)
Sets a HSL color lightness; h is the hue, s is the saturation, l is the lightness and a is the alpha ...
Definition: qcolor.cpp:829
GlobalColor
Definition: qnamespace.h:104
void getHsv(int *h, int *s, int *v, int *a=0) const
Sets the contents pointed to by h, s, v, and a, to the hue, saturation, value, and alpha-channel (tra...
Definition: qcolor.cpp:679
void setHsv(int h, int s, int v, int a=255)
Sets a HSV color value; h is the hue, s is the saturation, v is the value and a is the alpha componen...
Definition: qcolor.cpp:734
int qAlpha(QRgb rgba)
Returns the alpha component of the ARGB quadruplet rgba.
Definition: qrgb.h:66
ushort yellow
Returns the yellow color component of this color.
Definition: qcolor.h:259
ushort magenta
Returns the magenta color component of this color.
Definition: qcolor.h:258
QColor darker(int f=200) const
Returns a darker (or lighter) color, but does not change this object.
Definition: qcolor.h:301
void getRgbF(qreal *r, qreal *g, qreal *b, qreal *a=0) const
Sets the contents pointed to by r, g, b, and a, to the red, green, blue, and alpha-channel (transpare...
Definition: qcolor.cpp:887
signed char qint8
Definition: qglobal.h:933
int yellow() const
bool qt_get_named_rgb(const char *name, QRgb *rgb)
Definition: qcolor_p.cpp:306
void setHsvF(qreal h, qreal s, qreal v, qreal a=1.0)
Sets a HSV color value; h is the hue, s is the saturation, v is the value and a is the alpha componen...
Definition: qcolor.cpp:706
static bool allowX11ColorNames()
Returns true if setNamedColor() is allowed to look up colors in the X11 color database.
Definition: qcolor.cpp:2535
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
struct QColor::@220::@222 ahsv
ushort value
Returns the value color component of this color.
Definition: qcolor.h:252
bool setColorFromString(const QString &name)
Definition: qcolor.cpp:584
static FILE * stream
static QColor fromRgba(QRgb rgba)
Static convenience function that returns a QColor constructed from the given QRgb value rgba...
Definition: qcolor.cpp:1974
ushort array[5]
Definition: qcolor.h:269
static bool isValidColor(const QString &name)
Returns true if the name is a valid color name and can be used to construct a valid QColor object...
Definition: qcolor.cpp:579
ushort saturation
Returns the saturation color component of this color.
Definition: qcolor.h:251
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
void setHsl(int h, int s, int l, int a=255)
Sets a HSL color value; h is the hue, s is the saturation, l is the lightness and a is the alpha comp...
Definition: qcolor.cpp:861
void setBlueF(qreal blue)
Sets the blue color component of this color to blue.
Definition: qcolor.cpp:1282
int saturation() const
QColor dark(int f=200) const
Use darker(factor) instead.
Definition: qcolor.cpp:2447
void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a=1.0)
Sets the color to CMYK values, c (cyan), m (magenta), y (yellow), k (black), and a (alpha-channel...
Definition: qcolor.cpp:2276
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
friend Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QColor &)
Reads the color from the stream.
Definition: qcolor.cpp:2677
int hsvHue() const
Returns the hue color component of this color.
Definition: qcolor.cpp:1311
unsigned short quint16
Definition: qglobal.h:936
Q_CORE_EXPORT void qWarning(const char *,...)
unsigned int uint
Definition: qglobal.h:996
static QColor fromHslF(qreal h, qreal s, qreal l, qreal a=1.0)
Static convenience function that returns a QColor constructed from the HSV color values, h (hue), s (saturation), l (lightness), and a (alpha-channel, i.
Definition: qcolor.cpp:2153
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a=1.0)
Static convenience function that returns a QColor constructed from the RGB color values, r (red), g (green), b (blue), and a (alpha-channel, i.e.
Definition: qcolor.cpp:2017
static Qt::HANDLE appColormap(int screen=-1)
Returns a handle for the application&#39;s color map on the given screen.
static void setAllowX11ColorNames(bool enabled)
Allow setNamedColor() to look up colors in the X11 color database if enabled.
Definition: qcolor.cpp:2549
void setBlue(int blue)
Sets the blue color component of this color to blue.
Definition: qcolor.cpp:1199
uint pixel(const QColor &color) const
void setRgba(QRgb rgba)
Use setRgb() instead.
Definition: qcolor.cpp:1031
#define QRGBA(r, g, b, a)
qreal yellowF() const
Returns the yellow color component of this color.
Definition: qcolor.cpp:1611
qreal hueF() const
Returns the hue color component of this color.
Definition: qcolor.cpp:1366
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
static QColor fromHsl(int h, int s, int l, int a=255)
Static convenience function that returns a QColor constructed from the HSV color values, h (hue), s (saturation), l (lightness), and a (alpha-channel, i.
Definition: qcolor.cpp:2118
QColor toRgb() const
Create and returns an RGB QColor based on this color.
Definition: qcolor.cpp:1636
bool operator==(const QColor &c) const
Returns true if this color has the same RGB and alpha values as color; otherwise returns false...
Definition: qcolor.cpp:2486
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, {r}, {g}, {b}).
Definition: qrgb.h:69
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
int black() const
ushort blue
Returns the blue color component of this color.
Definition: qcolor.h:245
void setAlphaF(qreal alpha)
Sets the alpha of this color to alpha.
Definition: qcolor.cpp:1117
static QColor fromCmyk(int c, int m, int y, int k, int a=255)
Static convenience function that returns a QColor constructed from the given CMYK color values: c (cy...
Definition: qcolor.cpp:2305
static QCoreApplication * instance()
Returns a pointer to the application&#39;s QCoreApplication (or QApplication) instance.
qreal redF() const
Returns the red color component of this color.
Definition: qcolor.cpp:1213
static bool allowX11ColorNames
Definition: qcolor.cpp:55
int cyan() const
unsigned int quint32
Definition: qglobal.h:938
QColor convertTo(Spec colorSpec) const
Creates a copy of this color in the format specified by colorSpec.
Definition: qcolor.cpp:1927
void setAlpha(int alpha)
Sets the alpha of this color to alpha.
Definition: qcolor.cpp:1094
qreal blueF() const
Returns the blue color component of this color.
Definition: qcolor.cpp:1269
void getHsvF(qreal *h, qreal *s, qreal *v, qreal *a=0) const
Sets the contents pointed to by h, s, v, and a, to the hue, saturation, value, and alpha-channel (tra...
Definition: qcolor.cpp:651
QFactoryLoader * l
void getRgb(int *r, int *g, int *b, int *a=0) const
Sets the contents pointed to by r, g, b, and a, to the red, green, blue, and alpha-channel (transpare...
Definition: qcolor.cpp:916
static QColor fromRgb(QRgb rgb)
Static convenience function that returns a QColor constructed from the given QRgb value rgb...
Definition: qcolor.cpp:1958
ushort black
Returns the black color component of this color.
Definition: qcolor.h:260
static QStringList colorNames()
Returns a QStringList containing the color names Qt knows about.
Definition: qcolor.cpp:632
int blue() const
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
QColor()
Constructs an invalid color with the RGB value (0, 0, 0).
Definition: qcolor.h:279
int hsvSaturation() const
Returns the saturation color component of this color.
Definition: qcolor.cpp:1338
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
int hue() const
bool isValid() const
Returns true if the color is valid; otherwise returns false.
Definition: qcolor.h:295
QRgb rgba() const
Returns the RGB value of the color, including its alpha.
Definition: qcolor.cpp:1019
QColor toHsl() const
Creates and returns an HSL QColor based on this color.
Definition: qcolor.cpp:1834
int qGreen(QRgb rgb)
Returns the green component of the ARGB quadruplet rgb.
Definition: qrgb.h:60
QColor lighter(int f=150) const
Returns a lighter (or darker) color, but does not change this object.
Definition: qcolor.h:298
#define Q_MIN_3(a, b, c)
Definition: qcolor.cpp:1774
int qBlue(QRgb rgb)
Returns the blue component of the ARGB quadruplet rgb.
Definition: qrgb.h:63
#define QCOLOR_INT_RANGE_CHECK(fn, var)
Definition: qcolor.cpp:281
ushort cyan
Returns the cyan color component of this color.
Definition: qcolor.h:257
bool operator!=(const QColor &c) const
Returns true if this color has a different RGB and alpha values from color; otherwise returns false...
Definition: qcolor.cpp:2514
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
qreal magentaF() const
Returns the magenta color component of this color.
Definition: qcolor.cpp:1598
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
ushort green
Returns the green color component of this color.
Definition: qcolor.h:244
bool qt_get_hex_rgb(const char *name, QRgb *rgb)
Definition: qcolor_p.cpp:76
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QString name() const
Returns the name of the color in the format "#RRGGBB"; i.e.
Definition: qcolor.cpp:529
#define QCOLOR_REAL_RANGE_CHECK(fn, var)
Definition: qcolor.cpp:289
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition: qstring.h:712
QRgb rgb() const
Returns the RGB value of the color.
Definition: qcolor.cpp:1051
#define enabled
QRgb qRgba(int r, int g, int b, int a)
Returns the ARGB quadruplet ({a}, {r}, {g}, {b}).
Definition: qrgb.h:72
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203
static Display * display()
Returns the default display for the application.
void getHslF(qreal *h, qreal *s, qreal *l, qreal *a=0) const
Sets the contents pointed to by h, s, l, and a, to the hue, saturation, lightness, and alpha-channel (transparency) components of the color&#39;s HSL value.
Definition: qcolor.cpp:765
struct QColor::@220::@223 acmyk
QColor light(int f=150) const
Use lighter(factor) instead.
Definition: qcolor.cpp:2391
Spec spec() const
Returns how the color was specified.
Definition: qcolor.h:88
void setCmyk(int c, int m, int y, int k, int a=255)
Sets the color to CMYK values, c (cyan), m (magenta), y (yellow), k (black), and a (alpha-channel...
Definition: qcolor.cpp:2243