QColor ImageUtilities::computeAverageColor(const QImage& image) { long int rtotal = 0, gtotal = 0, btotal = 0; float total = 0.0f; // always sample 100x100 pixels int samplePoints = 100; int stepX = std::max(image.width() / samplePoints, 1); int stepY = std::max(image.height() / samplePoints, 1); for (int y = 0; y < image.height(); y += stepY) { for (int x = 0; x < image.width(); x += stepX) { QColor color = QColor::fromRgba(image.pixel(x, y)); float saturation = (qMax (color.red(), qMax (color.green(), color.blue())) - qMin (color.red(), qMin (color.green(), color.blue()))) / 255.0f; float relevance = .1 + .9 * (color.alpha() / 255.0f) * saturation; rtotal += (unsigned char) (color.red() * relevance); gtotal += (unsigned char) (color.green() * relevance); btotal += (unsigned char) (color.blue() * relevance); total += relevance * 255; } } QColor hsv = QColor::fromRgbF(rtotal / total, gtotal / total, btotal / total).toHsv(); /* Background color is the base color with 0.90f HSV value */ hsv.setHsvF(hsv.hueF(), (hsv.saturationF() > .15f) ? 0.65f : hsv.saturationF(), 0.90f); return hsv; }
void HueSlider::setColor(const QColor& color) { p->saturation = color.saturationF(); p->value = color.valueF(); p->updateGradient(); setColorHue(color.hueF()); }
QVariantMap Core::colorToHsv(const QColor &color) { QVariantMap map; map["h"] = color.hueF(); map["s"] = color.saturationF(); map["v"] = color.valueF(); return map; }
void QtGradientStopsControllerPrivate::slotChangeAlpha(int color) { QColor c = m_ui->alphaColorLine->color(); if (m_ui->hsvRadioButton->isChecked()) c.setHsvF(c.hueF(), c.saturationF(), c.valueF(), (qreal)color / 255); else c.setAlpha(color); slotChangeAlpha(c); }
double ColorFilterStrategyHue::pixelToZeroToOne (const QColor &pixel, QRgb /* rgbBackground */) const { double s = pixel.hueF(); if (s < 0) { // Color is achromatic (r=g=b) so it has no hue } return s; }
void ColorPickerItem::setColor(const QColor & color) { m_hue = color.hueF(); m_sat = color.saturationF(); m_val = color.valueF(); if (m_hue < 0.0) m_hue = 0.0; regenHueSatPixmap(); regenValPixmap(); update(); }
void Color_Wheel::setColor(QColor c) { qreal oldh = huem; huem = c.hueF(); if ( huem < 0 ) huem = 0; sat = c.saturationF(); val = c.valueF(); if ( !qFuzzyCompare(oldh+1,huem+1) ) render_rectangle(); update(); emit colorChanged(c); }
void Scene_c3t3_item::compute_color_map(const QColor& c) { typedef Indices::size_type size_type; size_type nb_domains = indices_.size(); size_type i = 0; for (Indices::iterator it = indices_.begin(), end = indices_.end(); it != end; ++it, ++i) { double hue = c.hueF() + 1. / nb_domains * i; if (hue > 1) { hue -= 1.; } d->colors[*it] = QColor::fromHsvF(hue, c.saturationF(), c.valueF()); } }
void KnotRendererBatch::themeChangedHandler() { /* * We exlude colors that are too dark (but light colors are probably OK) * lighter() multiplies a factor to the value, so it's not good enough to deal with potential pure black * Force the value to be at least 0.2 seems to make more sense */ QColor color = Plasma::Theme::defaultTheme()->color(Plasma::Theme::ViewBackgroundColor); if (color.valueF() < 0.2) color.setHsvF(color.hueF(), color.saturationF(), 0.2); emit colorRequest(color); geometryChangedHandler(); }
void QtGradientStopsControllerPrivate::slotChangeHue(const QColor &color) { QtGradientStop *stop = m_model->currentStop(); if (!stop) return; m_model->changeStop(stop, color); QList<QtGradientStop *> stops = m_model->selectedStops(); QListIterator<QtGradientStop *> itStop(stops); while (itStop.hasNext()) { QtGradientStop *s = itStop.next(); if (s != stop) { QColor c = s->color(); if (m_ui->hsvRadioButton->isChecked()) c.setHsvF(color.hueF(), c.saturationF(), c.valueF(), c.alphaF()); else c.setRgbF(color.redF(), c.greenF(), c.blueF(), c.alphaF()); m_model->changeStop(s, c); } } }
QString ColorRangeBase::toString(const QColor &clr, ColorRangeBase::ColorModel clrModel) { QString color; switch(clrModel){ case ColorRangeBase::cmRGBA: color += QString("RGBA(%1 %2 %3 %4)").arg(clr.redF(),0,'f',2).arg(clr.greenF(),0,'f',2).arg(clr.blueF(),0,'f',2).arg(clr.alphaF(),0,'f',2); break; case ColorRangeBase::cmHSLA: color += QString("HSLA(%1 %2 %3 %4)").arg(clr.hueF(),0,'f',2).arg(clr.saturationF(),0,'f',2).arg(clr.lightnessF(),0,'f',2).arg(clr.alphaF(),0,'f',2); break; case ColorRangeBase::cmCYMKA: color += QString("CMYKA(%1 %2 %3 %4 %5)").arg(clr.cyanF(),0,'f',2).arg(clr.magentaF(),0,'f',2).arg(clr.yellowF(),0,'f',2).arg(clr.blackF(),0,'f',2).arg(clr.alphaF(),0,'f',2); break; case ColorRangeBase::cmGREYSCALE: color += QString("GREY(%1)").arg(clr.redF(),0,'f',2); break; default: break; } return color; }
QColor TreeItemModel::adjustColorToContrastWithWhite(QColor color) const { // use some tricks in HSV space to get contrasting colors while // keeping the original color as much as possible. // // for colors: strengthen the saturation: s' = s+(1-s)/2 // for for grayscale: lower value, or give up for whites: // if s<0.1: v = max(0.7, v) double h = color.hueF(); double s = color.hsvSaturationF(); double v = color.valueF(); double isChromatic = s > 0.1; // our definition if (isChromatic) { s = s+(1.0-s)/2; } else { v = std::min(0.6, v); } return QColor::fromHsvF(h,s,v); }
void tst_QColor::setHsv() { QColor color; for (int A = 0; A <= USHRT_MAX; ++A) { { // 0-255 int a = A >> 8; color.setHsv(0, 0, 0, a); QCOMPARE(color.alpha(), a); int h, s, v, a2; color.getHsv(&h, &s, &v, &a2); QCOMPARE(a2, a); } { // 0.0-1.0 qreal a = A / qreal(USHRT_MAX); color.setHsvF(0.0, 0.0, 0.0, a); QCOMPARE(color.alphaF(), a); qreal h, s, v, a2; color.getHsvF(&h, &s, &v, &a2); QCOMPARE(a2, a); } } for (int H = 0; H < 36000; ++H) { { // 0-255 int h = H / 100; color.setHsv(h, 0, 0, 0); QCOMPARE(color.hue(), h); int h2, s, v, a; color.getHsv(&h2, &s, &v, &a); QCOMPARE(h2, h); } { // 0.0-1.0 qreal h = H / 36000.0; color.setHsvF(h, 0.0, 0.0, 0.0); QCOMPARE(color.hueF(), h); qreal h2, s, v, a; color.getHsvF(&h2, &s, &v, &a); QCOMPARE(h2, h); } } for (int S = 0; S <= USHRT_MAX; ++S) { { // 0-255 int s = S >> 8; color.setHsv(0, s, 0, 0); QCOMPARE(color.saturation(), s); int h, s2, v, a; color.getHsv(&h, &s2, &v, &a); QCOMPARE(s2, s); } { // 0.0-1.0 qreal s = S / qreal(USHRT_MAX); color.setHsvF(0.0, s, 0.0, 0.0); QCOMPARE(color.saturationF(), s); qreal h, s2, v, a; color.getHsvF(&h, &s2, &v, &a); QCOMPARE(s2, s); } } for (int V = 0; V <= USHRT_MAX; ++V) { { // 0-255 int v = V >> 8; color.setHsv(0, 0, v, 0); QCOMPARE(color.value(), v); int h, s, v2, a; color.getHsv(&h, &s, &v2, &a); QCOMPARE(v2, v); } { // 0.0-1.0 qreal v = V / qreal(USHRT_MAX); color.setHsvF(0.0, 0.0, v, 0.0); QCOMPARE(color.valueF(), v); qreal h, s, v2, a; color.getHsvF(&h, &s, &v2, &a); QCOMPARE(v2, v); } } }
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; }
void KisColorSelectorSimple::setColor(const QColor &c) { switch (m_parameter) { case KisColorSelector::SL: m_lastClickPos.setX(c.hslSaturationF()); m_lastClickPos.setY(1.-c.lightnessF()); emit paramChanged(-1, -1, -1, c.hslSaturationF(), c.lightnessF()); break; case KisColorSelector::LH: m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.)); m_lastClickPos.setY(1.-c.lightnessF()); emit paramChanged(c.hueF(), -1, -1, -1, c.lightnessF()); break; case KisColorSelector::SV: m_lastClickPos.setX(c.saturationF()); m_lastClickPos.setY(1-c.valueF()); emit paramChanged(-1, c.saturationF(), c.valueF(), -1, -1); break; case KisColorSelector::SV2: { qreal xRel = c.hsvSaturationF(); qreal yRel = 0.5; if(xRel != 1.0) yRel = 1.0 - qBound<qreal>(0.0, (c.valueF() - xRel) / (1.0 - xRel), 1.0); m_lastClickPos.setX(xRel); m_lastClickPos.setY(yRel); emit paramChanged(-1, -1, -1, xRel, yRel); break; } case KisColorSelector::VH: m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.)); m_lastClickPos.setY(c.valueF()); emit paramChanged(c.hueF(), -1, c.valueF(), -1, -1); break; case KisColorSelector::hsvSH: m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.)); m_lastClickPos.setY(1-c.saturationF()); emit paramChanged(c.hueF(), c.saturationF(), -1, -1, -1); break; case KisColorSelector::hslSH: m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.)); m_lastClickPos.setY(1-c.hslSaturationF()); emit paramChanged(c.hueF(), -1, -1, c.hslSaturationF(), -1); break; case KisColorSelector::L: m_lastClickPos.setX(c.lightnessF()); emit paramChanged(-1, -1, -1, -1, c.lightnessF()); break; case KisColorSelector::V: m_lastClickPos.setX(c.valueF()); emit paramChanged(-1, -1, c.valueF(), -1, -1); break; case KisColorSelector::hsvS: m_lastClickPos.setX(c.saturationF()); emit paramChanged(-1, c.saturationF(), -1, -1, -1); break; case KisColorSelector::hslS: m_lastClickPos.setX(c.hslSaturationF()); emit paramChanged(-1, -1, -1, c.hslSaturationF(), -1); break; case KisColorSelector::H: m_lastClickPos.setX(qBound<qreal>(0., c.hueF(), 1.)); emit paramChanged(c.hueF(), -1, -1, -1, -1); break; default: Q_ASSERT(false); break; } emit update(); }