Example #1
0
void ColorWheel::drawPicker(const QColor& color)
{
    QPainter painter(&mWheelPixmap);
    painter.setRenderHint(QPainter::Antialiasing);
    int ellipseSize = 9;

    QPoint squareTopLeft = mSquareRect.topLeft();
    QSize squareSize = mSquareRect.size();

    qreal S = color.hsvSaturationF() * (squareSize.width()-1);
    qreal V = (squareSize.height() - (color.valueF() * squareSize.height()-1));

    QPen pen;
    pen.setWidth(1);
    if (color.hsvSaturation() > 30 || color.value() < 50)
    {
        pen.setColor(Qt::white);
    }
    painter.setPen(pen);

    QTransform transform;
    transform.translate(-ellipseSize/2,-ellipseSize/2);
    transform.translate(squareTopLeft.x(),squareTopLeft.y()-1);
    painter.setTransform(transform);
    painter.drawEllipse(static_cast<int>(S), static_cast<int>(V), ellipseSize, ellipseSize);
}
Example #2
0
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];
}
Example #3
0
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 );
}
Example #4
0
void QgsColorWheel::setColorFromPos( const QPointF pos )
{
  QPointF center = QPointF( width() / 2.0, height() / 2.0 );
  //line from center to mouse position
  QLineF line = QLineF( center.x(), center.y(), pos.x(), pos.y() );

  QColor newColor = QColor();

  int h, s, l, alpha;
  mCurrentColor.getHsl( &h, &s, &l, &alpha );
  //override hue with explicit hue, so we don't get -1 values from QColor for hue
  h = hue();

  if ( mClickedPart == QgsColorWheel::Triangle )
  {
    //adapted from equations at https://github.com/timjb/colortriangle/blob/master/colortriangle.js by Tim Baumann

    //position of event relative to triangle center
    double x = pos.x() - center.x();
    double y = pos.y() - center.y();

    double eventAngleRadians = line.angle() * M_PI / 180.0;
    double hueRadians = h * M_PI / 180.0;
    double rad0 = fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
    double rad1 = fmod( rad0, (( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
    double length = mWheelImage->width() / 2.0;
    double triangleLength = length - mWheelThickness - 1;

    double a = 0.5 * triangleLength;
    double b = tan( rad1 ) * a;
    double r = sqrt( x * x + y * y );
    double maxR = sqrt( a * a + b * b );

    if ( r > maxR )
    {
      double dx = tan( rad1 ) * r;
      double rad2 = atan( dx / maxR );
      rad2 = qMin( rad2, M_PI / 3.0 );
      rad2 = qMax( rad2, -M_PI / 3.0 );
      eventAngleRadians += rad2 - rad1;
      rad0 = fmod( eventAngleRadians + 2.0 * M_PI - hueRadians, 2.0 * M_PI );
      rad1 = fmod( rad0, (( 2.0 / 3.0 ) * M_PI ) ) - ( M_PI / 3.0 );
      b = tan( rad1 ) * a;
      r = sqrt( a * a + b * b );
    }

    double triangleSideLength = sqrt( 3.0 ) * triangleLength;
    double newL = (( -sin( rad0 ) * r ) / triangleSideLength ) + 0.5;
    double widthShare = 1.0 - ( fabs( newL - 0.5 ) * 2.0 );
    double newS = ((( cos( rad0 ) * r ) + ( triangleLength / 2.0 ) ) / ( 1.5 * triangleLength ) ) / widthShare;
    s = qMin( qRound( qMax( 0.0, newS ) * 255.0 ), 255 );
    l = qMin( qRound( qMax( 0.0, newL ) * 255.0 ), 255 );
    newColor = QColor::fromHsl( h, s, l );
    //explicitly set the hue again, so that it's exact
    newColor.setHsv( h, newColor.hsvSaturation(), newColor.value(), alpha );
  }
  else if ( mClickedPart == QgsColorWheel::Wheel )
  {
    //use hue angle
    s = mCurrentColor.hsvSaturation();
    int v = mCurrentColor.value();
    int newHue = line.angle();
    newColor = QColor::fromHsv( newHue, s, v, alpha );
    //hue has changed, need to redraw triangle
    mTriangleDirty = true;
  }

  if ( newColor.isValid() && newColor != mCurrentColor )
  {
    //color has changed
    mCurrentColor = QColor( newColor );

    if ( mCurrentColor.hue() >= 0 )
    {
      //color has a valid hue, so update the QgsColorWidget's explicit hue
      mExplicitHue = mCurrentColor.hue();
    }

    update();
    emit colorChanged( mCurrentColor );
  }
}
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;
}
Example #6
0
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;
}