📄 qcolor.cpp
字号:
qWarning("QColor::fromCmykF: CMYK parameters out of range"); return QColor(); } QColor color; color.cspec = Cmyk; color.ct.acmyk.alpha = qRound(a * USHRT_MAX); color.ct.acmyk.cyan = qRound(c * USHRT_MAX); color.ct.acmyk.magenta = qRound(m * USHRT_MAX); color.ct.acmyk.yellow = qRound(y * USHRT_MAX); color.ct.acmyk.black = qRound(k * USHRT_MAX); return color;}/*! \fn QColor QColor::lighter(int factor) const \since 4.3 Returns a lighter (or darker) color, but does not change this object. If the \a factor is greater than 100, this functions returns a lighter color. Setting \a factor to 150 returns a color that is 50% brighter. If the \a factor is less than 100, the return color is darker, but we recommend using the darker() function for this purpose. If the \a factor is 0 or negative, the return value is unspecified. The function converts the current RGB color to HSV, multiplies the value (V) component by \a factor and converts the color back to RGB. \sa darker(), isValid()*//*! \obsolete Use lighter(\a factor) instead.*/QColor QColor::light(int factor) const{ if (factor <= 0) // invalid lightness factor return *this; else if (factor < 100) // makes color darker return darker(10000 / factor); QColor hsv = toHsv(); int s = hsv.ct.ahsv.saturation; int v = hsv.ct.ahsv.value; v = (factor*v)/100; if (v > USHRT_MAX) { // overflow... adjust saturation s -= v - USHRT_MAX; if (s < 0) s = 0; v = USHRT_MAX; } hsv.ct.ahsv.saturation = s; hsv.ct.ahsv.value = v; // convert back to same color spec as original color return hsv.convertTo(cspec);}/*! \fn QColor QColor::darker(int factor) const \since 4.3 Returns a darker (or lighter) color, but does not change this object. If the \a factor is greater than 100, this functions returns a darker color. Setting \a factor to 300 returns a color that has one-third the brightness. If the \a factor is less than 100, the return color is lighter, but we recommend using the lighter() function for this purpose. If the \a factor is 0 or negative, the return value is unspecified. The function converts the current RGB color to HSV, divides the value (V) component by \a factor and converts the color back to RGB. \sa lighter(), isValid()*//*! \obsolete Use darker(\a factor) instead.*/QColor QColor::dark(int factor) const{ if (factor <= 0) // invalid darkness factor return *this; else if (factor < 100) // makes color lighter return lighter(10000 / factor); QColor hsv = toHsv(); hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor; // convert back to same color spec as original color return hsv.convertTo(cspec);}/*! Assigns a copy of the color \a color to this color, and returns a reference to it.*/QColor &QColor::operator=(const QColor &color){ cspec = color.cspec; ct.argb = color.ct.argb; return *this;}/*! \overload Assigns a copy of the \a color and returns a reference to this color. */QColor &QColor::operator=(Qt::GlobalColor color){ return operator=(QColor(color));}/*! Returns true if this color has the same RGB value as the color \a color; otherwise returns false.*/bool QColor::operator==(const QColor &color) const{ return (cspec == color.cspec && ct.argb.alpha == color.ct.argb.alpha && ct.argb.red == color.ct.argb.red && ct.argb.green == color.ct.argb.green && ct.argb.blue == color.ct.argb.blue && ct.argb.pad == color.ct.argb.pad);}/*! Returns true if this color has a different RGB value from the color \a color; otherwise returns false.*/bool QColor::operator!=(const QColor &color) const{ return !operator==(color); }/*! Returns the color as a QVariant*/QColor::operator QVariant() const{ return QVariant(QVariant::Color, this);}#ifdef Q_WS_X11/*! Returns true if setNamedColor() is allowed to look up colors in the X11 color database. By default, this function returns false. \note This function is only available on the X11 platform. \sa setAllowX11ColorNames()*/bool QColor::allowX11ColorNames(){ return ::allowX11ColorNames;}/*! Allow setNamedColor() to look up colors in the X11 color database if \a enabled. By default, setNamedColor() does \e not look up colors in the X11 color database. \note This function is only available on the X11 platform. \sa setNamedColor(), allowX11ColorNames()*/void QColor::setAllowX11ColorNames(bool enabled){ ::allowX11ColorNames = enabled;}#endif/*! \internal Marks the color as invalid and sets all components to zero (alpha is set to fully opaque for compatibility with Qt 3).*/void QColor::invalidate(){ cspec = Invalid; ct.argb.alpha = USHRT_MAX; ct.argb.red = 0; ct.argb.green = 0; ct.argb.blue = 0; ct.argb.pad = 0;}#ifdef QT3_SUPPORT/*! Returns the pixel value used by the underlying window system to refer to a color. Use QColormap::pixel() instead. \oldcode QColor myColor; uint pixel = myColor.pixel(screen); \newcode QColormap cmap = QColormap::instance(screen); uint pixel = cmap.pixel(*this); \endcode*/uint QColor::pixel(int screen) const{ QColormap cmap = QColormap::instance(screen); return cmap.pixel(*this);}#endif // QT3_SUPPORT/***************************************************************************** QColor stream functions *****************************************************************************/#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug dbg, const QColor &c){#ifndef Q_BROKEN_DEBUG_STREAM if (!c.isValid()) dbg.nospace() << "QColor(Invalid)"; else if (c.spec() == QColor::Rgb) dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ")"; else if (c.spec() == QColor::Hsv) dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ")"; else if (c.spec() == QColor::Cmyk) dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", " << c.blackF()<< ")"; return dbg.space();#else qWarning("This compiler doesn't support streaming QColor to QDebug"); return dbg; Q_UNUSED(c);#endif}#endif#ifndef QT_NO_DATASTREAM/*! \fn QDataStream &operator<<(QDataStream &stream, const QColor &color) \relates QColor Writes the \a color to the \a stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &stream, const QColor &color){ if (stream.version() < 7) { quint32 p = (quint32)color.rgb(); if (stream.version() == 1) // Swap red and blue p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00); return stream << p; } qint8 s = color.cspec; quint16 a = color.ct.argb.alpha; quint16 r = color.ct.argb.red; quint16 g = color.ct.argb.green; quint16 b = color.ct.argb.blue; quint16 p = color.ct.argb.pad; stream << s; stream << a; stream << r; stream << g; stream << b; stream << p; return stream;}/*! \fn QDataStream &operator>>(QDataStream &stream, QColor &color) \relates QColor Reads the \a color from the \a stream. \sa { Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &stream, QColor &color){ if (stream.version() < 7) { quint32 p; stream >> p; if (stream.version() == 1) // Swap red and blue p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00); color.setRgb(p); return stream; } qint8 s; quint16 a, r, g, b, p; stream >> s; stream >> a; stream >> r; stream >> g; stream >> b; stream >> p; color.cspec = QColor::Spec(s); color.ct.argb.alpha = a; color.ct.argb.red = r; color.ct.argb.green = g; color.ct.argb.blue = b; color.ct.argb.pad = p; return stream;}#endif/***************************************************************************** QColor global functions (documentation only) *****************************************************************************//*! \fn int qRed(QRgb rgb) \relates QColor Returns the red component of the ARGB quadruplet \a rgb. \sa qRgb(), QColor::red()*//*! \fn int qGreen(QRgb rgb) \relates QColor Returns the green component of the ARGB quadruplet \a rgb. \sa qRgb(), QColor::green()*//*! \fn int qBlue(QRgb rgb) \relates QColor Returns the blue component of the ARGB quadruplet \a rgb. \sa qRgb(), QColor::blue()*//*! \fn int qAlpha(QRgb rgba) \relates QColor Returns the alpha component of the ARGB quadruplet \a rgba. \sa qRgb(), QColor::alpha()*//*! \fn QRgb qRgb(int r, int g, int b) \relates QColor Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}). \sa qRgba(), qRed(), qGreen(), qBlue()*//*! \fn QRgb qRgba(int r, int g, int b, int a) \relates QColor Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}). \sa qRgb(), qRed(), qGreen(), qBlue()*//*! \fn int qGray(int r, int g, int b) \relates QColor Returns a gray value (0 to 255) from the (\a r, \a g, \a b) triplet. The gray value is calculated using the formula (\a r * 11 + \a g * 16 + \a b * 5)/32.*//*! \fn int qGray(QRgb rgb) \overload \relates QColor Returns a gray value (0 to 255) from the given ARGB quadruplet \a rgb. The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32. Note that the alpha-channel is ignored.*//*! \fn QColor::QColor(int x, int y, int z, Spec colorSpec) Use one of the other QColor constructors, or one of the static convenience functions, instead.*//*! \fn QColor::rgb(int *r, int *g, int *b) const Use getRgb() instead.*//*! \fn QColor::hsv(int *h, int *s, int *v) const Use getHsv() instead.*//*! \fn QColor QColor::convertTo(Spec colorSpec) const Creates a copy of \e this color in the format specified by \a colorSpec. \sa spec(), toCmyk(), toHsv(), toRgb(), isValid()*//*! \typedef QRgb \relates QColor An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int. Note that the type also holds a value for the alpha-channel. The default alpha channel is \c ff, i.e opaque. For more information, see the \l {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section. \sa QColor::rgb(), QColor::rgba()*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -