void QgsGradientColorRampDialog::plotMouseMove( QPointF point ) { QColor newColor = mStopEditor->selectedStop().color; if ( mCurrentPlotColorComponent == 0 ) newColor = QColor::fromHslF( qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ), newColor.hslSaturationF(), newColor.lightnessF(), newColor.alphaF() ); else if ( mCurrentPlotColorComponent == 1 ) newColor = QColor::fromHslF( newColor.hslHueF(), newColor.hslSaturationF(), qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ), newColor.alphaF() ); else if ( mCurrentPlotColorComponent == 2 ) newColor = QColor::fromHslF( newColor.hslHueF(), qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ), newColor.lightnessF(), newColor.alphaF() ); else if ( mCurrentPlotColorComponent == 3 ) newColor = QColor::fromHslF( newColor.hslHueF(), newColor.hslSaturationF(), newColor.lightnessF(), qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ) ); mStopEditor->setSelectedStopDetails( newColor, qBound( qreal( 0.0 ), point.x(), qreal( 1.0 ) ) ); }
static QColor darkShade(QColor c) { qreal contrast = 0.7; // taken from kcolorscheme for the dark shade qreal darkAmount; if (c.lightnessF() < 0.006) { /* too dark */ darkAmount = 0.02 + 0.40 * contrast; } else if (c.lightnessF() > 0.93) { /* too bright */ darkAmount = -0.06 - 0.60 * contrast; } else { darkAmount = (-c.lightnessF()) * (0.55 + contrast * 0.35); } qreal v = c.lightnessF() + darkAmount; v = v > 0.0 ? (v < 1.0 ? v : 1.0) : 0.0; c.setHsvF(c.hslHueF(), c.hslSaturationF(), v); return c; }
void QgsGradientColorRampDialog::addMarkersForColor( double x, const QColor &color, bool isSelected ) { if ( mPlotHueCheckbox->isChecked() ) addPlotMarker( x, color.hslHueF(), color, isSelected && mCurrentPlotColorComponent == 0 ); if ( mPlotLightnessCheckbox->isChecked() ) addPlotMarker( x, color.lightnessF(), color, isSelected && mCurrentPlotColorComponent == 1 ); if ( mPlotSaturationCheckbox->isChecked() ) addPlotMarker( x, color.hslSaturationF(), color, isSelected && mCurrentPlotColorComponent == 2 ); if ( mPlotAlphaCheckbox->isChecked() ) addPlotMarker( x, color.alphaF(), color, isSelected && mCurrentPlotColorComponent == 3 ); }
// Tints an image with tintColor, while preserving alpha and lightness void StyleHelper::tintImage(QImage &img, const QColor &tintColor) { QPainter p(&img); p.setCompositionMode(QPainter::CompositionMode_Screen); for (int x = 0; x < img.width(); ++x) { for (int y = 0; y < img.height(); ++y) { QRgb rgbColor = img.pixel(x, y); int alpha = qAlpha(rgbColor); QColor c = QColor(rgbColor); if (alpha > 0) { c.toHsl(); qreal l = c.lightnessF(); QColor newColor = QColor::fromHslF(tintColor.hslHueF(), tintColor.hslSaturationF(), l); newColor.setAlpha(alpha); img.setPixel(x, y, newColor.rgba()); } } } }
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); } } }
void QgsGradientColorRampDialog::plotMousePress( QPointF point ) { //find closest part double minDist = 1; mCurrentPlotColorComponent = -1; mCurrentPlotMarkerIndex = -1; // first color for ( int i = 0; i < mRamp.count(); ++i ) { QColor currentCol; double currentOff = 0.0; if ( i == 0 ) { currentOff = 0.0; currentCol = mRamp.color1(); } else if ( i == mRamp.count() - 1 ) { currentOff = 1.0; currentCol = mRamp.color2(); } else { currentOff = mRamp.stops().at( i - 1 ).offset; currentCol = mRamp.stops().at( i - 1 ).color; } double currentDist; if ( mPlotHueCheckbox->isChecked() ) { currentDist = std::pow( point.x() - currentOff, 2.0 ) + std::pow( point.y() - currentCol.hslHueF(), 2.0 ); if ( currentDist < minDist ) { minDist = currentDist; mCurrentPlotColorComponent = 0; mCurrentPlotMarkerIndex = i; } } if ( mPlotLightnessCheckbox->isChecked() ) { currentDist = std::pow( point.x() - currentOff, 2.0 ) + std::pow( point.y() - currentCol.lightnessF(), 2.0 ); if ( currentDist < minDist ) { minDist = currentDist; mCurrentPlotColorComponent = 1; mCurrentPlotMarkerIndex = i; } } if ( mPlotSaturationCheckbox->isChecked() ) { currentDist = std::pow( point.x() - currentOff, 2.0 ) + std::pow( point.y() - currentCol.hslSaturationF(), 2.0 ); if ( currentDist < minDist ) { minDist = currentDist; mCurrentPlotColorComponent = 2; mCurrentPlotMarkerIndex = i; } } if ( mPlotAlphaCheckbox->isChecked() ) { currentDist = std::pow( point.x() - currentOff, 2.0 ) + std::pow( point.y() - currentCol.alphaF(), 2.0 ); if ( currentDist < minDist ) { minDist = currentDist;; mCurrentPlotColorComponent = 3; mCurrentPlotMarkerIndex = i; } } } // watch out - selected stop index may differ if stops in editor are out of order!!! if ( mCurrentPlotMarkerIndex >= 0 ) mStopEditor->selectStop( mCurrentPlotMarkerIndex ); }