📄 qcolor.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qcolor.h"#include "qcolor_p.h"#include "qnamespace.h"#include "qcolormap.h"#include "qdatastream.h"#include "qvariant.h"#include "qdebug.h"#ifdef Q_WS_X11# include "qapplication.h"# include "qx11info_x11.h"# include "private/qt_x11_p.h"static bool allowX11ColorNames = false;#endif#include <math.h>#include <stdio.h>#include <limits.h>/*! \class QColor \brief The QColor class provides colors based on RGB, HSV or CMYK values. \ingroup multimedia \ingroup appearance \mainclass A color is normally specified in terms of RGB (red, green, and blue) components, but it is also possible to specify it in terms of HSV (hue, saturation, and value) and CMYK (cyan, magenta, yellow and black) components. In addition a color can be specified using a color name. The color name can be any of the SVG 1.0 color names. \table \row \o \inlineimage qcolor-rgb.png \o \inlineimage qcolor-hsv.png \o \inlineimage qcolor-cmyk.png \header \o RGB \o HSV \o CMYK \endtable The QColor constructor creates the color based on RGB values. To create a QColor based on either HSV or CMYK values, use the toHsv() and toCmyk() functions respectively. These functions return a copy of the color using the desired format. In addition the static fromRgb(), fromHsv() and fromCmyk() functions create colors from the specified values. Alternatively, a color can be converted to any of the three formats using the convertTo() function (returning a copy of the color in the desired format), or any of the setRgb(), setHsv() and setCmyk() functions altering \e this color's format. The spec() function tells how the color was specified. A color can be set by passing an RGB string (such as "#112233"), or a color name (such as "blue"), to the setNamedColor() function. The color names are taken from the SVG 1.0 color names. The name() function returns the name of the color in the format "#RRGGBB". Colors can also be set using setRgb(), setHsv() and setCmyk(). To get a lighter or darker color use the lighter() and darker() functions respectively. The isValid() function indicates whether a QColor is legal at all. For example, a RGB color with RGB values out of range is illegal. For performance reasons, QColor mostly disregards illegal colors, and for that reason, the result of using an invalid color is undefined. The color components can be retrieved individually, e.g with red(), hue() and cyan(). The values of the color components can also be retrieved in one go using the getRgb(), getHsv() and getCmyk() functions. Using the RGB color model, the color components can in addition be accessed with rgb(). There are several related non-members: QRgb is a typdef for an unsigned int representing the RGB value triplet (r, g, b). Note that it also can hold a value for the alpha-channel (for more information, see the \l {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section). The qRed(), qBlue() and qGreen() functions return the respective component of the given QRgb value, while the qRgb() and qRgba() functions create and return the QRgb triplet based on the given component values. Finally, the qAlpha() function returns the alpha component of the provided QRgb, and the qGray() function calculates and return a gray value based on the given value. QColor is platform and device independent. The QColormap class maps the color to the hardware. For more information about painting in general, see \l{The Paint System} documentation. \tableofcontents \section1 Integer vs. Floating Point Precision QColor supports floating point precision and provides floating point versions of all the color components functions, e.g. getRgbF(), hueF() and fromCmykF(). Note that since the components are stored using 16-bit integers, there might be minor deviations between the values set using, for example, setRgbF() and the values returned by the getRgbF() function due to rounding. While the integer based functions take values in the range 0-255 (except hue() which must have values within the range 0-359), the floating point functions accept values in the range 0.0 - 1.0. \section1 Alpha-Blended Drawing QColor also support alpha-blended outlining and filling. The alpha channel of a color specifies the transparency effect, 0 represents a fully transparent color, while 255 represents a fully opaque color. For example: \code // Specfiy semi-transparent red painter.setBrush(QColor(255, 0, 0, 127)); painter.drawRect(0, 0, width()/2, height()); // Specify semi-transparend blue painter.setBrush(QColor(0, 0, 255, 127)); painter.drawRect(0, 0, width(), height()/2); \endcode The code above produces the following output: \img alphafill.png Alpha-blended drawing is supported on Windows, Mac OS X, and on X11 systems that have the X Render extension installed. The alpha channel of a color can be retrieved and set using the alpha() and setAlpha() functions if its value is an integer, and alphaF() and setAlphaF() if its value is qreal (double). By default, the alpha-channel is set to 255 (opaque). To retrieve and set \e all the RGB color components (including the alpha-channel) in one go, use the rgba() and setRgba() functions. \section1 Predefined Colors There are 20 predefined QColors: Qt::white, Qt::black, Qt::red, Qt::darkRed, Qt::green, Qt::darkGreen, Qt::blue, Qt::darkBlue, Qt::cyan, Qt::darkCyan, Qt::magenta, Qt::darkMagenta, Qt::yellow, Qt::darkYellow, Qt::gray, Qt::darkGray, Qt::lightGray, Qt::color0, Qt::color1, and Qt::transparent. \img qt-colors.png Qt Colors QColor provides the static colorNames() function which returns a QStringList containing the color names Qt knows about. The colors Qt::color0 (zero pixel value) and Qt::color1 (non-zero pixel value) are special colors for drawing in QBitmaps. Painting with Qt::color0 sets the bitmap bits to 0 (transparent, i.e. background), and painting with Qt::color1 sets the bits to 1 (opaque, i.e. foreground). \section1 The HSV Color Model The RGB model is hardware-oriented. Its representation is close to what most monitors show. In contrast, HSV represents color in a way more suited to the human perception of color. For example, the relationships "stronger than", "darker than", and "the opposite of" are easily expressed in HSV but are much harder to express in RGB. HSV, like RGB, has three components: \list \o H, for hue, is in the range 0 to 359 if the color is chromatic (not gray), or meaningless if it is gray. It represents degrees on the color wheel familiar to most people. Red is 0 (degrees), green is 120, and blue is 240. \inlineimage qcolor-hue.png \o S, for saturation, is in the range 0 to 255, and the bigger it is, the stronger the color is. Grayish colors have saturation near 0; very strong colors have saturation near 255. \inlineimage qcolor-saturation.png \o V, for value, is in the range 0 to 255 and represents lightness or brightness of the color. 0 is black; 255 is as far from black as possible. \inlineimage qcolor-value.png \endlist Here are some examples: pure red is H=0, S=255, V=255; a dark red, moving slightly towards the magenta, could be H=350 (equivalent to -10), S=255, V=180; a grayish light red could have H about 0 (say 350-359 or 0-10), S about 50-100, and S=255. Qt returns a hue value of -1 for achromatic colors. If you pass a hue value that is too large, Qt forces it into range. Hue 360 or 720 is treated as 0; hue 540 is treated as 180. In addition to the standard HSV model, Qt provides an alpha-channel to feature \l {QColor#Alpha-Blended Drawing}{alpha-blended drawing}. \section1 The CMYK Color Model While the RGB and HSV color models are used for display on computer monitors, the CMYK model is used in the four-color printing process of printing presses and some hard-copy devices. CMYK has four components, all in the range 0-255: cyan (C), magenta (M), yellow (Y) and black (K). Cyan, magenta and yellow are called subtractive colors; the CMYK color model creates color by starting with a white surface and then subtracting color by applying the appropriate components. While combining cyan, magenta and yellow gives the color black, subtracting one or more will yield any other color. When combined in various percentages, these three colors can create the entire spectrum of colors. Mixing 100 percent of cyan, magenta and yellow \e does produce black, but the result is unsatisfactory since it wastes ink, increases drying time, and gives a muddy colour when printing. For that reason, black is added in professional printing to provide a solid black tone; hence the term 'four color process'. In addition to the standard CMYK model, Qt provides an alpha-channel to feature \l {QColor#Alpha-Blended Drawing}{alpha-blended drawing}. \sa QPalette, QBrush, QApplication::setColorSpec()*/#define QCOLOR_INT_RANGE_CHECK(fn, var) \ do { \ if (var < 0 || var > 255) { \ qWarning(#fn": invalid value %d", var); \ var = qMax(0, qMin(var, 255)); \ } \ } while (0)#define QCOLOR_REAL_RANGE_CHECK(fn, var) \ do { \ if (var < qreal(0.0) || var > qreal(1.0)) { \ qWarning(#fn": invalid value %g", var); \ var = qMax(qreal(0.0), qMin(var, qreal(1.0))); \ } \ } while (0)/***************************************************************************** QColor member functions *****************************************************************************//*! \enum QColor::Spec The type of color specified, either RGB, HSV or CMYK. \value Rgb \value Hsv \value Cmyk \value Invalid \sa spec(), convertTo()*//*! \fn Spec QColor::spec() const Returns how the color was specified. \sa Spec, convertTo()*//*! \fn QColor::QColor() Constructs an invalid color with the RGB value (0, 0, 0). An invalid color is a color that is not properly set up for the underlying window system. The alpha value of an invalid color is unspecified. \sa isValid()*//*! \overload Constructs a new color with a color value of \a color. \sa isValid(), {QColor#Predefined Colors}{Predefined Colors} */QColor::QColor(Qt::GlobalColor color){#define QRGB(r, g, b) \ QRgb(((0xff << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))#define QRGBA(r, g, b, a) \ QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)) static const QRgb global_colors[] = { QRGB(255, 255, 255), // Qt::color0 QRGB( 0, 0, 0), // Qt::color1 QRGB( 0, 0, 0), // black QRGB(255, 255, 255), // white /* * From the "The Palette Manager: How and Why" by Ron Gery, * March 23, 1992, archived on MSDN: * * The Windows system palette is broken up into two * sections, one with fixed colors and one with colors * that can be changed by applications. The system palette * predefines 20 entries; these colors are known as the * static or reserved colors and consist of the 16 colors * found in the Windows version 3.0 VGA driver and 4 * additional colors chosen for their visual appeal. The * DEFAULT_PALETTE stock object is, as the name implies, * the default palette selected into a device context (DC) * and consists of these static colors. Applications can * set the remaining 236 colors using the Palette Manager. * * The 20 reserved entries have indices in [0,9] and * [246,255]. We reuse 17 of them. */ QRGB(128, 128, 128), // index 248 medium gray QRGB(160, 160, 164), // index 247 light gray QRGB(192, 192, 192), // index 7 light gray QRGB(255, 0, 0), // index 249 red QRGB( 0, 255, 0), // index 250 green QRGB( 0, 0, 255), // index 252 blue QRGB( 0, 255, 255), // index 254 cyan QRGB(255, 0, 255), // index 253 magenta QRGB(255, 255, 0), // index 251 yellow QRGB(128, 0, 0), // index 1 dark red QRGB( 0, 128, 0), // index 2 dark green QRGB( 0, 0, 128), // index 4 dark blue QRGB( 0, 128, 128), // index 6 dark cyan QRGB(128, 0, 128), // index 5 dark magenta QRGB(128, 128, 0), // index 3 dark yellow QRGBA(0, 0, 0, 0) // transparent };#undef QRGB#undef QRGBA setRgb(qRed(global_colors[color]), qGreen(global_colors[color]), qBlue(global_colors[color]), qAlpha(global_colors[color]));}/*! \fn QColor::QColor(int r, int g, int b, int a = 255) Constructs a color with the RGB value \a r, \a g, \a b, and the alpha-channel (transparency) value of \a a. The color is left invalid if any of the arguments are invalid. \sa setRgba(), isValid()*//*! Constructs a color with the value \a color. The alpha component is ignored and set to solid. \sa fromRgb(), isValid()*/QColor::QColor(QRgb color){ cspec = Rgb; ct.argb.alpha = 0xffff; ct.argb.red = qRed(color) * 0x101; ct.argb.green = qGreen(color) * 0x101; ct.argb.blue = qBlue(color) * 0x101; ct.argb.pad = 0;}/*! \internal Constructs a color with the given \a spec. This function is primarly present to avoid that QColor::Invalid becomes a valid color by accident.*/QColor::QColor(Spec spec){ switch (spec) { case Invalid: invalidate(); break; case Rgb: setRgb(0, 0, 0); break; case Hsv: setHsv(0, 0, 0); break; case Cmyk: setCmyk(0, 0, 0, 0); break; }}/*! \fn QColor::QColor(const QString &name) Constructs a named color in the same way as setNamedColor() using the given \a name.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -