📄 qcolor.cpp
字号:
qreal QColor::alphaF() const{ return ct.argb.alpha / qreal(USHRT_MAX); }/*! Sets the alpha of this color to \a alpha. qreal alpha is specified in the range 0.0-1.0. \sa alphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}*/void QColor::setAlphaF(qreal alpha){ QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha); qreal tmp = alpha * USHRT_MAX; ct.argb.alpha = qRound(tmp);}/*! Returns the red color component of this color. \sa setRed(), redF(), getRgb()*/int QColor::red() const{ if (cspec != Invalid && cspec != Rgb) return toRgb().red(); return ct.argb.red >> 8;}/*! Sets the red color component of this color to \a red. Int components are specified in the range 0-255. \sa red(), redF(), setRgb()*/void QColor::setRed(int red){ QCOLOR_INT_RANGE_CHECK("QColor::setRed", red); if (cspec != Rgb) setRgb(red, green(), blue(), alpha()); else ct.argb.red = red * 0x101;}/*! Returns the green color component of this color. \sa setGreen(), greenF(), getRgb()*/int QColor::green() const{ if (cspec != Invalid && cspec != Rgb) return toRgb().green(); return ct.argb.green >> 8;}/*! Sets the green color component of this color to \a green. Int components are specified in the range 0-255. \sa green(), greenF(), setRgb()*/void QColor::setGreen(int green){ QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green); if (cspec != Rgb) setRgb(red(), green, blue(), alpha()); else ct.argb.green = green * 0x101;}/*! Returns the blue color component of this color. \sa setBlue(), blueF(), getRgb()*/int QColor::blue() const{ if (cspec != Invalid && cspec != Rgb) return toRgb().blue(); return ct.argb.blue >> 8;}/*! Sets the blue color component of this color to \a blue. Int components are specified in the range 0-255. \sa blue(), blueF(), setRgb()*/void QColor::setBlue(int blue){ QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue); if (cspec != Rgb) setRgb(red(), green(), blue, alpha()); else ct.argb.blue = blue * 0x101;}/*! Returns the red color component of this color. \sa setRedF(), red(), getRgbF()*/qreal QColor::redF() const{ if (cspec != Invalid && cspec != Rgb) return toRgb().redF(); return ct.argb.red / qreal(USHRT_MAX);}/*! Sets the red color component of this color to \a red. Float components are specified in the range 0.0-1.0. \sa redF(), red(), setRgbF()*/void QColor::setRedF(qreal red){ QCOLOR_REAL_RANGE_CHECK("QColor::setRedF", red); if (cspec != Rgb) setRgbF(red, greenF(), blueF(), alphaF()); else ct.argb.red = qRound(red * USHRT_MAX);}/*! Returns the green color component of this color. \sa setGreenF(), green(), getRgbF()*/qreal QColor::greenF() const{ if (cspec != Invalid && cspec != Rgb) return toRgb().greenF(); return ct.argb.green / qreal(USHRT_MAX);}/*! Sets the green color component of this color to \a green. Float components are specified in the range 0.0-1.0. \sa greenF(), green(), setRgbF()*/void QColor::setGreenF(qreal green){ QCOLOR_REAL_RANGE_CHECK("QColor::setGreenF", green); if (cspec != Rgb) setRgbF(redF(), green, blueF(), alphaF()); else ct.argb.green = qRound(green * USHRT_MAX);}/*! Returns the blue color component of this color. \sa setBlueF(), blue(), getRgbF()*/qreal QColor::blueF() const{ if (cspec != Invalid && cspec != Rgb) return toRgb().blueF(); return ct.argb.blue / qreal(USHRT_MAX);}/*! Sets the blue color component of this color to \a blue. Float components are specified in the range 0.0-1.0. \sa blueF(), blue(), setRgbF()*/void QColor::setBlueF(qreal blue){ QCOLOR_REAL_RANGE_CHECK("QColor::setBlueF", blue); if (cspec != Rgb) setRgbF(redF(), greenF(), blue, alphaF()); else ct.argb.blue = qRound(blue * USHRT_MAX);}/*! Returns the hue color component of this color. \sa hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}*/int QColor::hue() const{ if (cspec != Invalid && cspec != Hsv) return toHsv().hue(); return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;}/*! Returns the saturation color component of this color. \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}*/int QColor::saturation() const{ if (cspec != Invalid && cspec != Hsv) return toHsv().saturation(); return ct.ahsv.saturation >> 8;}/*! Returns the value color component of this color. \sa valueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}*/int QColor::value() const{ if (cspec != Invalid && cspec != Hsv) return toHsv().value(); return ct.ahsv.value >> 8;}/*! Returns the hue color component of this color. \sa hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}*/qreal QColor::hueF() const{ if (cspec != Invalid && cspec != Hsv) return toHsv().hueF(); return ct.ahsv.hue == USHRT_MAX ? -1.0 : ct.ahsv.hue / 36000.0;}/*! Returns the saturation color component of this color. \sa saturation() getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}*/qreal QColor::saturationF() const{ if (cspec != Invalid && cspec != Hsv) return toHsv().saturationF(); return ct.ahsv.saturation / qreal(USHRT_MAX);}/*! Returns the value color component of this color. \sa value() getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}*/qreal QColor::valueF() const{ if (cspec != Invalid && cspec != Hsv) return toHsv().valueF(); return ct.ahsv.value / qreal(USHRT_MAX);}/*! Returns the cyan color component of this color. \sa cyanF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/int QColor::cyan() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().cyan(); return ct.acmyk.cyan >> 8;}/*! Returns the magenta color component of this color. \sa magentaF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/int QColor::magenta() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().magenta(); return ct.acmyk.magenta >> 8;}/*! Returns the yellow color component of this color. \sa yellowF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/int QColor::yellow() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().yellow(); return ct.acmyk.yellow >> 8;}/*! Returns the black color component of this color. \sa blackF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/int QColor::black() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().black(); return ct.acmyk.black >> 8;}/*! Returns the cyan color component of this color. \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/qreal QColor::cyanF() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().cyanF(); return ct.acmyk.cyan / qreal(USHRT_MAX);}/*! Returns the magenta color component of this color. \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/qreal QColor::magentaF() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().magentaF(); return ct.acmyk.magenta / qreal(USHRT_MAX);}/*! Returns the yellow color component of this color. \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/qreal QColor::yellowF() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().yellowF(); return ct.acmyk.yellow / qreal(USHRT_MAX);}/*! Returns the black color component of this color. \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/qreal QColor::blackF() const{ if (cspec != Invalid && cspec != Cmyk) return toCmyk().blackF(); return ct.acmyk.black / qreal(USHRT_MAX);}/*! Create and returns an RGB QColor based on this color. \sa fromRgb(), convertTo(), isValid()*/QColor QColor::toRgb() const{ if (!isValid() || cspec == Rgb) return *this; QColor color; color.cspec = Rgb; color.ct.argb.alpha = ct.argb.alpha; color.ct.argb.pad = 0; switch (cspec) { case Hsv: { if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) { // achromatic case color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value; break; } // chromatic case const qreal h = ct.ahsv.hue / 6000.; const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX); const qreal v = ct.ahsv.value / qreal(USHRT_MAX); const int i = int(h); const qreal f = h - i; const qreal p = v * (1.0 - s); if (i & 1) { const qreal q = v * (1.0 - (s * f)); switch (i) { case 1: color.ct.argb.red = qRound(q * USHRT_MAX); color.ct.argb.green = qRound(v * USHRT_MAX); color.ct.argb.blue = qRound(p * USHRT_MAX); break; case 3: color.ct.argb.red = qRound(p * USHRT_MAX); color.ct.argb.green = qRound(q * USHRT_MAX); color.ct.argb.blue = qRound(v * USHRT_MAX); break; case 5: color.ct.argb.red = qRound(v * USHRT_MAX); color.ct.argb.green = qRound(p * USHRT_MAX); color.ct.argb.blue = qRound(q * USHRT_MAX); break; } } else { const qreal t = v * (1.0 - (s * (1.0 - f))); switch (i) { case 0: color.ct.argb.red = qRound(v * USHRT_MAX); color.ct.argb.green = qRound(t * USHRT_MAX); color.ct.argb.blue = qRound(p * USHRT_MAX); break; case 2: color.ct.argb.red = qRound(p * USHRT_MAX); color.ct.argb.green = qRound(v * USHRT_MAX); color.ct.argb.blue = qRound(t * USHRT_MAX); break; case 4: color.ct.argb.red = qRound(t * USHRT_MAX); color.ct.argb.green = qRound(p * USHRT_MAX); color.ct.argb.blue = qRound(v * USHRT_MAX); break; } } break; } case Cmyk: { const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX); const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX); const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX); const qreal k = ct.acmyk.black / qreal(USHRT_MAX);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -