void QgsColorBox::setColor( const QColor &color, const bool emitSignals ) { //check if we need to redraw the box image if ( mComponent == QgsColorWidget::Red && mCurrentColor.red() != color.red() ) { mDirty = true; } else if ( mComponent == QgsColorWidget::Green && mCurrentColor.green() != color.green() ) { mDirty = true; } else if ( mComponent == QgsColorWidget::Blue && mCurrentColor.blue() != color.blue() ) { mDirty = true; } else if ( mComponent == QgsColorWidget::Hue && color.hsvHue() >= 0 && hue() != color.hsvHue() ) { mDirty = true; } else if ( mComponent == QgsColorWidget::Saturation && mCurrentColor.hsvSaturation() != color.hsvSaturation() ) { mDirty = true; } else if ( mComponent == QgsColorWidget::Value && mCurrentColor.value() != color.value() ) { mDirty = true; } QgsColorWidget::setColor( color, emitSignals ); }
void ColorHistogram(double* result, int d1, int d2, int d3, QString filename) { int i, j; QImage image; QRgb rgb; QColor color; for (i = 0; i < d1 * d2 * d3; i++) result[i] = 0; image.load(filename); for (i = 0; i < image.width(); i++) for (j = 0; j < image.height(); j++) { rgb = image.pixel(i, j); color = QColor::fromRgba(rgb); result[color.hsvHue() / (360 / d1) * d2 * d3 + color.hsvSaturation() / (256 / d2) * d3 + color.value() / (256 / d3)]++; } for (i = 1; i < d1 * d2 * d3; i++) result[i] += result[i - 1]; }
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 ColorInspector::setColor(QColor newColor) { // this is a UI update function, never emit any signals // grab the color from color manager, and then update itself, that's it. // compare under the same color spec newColor = (isRgbColors) ? newColor.toRgb() : newColor.toHsv(); if (newColor == mCurrentColor) { return; } if(isRgbColors) { QSignalBlocker b1(ui->red_slider); QSignalBlocker b2(ui->green_slider); QSignalBlocker b3(ui->blue_slider); QSignalBlocker b4(ui->alpha_slider); ui->red_slider->setRgb(newColor); ui->green_slider->setRgb(newColor); ui->blue_slider->setRgb(newColor); ui->alpha_slider->setRgb(newColor); QSignalBlocker b5(ui->RedspinBox); QSignalBlocker b6(ui->GreenspinBox); QSignalBlocker b7(ui->BluespinBox); QSignalBlocker b8(ui->AlphaspinBox); ui->RedspinBox->setValue(newColor.red()); ui->GreenspinBox->setValue(newColor.green()); ui->BluespinBox->setValue(newColor.blue()); ui->AlphaspinBox->setValue(newColor.alpha()); } else { QSignalBlocker b1(ui->red_slider); QSignalBlocker b2(ui->green_slider); QSignalBlocker b3(ui->blue_slider); QSignalBlocker b4(ui->alpha_slider); ui->red_slider->setHsv(newColor); ui->green_slider->setHsv(newColor); ui->blue_slider->setHsv(newColor); ui->alpha_slider->setHsv(newColor); QSignalBlocker b5(ui->RedspinBox); QSignalBlocker b6(ui->GreenspinBox); QSignalBlocker b7(ui->BluespinBox); QSignalBlocker b8(ui->AlphaspinBox); ui->RedspinBox->setValue(newColor.hsvHue()); ui->GreenspinBox->setValue(qRound(newColor.hsvSaturation() / 2.55)); ui->BluespinBox->setValue(qRound(newColor.value() / 2.55)); ui->AlphaspinBox->setValue(qRound(newColor.alpha() / 2.55)); } mCurrentColor = newColor; QPalette p1 = ui->colorWrapper->palette(), p2 = ui->color->palette(); p1.setBrush(QPalette::Background, QBrush(QImage(":/background/checkerboard.png"))); p2.setColor(QPalette::Background, mCurrentColor); ui->colorWrapper->setPalette(p1); ui->color->setPalette(p2); update(); }
ColorVector MoodbarRenderer::Colors(const QByteArray& data, MoodbarStyle style, const QPalette& palette) { const int samples = data.size() / 3; // Set some parameters based on the moodbar style StyleProperties properties; switch (style) { case Style_Angry: properties = StyleProperties(samples / 360 * 9, 45, -45, 200, 100); break; case Style_Frozen: properties = StyleProperties(samples / 360 * 1, 140, 160, 50, 100); break; case Style_Happy: properties = StyleProperties(samples / 360 * 2, 0, 359, 150, 250); break; case Style_Normal: properties = StyleProperties(samples / 360 * 3, 0, 359, 100, 100); break; case Style_SystemPalette: default: { const QColor highlight_color( palette.color(QPalette::Active, QPalette::Highlight)); properties.threshold_ = samples / 360 * 3; properties.range_start_ = (highlight_color.hsvHue() - 20 + 360) % 360; properties.range_delta_ = 20; properties.sat_ = highlight_color.hsvSaturation(); properties.val_ = highlight_color.value() / 2; } } const unsigned char* data_p = reinterpret_cast<const unsigned char*>(data.constData()); int hue_distribution[360]; int total = 0; memset(hue_distribution, 0, arraysize(hue_distribution)); ColorVector colors; // Read the colors, keeping track of some histograms for (int i = 0; i < samples; ++i) { QColor color; color.setRed(int(*data_p++)); color.setGreen(int(*data_p++)); color.setBlue(int(*data_p++)); colors << color; const int hue = qMax(0, color.hue()); if (hue_distribution[hue]++ == properties.threshold_) { total++; } } total = qMax(total, 1); // Remap the hue values to be between rangeStart and // rangeStart + rangeDelta. Every time we see an input hue // above the threshold, increment the output hue by // (1/total) * rangeDelta. for (int i = 0, n = 0; i < 360; i++) { hue_distribution[i] = ((hue_distribution[i] > properties.threshold_ ? n++ : n) * properties.range_delta_ / total + properties.range_start_) % 360; } // Now huedist is a hue mapper: huedist[h] is the new hue value // for a bar with hue h for (ColorVector::iterator it = colors.begin(); it != colors.end(); ++it) { const int hue = qMax(0, it->hue()); *it = QColor::fromHsv( qBound(0, hue_distribution[hue], 359), qBound(0, it->saturation() * properties.sat_ / 100, 255), qBound(0, it->value() * properties.val_ / 100, 255)); } return colors; }