Exemplo n.º 1
0
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 ) ) );
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
0
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 );
}
Exemplo n.º 4
0
// 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());
            }
        }
    }
}
Exemplo n.º 5
0
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);
        }
    }
}
Exemplo n.º 6
0
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 );
}
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();
}