void tst_QColor::achromaticHslHue() { QColor color = Qt::black; QColor hsl = color.toHsl(); QCOMPARE(hsl.hslHue(), -1); }
bool LIFXLightClient::sendColour() { LIFXPacket *packet = new LIFXPacket(LIFX_SET_LIGHT_COLOR); memcpy(packet->header.target_mac_address, m_addressraw, sizeof(packet->header.target_mac_address)); LIFXSetLightColorPayload *payload = (LIFXSetLightColorPayload*)packet->payloaddata; if(payload != NULL) { QColor color = QColor::fromRgb(red, green, blue); //color.getHsl( *payload->hue, *payload->saturation, *payload->brightness); payload->stream = 0; payload->hue = color.hslHue() *275; payload->saturation = color.hslSaturation()*257; payload->brightness = color.lightness()*655.35; payload->kelvin = kelvin; payload->fade_time = fadetime; ((LIFXGatewayClient*)gatewayclient)->SendPacket(packet); return true; } return false; }
void FlatButton::setIcons(const QIcon& _icon, const QIcon& _checkedIcon, const QIcon& _hoverIcon) { m_icon = _icon; ImageHelper::setIconColor(m_icon, ICON_SIZE, palette().text().color()); m_checkedIcon = _checkedIcon; if (!m_checkedIcon.isNull()) { m_checkedIconHighlight = false; ImageHelper::setIconColor(m_checkedIcon, ICON_SIZE, palette().text().color()); } else { m_checkedIconHighlight = true; m_checkedIcon = _icon; QColor highlightColor = palette().highlight().color(); #ifdef Q_OS_MAC highlightColor.setHsl(highlightColor.hslHue(), highlightColor.hslSaturation(), highlightColor.lightness() - 50); #endif ImageHelper::setIconColor(m_checkedIcon, ICON_SIZE, highlightColor); } m_hoverIcon = _hoverIcon; aboutUpdateIcon(); }
void tst_QColor::setHsl() { QColor color; for (int A = 0; A <= USHRT_MAX; ++A) { { // 0-255 int a = A >> 8; color.setHsl(0, 0, 0, a); QCOMPARE(color.alpha(), a); int h, s, l, a2; color.getHsv(&h, &s, &l, &a2); QCOMPARE(a2, a); } { // 0.0-1.0 qreal a = A / qreal(USHRT_MAX); color.setHslF(0.0, 0.0, 0.0, a); QCOMPARE(color.alphaF(), a); qreal h, s, l, a2; color.getHslF(&h, &s, &l, &a2); QCOMPARE(a2, a); } } for (int H = 0; H < 36000; ++H) { { // 0-255 int h = H / 100; color.setHsl(h, 0, 0, 0); QCOMPARE(color.hslHue(), h); int h2, s, l, a; color.getHsl(&h2, &s, &l, &a); QCOMPARE(h2, h); } { // 0.0-1.0 qreal h = H / 36000.0; color.setHslF(h, 0.0, 0.0, 0.0); QCOMPARE(color.hslHueF(), h); qreal h2, s, l, a; color.getHslF(&h2, &s, &l, &a); QCOMPARE(h2, h); } } for (int S = 0; S <= USHRT_MAX; ++S) { { // 0-255 int s = S >> 8; color.setHsl(0, s, 0, 0); QCOMPARE(color.hslSaturation(), s); int h, s2, l, a; color.getHsl(&h, &s2, &l, &a); QCOMPARE(s2, s); } { // 0.0-1.0 qreal s = S / qreal(USHRT_MAX); color.setHslF(0.0, s, 0.0, 0.0); QCOMPARE(color.hslSaturationF(), s); qreal h, s2, l, a; color.getHslF(&h, &s2, &l, &a); QCOMPARE(s2, s); } } for (int L = 0; L <= USHRT_MAX; ++L) { { // 0-255 int l = L >> 8; color.setHsl(0, 0, l, 0); QCOMPARE(color.lightness(), l); int h, s, l2, a; color.getHsl(&h, &s, &l2, &a); QCOMPARE(l2, l); } { // 0.0-1.0 qreal l = L / qreal(USHRT_MAX); color.setHslF(0.0, 0.0, l, 0.0); QCOMPARE(color.lightnessF(), l); qreal h, s, l2, a; color.getHslF(&h, &s, &l2, &a); QCOMPARE(l2, l); } } }
QString colorToString(const QColor &color, ColorFormat format) { QString ret; QString prefix; QString colorComponents; if (format == ColorFormat::QCssRgbUCharFormat) { prefix = QLatin1String("rgb("); colorComponents = QString::number(color.red()) + QLatin1String(", ") + QString::number(color.green()) + QLatin1String(", ") + QString::number(color.blue()); qreal alpha = color.alphaF(); if (alpha < 1.0) { prefix.insert(3, QLatin1Char('a')); colorComponents += QLatin1String(", ") + colorDoubleToQString(color.alphaF()); } } if (format == ColorFormat::QCssRgbPercentFormat) { int rP = qRound(color.redF() * 100); int gP = qRound(color.greenF() * 100); int bP = qRound(color.blueF() * 100); prefix = QLatin1String("rgb("); colorComponents = QString::number(rP) + QChar::fromLatin1('%') + QLatin1String(", ") + QString::number(gP) + QChar::fromLatin1('%') + QLatin1String(", ") + QString::number(bP) + QChar::fromLatin1('%'); qreal alpha = color.alphaF(); if (alpha < 1.0) { prefix.insert(3, QLatin1Char('a')); colorComponents += QLatin1String(", ") + colorDoubleToQString(alpha); } } else if (format == ColorFormat::QssHsvFormat) { prefix = QLatin1String("hsv("); colorComponents = QString::number(color.hsvHue()) + QLatin1String(", ") + QString::number(color.hsvSaturation()) + QLatin1String(", ") + QString::number(color.value()); int aP = qRound(color.alphaF() * 100); if (aP < 100) { prefix.insert(3, QLatin1Char('a')); colorComponents += QLatin1String(", ") + colorDoubleToQString(aP); } } else if (format == ColorFormat::CssHslFormat) { prefix = QLatin1String("hsl("); int sP = qRound(color.hslSaturationF() * 100); int lP = qRound(color.lightnessF() * 100); colorComponents = QString::number(color.hslHue()) + QLatin1String(", ") + QString::number(sP) + QChar::fromLatin1('%') + QLatin1String(", ") + QString::number(lP) + QChar::fromLatin1('%'); qreal alpha = color.alphaF(); if (alpha < 1.0) { prefix.insert(3, QLatin1Char('a')); colorComponents += QLatin1String(", ") + colorDoubleToQString(color.alphaF()); } } else if (format == ColorFormat::QmlRgbaFormat) { prefix = QLatin1String("Qt.rgba("); colorComponents = colorDoubleToQString(color.redF()) + QLatin1String(", ") + colorDoubleToQString(color.greenF()) + QLatin1String(", ") + colorDoubleToQString(color.blueF()) + QLatin1String(", ") + colorDoubleToQString(color.alphaF()); } else if (format == ColorFormat::QmlHslaFormat) { prefix = QLatin1String("Qt.hsla("); colorComponents = colorDoubleToQString(color.hueF()) + QLatin1String(", ") + colorDoubleToQString(color.saturationF()) + QLatin1String(", ") + colorDoubleToQString(color.lightnessF()) + QLatin1String(", ") + colorDoubleToQString(color.alphaF()); } else if (format == ColorFormat::GlslFormat) { prefix = QLatin1String("vec"); colorComponents = colorDoubleToQString(color.redF()) + QLatin1String(", ") + colorDoubleToQString(color.greenF()) + QLatin1String(", ") + colorDoubleToQString(color.blueF()); qreal alpha = color.alphaF(); if (alpha < 1.0) { prefix.append(QLatin1Char('4')); colorComponents += QLatin1String(", ") + colorDoubleToQString(color.alphaF()); } else { prefix.append(QLatin1Char('3')); } prefix.append(QLatin1Char('(')); } else if (format == ColorFormat::HexFormat) { prefix = QLatin1String("#"); int alpha = color.alpha(); if (alpha < 255) colorComponents.sprintf("%02x%02x%02x%02x", alpha, color.red(), color.green(), color.blue()); else colorComponents.sprintf("%02x%02x%02x", color.red(), color.green(), color.blue()); colorComponents = colorComponents.toUpper(); } Q_ASSERT(!prefix.isNull()); Q_ASSERT(!colorComponents.isNull()); ret = prefix + colorComponents; if (format != ColorFormat::HexFormat) ret += QChar::fromLatin1(')'); Q_ASSERT_X(!ret.isNull(), Q_FUNC_INFO, "The string version of the color is invalid"); return ret; }