📄 qcolor.cpp
字号:
color.ct.argb.red = qRound((1.0 - (c * (1.0 - k) + k)) * USHRT_MAX); color.ct.argb.green = qRound((1.0 - (m * (1.0 - k) + k)) * USHRT_MAX); color.ct.argb.blue = qRound((1.0 - (y * (1.0 - k) + k)) * USHRT_MAX); break; } default: break; } return color;}#define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )#define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )/*! Creates and returns an HSV QColor based on this color. \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}*/QColor QColor::toHsv() const{ if (!isValid()) return *this; if (cspec != Rgb) return toRgb().toHsv(); QColor color; color.cspec = Hsv; color.ct.ahsv.alpha = ct.argb.alpha; color.ct.ahsv.pad = 0; const qreal r = ct.argb.red / qreal(USHRT_MAX); const qreal g = ct.argb.green / qreal(USHRT_MAX); const qreal b = ct.argb.blue / qreal(USHRT_MAX); const qreal max = Q_MAX_3(r, g, b); const qreal min = Q_MIN_3(r, g, b); const qreal delta = max - min; color.ct.ahsv.value = qRound(max * USHRT_MAX); if (qFuzzyCompare(delta, qreal(0.0))) { // achromatic case, hue is undefined color.ct.ahsv.hue = USHRT_MAX; color.ct.ahsv.saturation = 0; } else { // chromatic case qreal hue = 0; color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX); if (qFuzzyCompare(r, max)) { hue = ((g - b) /delta); } else if (qFuzzyCompare(g, max)) { hue = (2.0 + (b - r) / delta); } else if (qFuzzyCompare(b, max)) { hue = (4.0 + (r - g) / delta); } else { Q_ASSERT_X(false, "QColor::toHsv", "internal error"); } hue *= 60.0; if (hue < 0.0) hue += 360.0; color.ct.ahsv.hue = qRound(hue * 100); } return color;}/*! Creates and returns a CMYK QColor based on this color. \sa fromCmyk(), convertTo(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/QColor QColor::toCmyk() const{ if (!isValid()) return *this; if (cspec != Rgb) return toRgb().toCmyk(); QColor color; color.cspec = Cmyk; color.ct.acmyk.alpha = ct.argb.alpha; // rgb -> cmy const qreal r = ct.argb.red / qreal(USHRT_MAX); const qreal g = ct.argb.green / qreal(USHRT_MAX); const qreal b = ct.argb.blue / qreal(USHRT_MAX); qreal c = 1.0 - r; qreal m = 1.0 - g; qreal y = 1.0 - b; // cmy -> cmyk const qreal k = qMin(c, qMin(m, y)); if (!qFuzzyCompare(k,1)) { c = (c - k) / (1.0 - k); m = (m - k) / (1.0 - k); y = (y - k) / (1.0 - k); } 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;}QColor QColor::convertTo(QColor::Spec colorSpec) const{ if (colorSpec == cspec) return *this; switch (colorSpec) { case Rgb: return toRgb(); case Hsv: return toHsv(); case Cmyk: return toCmyk(); case Invalid: break; } return QColor(); // must be invalid}/*! Static convenience function that returns a QColor constructed from the given QRgb value \a rgb. Note that the alpha component of \a rgb is ignored (i.e. it is automatically set to 255), use the fromRgba() function to include the alpha-channel specified by the given QRgb value. \sa fromRgba(), fromRgbF(), toRgb(), isValid()*/QColor QColor::fromRgb(QRgb rgb){ return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));}/*! Static convenience function that returns a QColor constructed from the given QRgb value \a rgba. Note that unlike the fromRgb() function, the alpha-channel specified by the given QRgb value is included. \sa fromRgb(), isValid()*/QColor QColor::fromRgba(QRgb rgba){ return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));}/*! Static convenience function that returns a QColor constructed from the RGB color values, \a r (red), \a g (green), \a b (blue), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0-255. \sa toRgb(), fromRgbF(), isValid()*/QColor QColor::fromRgb(int r, int g, int b, int a){ if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255) { qWarning("QColor::fromRgb: RGB parameters out of range"); return QColor(); } QColor color; color.cspec = Rgb; color.ct.argb.alpha = a * 0x101; color.ct.argb.red = r * 0x101; color.ct.argb.green = g * 0x101; color.ct.argb.blue = b * 0x101; color.ct.argb.pad = 0; return color;}/*! Static convenience function that returns a QColor constructed from the RGB color values, \a r (red), \a g (green), \a b (blue), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0.0-1.0. \sa fromRgb(), toRgb(), isValid()*/QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a){ if (r < 0.0 || r > 1.0 || g < 0.0 || g > 1.0 || b < 0.0 || b > 1.0 || a < 0.0 || a > 1.0) { qWarning("QColor::fromRgbF: RGB parameters out of range"); return QColor(); } QColor color; color.cspec = Rgb; color.ct.argb.alpha = qRound(a * USHRT_MAX); color.ct.argb.red = qRound(r * USHRT_MAX); color.ct.argb.green = qRound(g * USHRT_MAX); color.ct.argb.blue = qRound(b * USHRT_MAX); color.ct.argb.pad = 0; return color;}/*! Static convenience function that returns a QColor constructed from the HSV color values, \a h (hue), \a s (saturation), \a v (value), and \a a (alpha-channel, i.e. transparency). The value of \a s, \a v, and \a a must all be in the range 0-255; the value of \a h must be in the range 0-359. \sa toHsv(), fromHsvF(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}*/QColor QColor::fromHsv(int h, int s, int v, int a){ if (((h < 0 || h >= 360) && h != -1) || s < 0 || s > 255 || v < 0 || v > 255 || a < 0 || a > 255) { qWarning("QColor::fromHsv: HSV parameters out of range"); return QColor(); } QColor color; color.cspec = Hsv; color.ct.ahsv.alpha = a * 0x101; color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100; color.ct.ahsv.saturation = s * 0x101; color.ct.ahsv.value = v * 0x101; color.ct.ahsv.pad = 0; return color;}/*! \overload Static convenience function that returns a QColor constructed from the HSV color values, \a h (hue), \a s (saturation), \a v (value), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0.0-1.0. \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}*/QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a){ if (((h < 0.0 || h > 1.0) && h != -1.0) || (s < 0.0 || s > 1.0) || (v < 0.0 || v > 1.0) || (a < 0.0 || a > 1.0)) { qWarning("QColor::fromHsvF: HSV parameters out of range"); return QColor(); } QColor color; color.cspec = Hsv; color.ct.ahsv.alpha = qRound(a * USHRT_MAX); color.ct.ahsv.hue = h == -1.0 ? USHRT_MAX : qRound(h * 36000); color.ct.ahsv.saturation = qRound(s * USHRT_MAX); color.ct.ahsv.value = qRound(v * USHRT_MAX); color.ct.ahsv.pad = 0; return color;}/*! Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the cyan, magenta, yellow, black, and alpha-channel (transparency) components of the color's CMYK value. Note that the components can be retrieved individually using the cyan(), magenta(), yellow(), black() and alpha() functions. \sa setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a){ if (!c || !m || !y || !k) return; if (cspec != Invalid && cspec != Cmyk) { toCmyk().getCmyk(c, m, y, k, a); return; } *c = ct.acmyk.cyan >> 8; *m = ct.acmyk.magenta >> 8; *y = ct.acmyk.yellow >> 8; *k = ct.acmyk.black >> 8; if (a) *a = ct.acmyk.alpha >> 8;}/*! Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the cyan, magenta, yellow, black, and alpha-channel (transparency) components of the color's CMYK value. Note that the components can be retrieved individually using the cyanF(), magentaF(), yellowF(), blackF() and alphaF() functions. \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a){ if (!c || !m || !y || !k) return; if (cspec != Invalid && cspec != Cmyk) { toCmyk().getCmykF(c, m, y, k, a); return; } *c = ct.acmyk.cyan / qreal(USHRT_MAX); *m = ct.acmyk.magenta / qreal(USHRT_MAX); *y = ct.acmyk.yellow / qreal(USHRT_MAX); *k = ct.acmyk.black / qreal(USHRT_MAX); if (a) *a = ct.acmyk.alpha / qreal(USHRT_MAX);}/*! Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow), \a k (black), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0-255. \sa getCmyk(), setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/void QColor::setCmyk(int c, int m, int y, int k, int a){ if (c < 0 || c > 255 || m < 0 || m > 255 || y < 0 || y > 255 || k < 0 || k > 255 || a < 0 || a > 255) { qWarning("QColor::setCmyk: CMYK parameters out of range"); return; } cspec = Cmyk; ct.acmyk.alpha = a * 0x101; ct.acmyk.cyan = c * 0x101; ct.acmyk.magenta = m * 0x101; ct.acmyk.yellow = y * 0x101; ct.acmyk.black = k * 0x101;}/*! \overload Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow), \a k (black), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0.0-1.0. \sa getCmykF() setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/void QColor::setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a){ if (c < 0.0 || c > 1.0 || m < 0.0 || m > 1.0 || y < 0.0 || y > 1.0 || k < 0.0 || k > 1.0 || a < 0.0 || a > 1.0) { qWarning("QColor::setCmykF: CMYK parameters out of range"); return; } cspec = Cmyk; ct.acmyk.alpha = qRound(a * USHRT_MAX); ct.acmyk.cyan = qRound(c * USHRT_MAX); ct.acmyk.magenta = qRound(m * USHRT_MAX); ct.acmyk.yellow = qRound(y * USHRT_MAX); ct.acmyk.black = qRound(k * USHRT_MAX);}/*! Static convenience function that returns a QColor constructed from the given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k (black), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0-255. \sa toCmyk(), fromCmykF(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/QColor QColor::fromCmyk(int c, int m, int y, int k, int a){ if (c < 0 || c > 255 || m < 0 || m > 255 || y < 0 || y > 255 || k < 0 || k > 255 || a < 0 || a > 255) { qWarning("QColor::fromCmyk: CMYK parameters out of range"); return QColor(); } QColor color; color.cspec = Cmyk; color.ct.acmyk.alpha = a * 0x101; color.ct.acmyk.cyan = c * 0x101; color.ct.acmyk.magenta = m * 0x101; color.ct.acmyk.yellow = y * 0x101; color.ct.acmyk.black = k * 0x101; return color;}/*! \overload Static convenience function that returns a QColor constructed from the given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k (black), and \a a (alpha-channel, i.e. transparency). All the values must be in the range 0.0-1.0. \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}*/QColor QColor::fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a){ if (c < 0.0 || c > 1.0 || m < 0.0 || m > 1.0 || y < 0.0 || y > 1.0 || k < 0.0 || k > 1.0 || a < 0.0 || a > 1.0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -