コード例 #1
0
ファイル: colorpicker.cpp プロジェクト: timsee/Corluma
void ColorPicker::handleMouseEvent(QMouseEvent *event) {
    QRect geometry = mColorWheel->geometry();
    if (eventIsOverWheel(event)) {
        QPixmap pixmap = QWidget::grab(mColorWheel->geometry());
        // some mobile and other environments save pixmaps without pixel density taken into account. For example, breaks on iPhone 6 without this.
        if (pixmap.size() != mColorWheel->geometry().size()) {
            pixmap = pixmap.scaled(mColorWheel->geometry().size().width(),
                                   mColorWheel->geometry().size().height());
        }
        QColor color = pixmap.toImage().pixel(event->pos().x() - geometry.x(),
                                              event->pos().y() - geometry.y());
        if (checkIfColorIsValid(color)){
            if (mCurrentLayoutColorPicker == ELayoutColorPicker::multiColorLayout) {
                if (mWheelIsEnabled) {
                    mCustomColorPicker->updateSelected(color);
                    emit multiColorUpdate();
                }
            } else if (mCurrentLayoutColorPicker == ELayoutColorPicker::ambientLayout) {
                // use the poorly named "value" of the HSV range to calculate the brightness
                auto brightness = uint32_t(color.valueF() * 100.0);
                // adjust the color so that it has a maxed out value in the HSV colorspace
                color.setHsv(color.hue(),
                             color.saturation(),
                             255);
                // then calculate then use the resulting QColor to convert to color temperature.
                int ct = cor::rgbToColorTemperature(color);
                chooseAmbient(ct, brightness, true);
                mTempBrightSliders->changeTemperatureAndBrightness(ct, brightness);
            } else if (mCurrentLayoutColorPicker == ELayoutColorPicker::brightnessLayout) {
                // use the poorly named "value" of the HSV range to calculate the brightness
                auto brightness = uint32_t(color.valueF() * 100.0);
                chooseBrightness(brightness);
                mBrightnessSlider->changeBrightness(brightness);
            } else if (mCurrentLayoutColorPicker == ELayoutColorPicker::standardLayout) {
                chooseColor(color);
                mRGBSliders->changeColor(color);
            } else if (mCurrentLayoutColorPicker == ELayoutColorPicker::colorSchemeLayout) {
                if (mCircleIndex == -1) {
                    mColorSchemeCircles->moveCenterCircle(event->pos(), true);
                    mCircleIndex = 10;
                } else if (mCircleIndex == 10) {
                    mColorSchemeCircles->moveCenterCircle(event->pos(), false);
                } else {
                    mColorSchemeCircles->moveStandardCircle(uint32_t(mCircleIndex), event->pos());
                }

                // turn into vector of colors
                std::vector<QColor> colors;
                for (const auto& circle : mColorSchemeCircles->circles()) {
                    colors.push_back(circle.color);
                }
                emit colorsUpdate(colors);

               // mColorSchemeGrid->setColor(2, color);
            }
        }
    }
}
コード例 #2
0
QVariantMap Core::colorToHsv(const QColor &color) {
    QVariantMap map;
    map["h"] = color.hueF();
    map["s"] = color.saturationF();
    map["v"] = color.valueF();
    return map;
}
コード例 #3
0
ファイル: colorwheel.cpp プロジェクト: chchwy/pencil2d
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);
}
コード例 #4
0
ファイル: colorinspector.cpp プロジェクト: Forkhere/pencil
void ColorInspector::setColor(const QColor &newColor)
{
    if (newColor == m_color)
    {
        return;
    }
    noColorUpdate = true;

    if(isRgbColors)
    {
        ui->RedspinBox->setValue(newColor.red());
        ui->GreenspinBox->setValue(newColor.green());
        ui->BluespinBox->setValue(newColor.blue());
        ui->AlphaspinBox->setValue(newColor.alpha());
    }
    else
    {
        ui->RedspinBox->setValue( qBound(0.0, newColor.hsvHueF() * 359, 359.0) );
        ui->GreenspinBox->setValue( qBound(0.0, newColor.hsvSaturationF() * 100, 100.0) );
        ui->BluespinBox->setValue( qBound(0.0, newColor.valueF() * 100, 100.0) );
        ui->AlphaspinBox->setValue( qBound(0.0, newColor.alphaF() * 100, 100.0) );
    }
    m_color = newColor;

    QPalette p = ui->color->palette();
    p.setColor(QPalette::Background, m_color);
    ui->color->setPalette(p);
    //ui->color->setFixedSize(30,30);
    noColorUpdate = false;
}
コード例 #5
0
ファイル: hue_slider.cpp プロジェクト: Rambo2015/Drawpile
void HueSlider::setColor(const QColor& color)
{
    p->saturation = color.saturationF();
    p->value = color.valueF();
    p->updateGradient();
    setColorHue(color.hueF());
}
コード例 #6
0
ファイル: nqcolorwheel.cpp プロジェクト: Negusbuk/MatDB
void NQColorWheel::drawPicker(const QColor &color)
{
    QPainter painter(&pixmapWheel_);
    painter.setRenderHint(QPainter::Antialiasing);
    QPen pen;

    // region of the widget
    int w = qMin(width(), height());
    // radius of outer circle
    qreal r = w/2-margin_;
    // radius of inner circle
    qreal ir = r-wheelWidth_;
    // left corner of square
    qreal m = w/2.0-ir/qSqrt(2);
    painter.translate(m-5, m-5);
    qreal SquareWidth = 2*ir/qSqrt(2);
    qreal S = (color.saturationF()-minimumS_)/(1.0-minimumS_)*SquareWidth;
    qreal V = color.valueF()*SquareWidth;

    if (color.saturation() > 30 || color.value() < 50){
        //pen.setColor(Qt::white);
    }

    pen.setWidth(2);
    painter.setPen(pen);
    painter.drawEllipse(S, V, 8, 8);
}
コード例 #7
0
void ColorSpinBoxGroup::setColor(const QColor &c)
{
    if(c == color_) return;
    noColorUpdate = true;
    if(isRgbColors){
        ui->RedspinBox->setValue(c.red());
        ui->GreenspinBox->setValue(c.green());
        ui->BluespinBox->setValue(c.blue());
    }else{
        ui->RedspinBox->setValue(
                    qBound(0.0, c.hsvHueF()*359, 359.0)
                    );
        ui->GreenspinBox->setValue(
                    qBound(0.0, c.hsvSaturationF()*100, 100.0)
                    );
        ui->BluespinBox->setValue(
                    qBound(0.0, c.valueF()*100, 100.0)
                    );
    }
    color_ = c;
    QPalette p = ui->label->palette();
    p.setColor(QPalette::Background, color_);
    ui->label->setPalette(p);
    noColorUpdate = false;
}
コード例 #8
0
ファイル: theme.cpp プロジェクト: Stankim/dukto
float Theme::getLightness(QString color) {

    QColor c;
    c.setNamedColor(color);
    QColor converted = c.toHsv();
    return converted.valueF();
}
コード例 #9
0
void QtGradientStopsControllerPrivate::slotChangeHue(int color)
{
    QColor c = m_ui->hueColorLine->color();
    if (m_ui->hsvRadioButton->isChecked())
        c.setHsvF((qreal)color / 360.0, c.saturationF(), c.valueF(), c.alphaF());
    else
        c.setRed(color);
    slotChangeHue(c);
}
コード例 #10
0
void Color2DSlider::setColor(const QColor& c)
{
    p->hue = c.hsvHueF();
    p->sat = c.saturationF();
    p->val = c.valueF();
    p->renderSquare(size());
    update();
    emit colorChanged(color());
}
コード例 #11
0
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);
}
コード例 #12
0
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();
}
コード例 #13
0
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);
}
コード例 #14
0
ファイル: tracer_params.cpp プロジェクト: bjandras/Newtown
float Tracer::distSep(Point const & p) const
{
	if (m_populationMapImage.isNull())
	{
		return distSep();
	}

	QPoint pxp = m_populationMapImage.toImageCoords(p.pos()).toPoint();
	QRgb rgb = m_populationMapImage.pixel(pxp);
	QColor c = QColor::fromRgb(rgb);
	float  k = c.valueF();

	return distSep() * (1.5f - k);
}
コード例 #15
0
QPointF ColorVolumeWidget::getColorPosition(const QColor &color) const
{
	qreal size = (this->direction == Horizontal) ? imageRect.width() : imageRect.height();
	qreal pos;

	if (this->mode == DarkOnly)
	{
		pos = color.valueF() * size;
	}
	else
	{
		if (color.value() < 255)
		{
			pos = (color.valueF()) * (size / 2);
		}
		else
		{
			pos = (255.0 - color.saturation()) / 255 * (size / 2) + size / 2;
		}
	}

	return (this->direction == Horizontal) ? QPointF(pos, 0) : QPointF(0, pos);
}
コード例 #16
0
ファイル: Scene_c3t3_item.cpp プロジェクト: logtcn/cgal
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());
  }
}
コード例 #17
0
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();
}
コード例 #18
0
void QtGradientStopsControllerPrivate::slotChangeAlpha(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(c.hueF(), c.saturationF(), c.valueF(), color.alphaF());
                int hue = c.hue();
                if (hue == 360 || hue == -1)
                    c.setHsvF(0.0, c.saturationF(), c.valueF(), c.alphaF());
            } else {
                c.setRgbF(c.redF(), c.greenF(), c.blueF(), color.alphaF());
            }
            m_model->changeStop(s, c);
        }
    }
}
コード例 #19
0
ファイル: Qt_colors.cpp プロジェクト: erdoukki/radium
void GFX_SetBrightness(struct Tracker_Windows *tvisual, float how_much){
  EditorWidget *editorwidget=(EditorWidget *)tvisual->os_visual.widget;
  if(is_starting_up)
    return;
  return;
#if 0
  float threshold = QColor(SETTINGS_read_string(talloc_format("color%d",15),"#d0d5d0")).valueF();
  
  for(int i=0;i<15;i++){
    QColor color = QColor(SETTINGS_read_string(talloc_format("color%d",i),"#d0d5d0"));
      
    //QColor color = editorwidget->colors[i];
    float value = color.valueF();
    if (value > threshold)
      color = color.lighter(scale(how_much, 0, 1, 0, 200));
    else
      color = color.darker(scale(how_much, 0, 1, 0, 200));

    if (i!=11)
      setColor((enum ColorNums)i, color.rgb());
    printf("value for %d: %f\n",i,value);
    //color.setLightntess(lightness
  }
#else
  
  how_much = scale(how_much,0,1,-1,1);

  for(int i=0;i<16;i++){
    QColor color = QColor(SETTINGS_read_string(talloc_format("color%d",i),"#d0d5d0"));

    qreal h,s,v,a;
    color.getHsvF(&h,&s,&v,&a);

    float value = R_BOUNDARIES(0,v+how_much,1);
    color.setHsvF(h, s, value, a);
    
    //QColor color = editorwidget->colors[i];
    setColor((enum ColorNums)i, color.rgb());
    
    printf("value for %d: %f. s: %f, how_much: %f\n",i,value,s,how_much);
    //color.setLightntess(lightness
  }
#endif
  
  updateAll(editorwidget);
  tvisual->must_redraw = true;
}
コード例 #20
0
StatesEditorWidget::StatesEditorWidget(StatesEditorView *statesEditorView, StatesEditorModel *statesEditorModel):
        QWidget(),
    m_declarativeView(new QDeclarativeView(this)),
    m_statesEditorView(statesEditorView),
    m_imageProvider(0)
{
    m_imageProvider = new Internal::StatesEditorImageProvider;
    m_imageProvider->setNodeInstanceView(statesEditorView->nodeInstanceView());
    m_declarativeView->engine()->addImageProvider(
            QLatin1String("qmldesigner_stateseditor"), m_imageProvider);

    m_declarativeView->setAcceptDrops(false);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addWidget(m_declarativeView.data());

    m_declarativeView->setResizeMode(QDeclarativeView::SizeRootObjectToView);

    m_declarativeView->rootContext()->setContextProperty(QLatin1String("statesEditorModel"), statesEditorModel);
    QColor highlightColor = palette().highlight().color();
    if (0.5*highlightColor.saturationF()+0.75-highlightColor.valueF() < 0)
        highlightColor.setHsvF(highlightColor.hsvHueF(),0.1 + highlightColor.saturationF()*2.0, highlightColor.valueF());
    m_declarativeView->rootContext()->setContextProperty(QLatin1String("highlightColor"), highlightColor);

    // Work around ASSERT in the internal QGraphicsScene that happens when
    // the scene is created + items set dirty in one event loop run (BAUHAUS-459)
    //QApplication::processEvents();

    m_declarativeView->setSource(QUrl("qrc:/stateseditor/stateslist.qml"));

    if (!m_declarativeView->rootObject())
        throw InvalidQmlSourceException(__LINE__, __FUNCTION__, __FILE__);

    m_declarativeView->setFocusPolicy(Qt::ClickFocus);
    QApplication::sendEvent(m_declarativeView->scene(), new QEvent(QEvent::WindowActivate));

    connect(m_declarativeView->rootObject(), SIGNAL(currentStateInternalIdChanged()), statesEditorView, SLOT(synchonizeCurrentStateFromWidget()));
    connect(m_declarativeView->rootObject(), SIGNAL(createNewState()), statesEditorView, SLOT(createNewState()));
    connect(m_declarativeView->rootObject(), SIGNAL(deleteState(int)), statesEditorView, SLOT(removeState(int)));

    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));

    setWindowTitle(tr("States", "Title of Editor widget"));
}
コード例 #21
0
StatesEditorWidget::StatesEditorWidget(StatesEditorView *statesEditorView, StatesEditorModel *statesEditorModel)
    : QWidget(),
      m_quickView(new QQuickView()),
      m_statesEditorView(statesEditorView),
      m_imageProvider(0),
      m_qmlSourceUpdateShortcut(0)
{
    m_imageProvider = new Internal::StatesEditorImageProvider;
    m_imageProvider->setNodeInstanceView(statesEditorView->nodeInstanceView());

    m_quickView->engine()->addImageProvider(QLatin1String("qmldesigner_stateseditor"), m_imageProvider);
    m_quickView->engine()->addImportPath(statesEditorResourcesPath());

    //m_quickView->setAcceptDrops(false);

    m_qmlSourceUpdateShortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_F4), this);
    connect(m_qmlSourceUpdateShortcut, SIGNAL(activated()), this, SLOT(reloadQmlSource()));

    QVBoxLayout *layout = new QVBoxLayout(this);
    setMinimumHeight(160);
    layout->setMargin(0);
    layout->setSpacing(0);
    QWidget *container = createWindowContainer(m_quickView.data());
    layout->addWidget(container);

    m_quickView->setResizeMode(QQuickView::SizeRootObjectToView);

    m_quickView->rootContext()->setContextProperty(QLatin1String("statesEditorModel"), statesEditorModel);
    QColor highlightColor = palette().highlight().color();
    if (0.5*highlightColor.saturationF()+0.75-highlightColor.valueF() < 0)
        highlightColor.setHsvF(highlightColor.hsvHueF(),0.1 + highlightColor.saturationF()*2.0, highlightColor.valueF());
    m_quickView->rootContext()->setContextProperty(QLatin1String("highlightColor"), highlightColor);

    m_quickView->rootContext()->setContextProperty("canAddNewStates", true);

    setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred));

    setWindowTitle(tr("States", "Title of Editor widget"));

    // init the first load of the QML UI elements
    reloadQmlSource();
}
コード例 #22
0
void drawSelectionBackground(QPainter *painter, const QStyleOption &option)
{
    QWidget colorReference;

    painter->save();
    QLinearGradient gradient;
    QColor highlightColor = colorReference.palette().highlight().color();
    if (0.5*highlightColor.saturationF()+0.75-highlightColor.valueF() < 0)
        highlightColor.setHsvF(highlightColor.hsvHueF(),0.1 + highlightColor.saturationF()*2.0, highlightColor.valueF());
    gradient.setColorAt(0, highlightColor.lighter(130));
    gradient.setColorAt(1, highlightColor.darker(130));
    gradient.setStart(option.rect.topLeft());
    gradient.setFinalStop(option.rect.bottomLeft());
    painter->fillRect(option.rect, gradient);
    painter->setPen(highlightColor.lighter());
    painter->drawLine(option.rect.topLeft(),option.rect.topRight());
    painter->setPen(highlightColor.darker());
    painter->drawLine(option.rect.bottomLeft(),option.rect.bottomRight());
    painter->restore();
}
コード例 #23
0
ファイル: ColorPickerPushButton.cpp プロジェクト: JobstM/ogs
void ColorPickerPushButton::setColor( QColor color )
{
	_color = color;

	// Compute text color
	QColor hsv = _color.toHsv();
	QString textColorStr;
	if (hsv.valueF() < 0.5f)
		textColorStr = "color: rgb(255, 255, 255);";
	else
		textColorStr = "color: rgb(0, 0, 0);";

	QString stylesheetStr = "background-color: ";
	stylesheetStr.append(colorToCss(_color));
	stylesheetStr.append(";");
	stylesheetStr.append(textColorStr);
	this->setStyleSheet(stylesheetStr);

	this->setText(colorToString(_color));
}
コード例 #24
0
ファイル: colorbox.cpp プロジェクト: callaa/Drawpile
void ColorBox::setColor(const QColor& color)
{
	_updating = true;
	_ui->red->setFirstColor(QColor(0, color.green(), color.blue()));
	_ui->red->setLastColor(QColor(255, color.green(), color.blue()));
	_ui->red->setValue(color.red());
	_ui->redbox->setValue(color.red());

	_ui->green->setFirstColor(QColor(color.red(), 0, color.blue()));
	_ui->green->setLastColor(QColor(color.red(), 255, color.blue()));
	_ui->green->setValue(color.green());
	_ui->greenbox->setValue(color.green());

	_ui->blue->setFirstColor(QColor(color.red(), color.green(), 0));
	_ui->blue->setLastColor(QColor(color.red(), color.green(), 255));
	_ui->blue->setValue(color.blue());
	_ui->bluebox->setValue(color.blue());


	_ui->hue->setColorSaturation(color.saturationF());
	_ui->hue->setColorValue(color.valueF());
	_ui->hue->setValue(color.hue());
	_ui->huebox->setValue(color.hue());

	_ui->saturation->setFirstColor(QColor::fromHsv(color.hue(), 0, color.value()));
	_ui->saturation->setLastColor(QColor::fromHsv(color.hue(), 255, color.value()));
	_ui->saturation->setValue(color.saturation());
	_ui->saturationbox->setValue(color.saturation());

	_ui->value->setFirstColor(QColor::fromHsv(color.hue(), color.saturation(), 0));
	_ui->value->setLastColor(QColor::fromHsv(color.hue(), color.saturation(), 255));
	_ui->value->setValue(color.value());
	_ui->valuebox->setValue(color.value());

	_ui->colorwheel->setColor(color);
	_updating = false;
}
コード例 #25
0
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);
}
コード例 #26
0
ファイル: colorwheel.cpp プロジェクト: CandyFace/pencil
void ColorWheel::drawPicker(const QColor &color)
{
    QPainter painter(&m_wheelPixmap);
    painter.setRenderHint(QPainter::Antialiasing);

    QPoint squareTopLeft = m_squareRegion.boundingRect().topLeft();

    painter.translate(squareTopLeft.x(), squareTopLeft.y());

    QSize squareSize = m_squareRegion.boundingRect().size();

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

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

    painter.drawEllipse(S - 5, V - 5, 10, 10);
}
コード例 #27
0
ファイル: ColorUtils.cpp プロジェクト: pothosware/pothos-gui
/***********************************************************************
 * color map helper utilities
 **********************************************************************/
static QColor pastelize(const QColor &c)
{
    //Pastels have high value and low to intermediate saturation:
    //http://en.wikipedia.org/wiki/Pastel_%28color%29
    return QColor::fromHsv(c.hue(), int(c.saturationF()*128), int(c.valueF()*64)+191);
}
コード例 #28
0
QImage GradientLoader::drawGradient(const GradientList& data)
{
    if (data.isEmpty())
    {
        return QImage();
    }

    QPixmap pix(MAX_THUMB_SIZE, MAX_THUMB_SIZE / 2);
    pix.fill(Qt::white);

    QPainter p(&pix);
    p.setPen(QPen(Qt::transparent));
    p.setRenderHint(QPainter::Antialiasing, true);

    QLinearGradient grad(0.0, 0.0, 1.0, 0.0);
    grad.setCoordinateMode(QGradient::StretchToDeviceMode);

    foreach(const GradientData & gradient, data)
    {
        if (gradient.status != GradientData::Ok)
        {
            continue;
        }

        qreal start = gradient.startPoint + 0.01;
        qreal end   = gradient.endPoint;

        // do some basic range checks
        start = qMin(1.0, start);
        start = qMax(0.0, start);
        end   = qMin(1.0, end);
        end   = qMax(0.0, end);

        // check coloring mode
        if (gradient.coloringMode == GradientData::HSVclockwise ||
            gradient.coloringMode == GradientData::HSVcounterClockwise
           )
        {
            const int hue_range = 360;

            bool reverse        = (gradient.coloringMode == GradientData::HSVcounterClockwise);
            qreal stepping      = qAbs(end - start) / (qreal)hue_range;
            qreal pos           = reverse ? start : end;

            QColor col = QColor::fromRgbF(gradient.leftColorRed,
                                          gradient.leftColorGreen,
                                          gradient.leftColorBlue,
                                          gradient.leftColorAlpha);
            QColor hsv        = col.toHsv();
            qreal saturationF = hsv.saturationF();
            qreal valueF      = hsv.valueF();
            qreal alphaF      = hsv.alphaF();

            for (int h = 0.0; h < hue_range; ++h)
            {
                grad.setColorAt(pos, QColor::fromHsvF((qreal)h / hue_range,
                                                      saturationF,
                                                      valueF,
                                                      alphaF));

                if (reverse)
                {
                    pos += stepping;
                }
                else
                {
                    pos -= stepping;
                }
            }
        }
        else
        {
            grad.setColorAt(start, QColor::fromRgbF(gradient.leftColorRed,
                                                    gradient.leftColorGreen,
                                                    gradient.leftColorBlue,
                                                    gradient.leftColorAlpha));

            grad.setColorAt(end, QColor::fromRgbF(gradient.rightColorRed,
                                                  gradient.rightColorGreen,
                                                  gradient.rightColorBlue,
                                                  gradient.rightColorAlpha));
        }
    }

    p.fillRect(pix.rect(), QBrush(grad));
    p.end();

    return pix.toImage();
}
コード例 #29
0
ファイル: tst_qcolor.cpp プロジェクト: KDE/android-qt
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);
        }
    }
}
コード例 #30
0
ItemLibraryWidget::ItemLibraryWidget(QWidget *parent) :
    QFrame(parent),
    m_iconProvider(m_resIconSize),
    m_itemIconSize(24, 24),
    m_resIconSize(24, 24),
    m_itemsView(new QDeclarativeView(this)),
    m_resourcesView(new Internal::ItemLibraryTreeView(this)),
    m_filterFlag(QtBasic)
{
    setWindowTitle(tr("Library", "Title of library view"));

    /* create Items view and its model */
    m_itemsView->setAttribute(Qt::WA_OpaquePaintEvent);
    m_itemsView->setAttribute(Qt::WA_NoSystemBackground);
    m_itemsView->setAcceptDrops(false);
    m_itemsView->setFocusPolicy(Qt::ClickFocus);
    m_itemsView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    m_itemLibraryModel = new Internal::ItemLibraryModel(QDeclarativeEnginePrivate::getScriptEngine(m_itemsView->engine()), this);
    m_itemLibraryModel->setItemIconSize(m_itemIconSize);

    QDeclarativeContext *rootContext = m_itemsView->rootContext();
    rootContext->setContextProperty(QLatin1String("itemLibraryModel"), m_itemLibraryModel.data());
    rootContext->setContextProperty(QLatin1String("itemLibraryIconWidth"), m_itemIconSize.width());
    rootContext->setContextProperty(QLatin1String("itemLibraryIconHeight"), m_itemIconSize.height());

    QColor highlightColor = palette().highlight().color();
    if (0.5*highlightColor.saturationF()+0.75-highlightColor.valueF() < 0)
        highlightColor.setHsvF(highlightColor.hsvHueF(),0.1 + highlightColor.saturationF()*2.0, highlightColor.valueF());
    m_itemsView->rootContext()->setContextProperty(QLatin1String("highlightColor"), highlightColor);

    // loading the qml has to come after all needed context properties are set
    m_itemsView->setSource(QUrl("qrc:/ItemLibrary/qml/ItemsView.qml"));

    QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(m_itemsView->rootObject());
    connect(rootItem, SIGNAL(itemSelected(int)), this, SLOT(showItemInfo(int)));
    connect(rootItem, SIGNAL(itemDragged(int)), this, SLOT(startDragAndDrop(int)));
    connect(this, SIGNAL(scrollItemsView(QVariant)), rootItem, SLOT(scrollView(QVariant)));
    connect(this, SIGNAL(resetItemsView()), rootItem, SLOT(resetView()));

    /* create Resources view and its model */
    m_resourcesFileSystemModel = new QFileSystemModel(this);
    m_resourcesFileSystemModel->setIconProvider(&m_iconProvider);
    m_resourcesView->setModel(m_resourcesFileSystemModel.data());
    m_resourcesView->setIconSize(m_resIconSize);

    /* create image provider for loading item icons */
    m_itemsView->engine()->addImageProvider(QLatin1String("qmldesigner_itemlibrary"), new Internal::ItemLibraryImageProvider);

    /* other widgets */
    QTabBar *tabBar = new QTabBar(this);
    tabBar->addTab(tr("Items", "Title of library items view"));
    tabBar->addTab(tr("Resources", "Title of library resources view"));
    tabBar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    m_lineEdit = new Utils::FilterLineEdit(this);
    m_lineEdit->setObjectName(QLatin1String("itemLibrarySearchInput"));
    m_lineEdit->setPlaceholderText(tr("<Filter>", "Library search input hint text"));
    m_lineEdit->setDragEnabled(false);
    m_lineEdit->setMinimumWidth(75);
    m_lineEdit->setTextMargins(0, 0, 20, 0);
    QWidget *lineEditFrame = new QWidget(this);
    lineEditFrame->setObjectName(QLatin1String("itemLibrarySearchInputFrame"));
    QGridLayout *lineEditLayout = new QGridLayout(lineEditFrame);
    lineEditLayout->setMargin(2);
    lineEditLayout->setSpacing(0);
    lineEditLayout->addItem(new QSpacerItem(5, 3, QSizePolicy::Fixed, QSizePolicy::Fixed), 0, 0, 1, 3);
    lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 0);
    lineEditLayout->addWidget(m_lineEdit.data(), 1, 1, 1, 1);
    lineEditLayout->addItem(new QSpacerItem(5, 5, QSizePolicy::Fixed, QSizePolicy::Fixed), 1, 2);
    connect(m_lineEdit.data(), SIGNAL(filterChanged(QString)), this, SLOT(setSearchFilter(QString)));

    m_stackedWidget = new QStackedWidget(this);
    m_stackedWidget->addWidget(m_itemsView.data());
    m_stackedWidget->addWidget(m_resourcesView.data());
    connect(tabBar, SIGNAL(currentChanged(int)),
            m_stackedWidget.data(), SLOT(setCurrentIndex(int)));
    connect(tabBar, SIGNAL(currentChanged(int)),
            this, SLOT(updateSearch()));

    QWidget *spacer = new QWidget(this);
    spacer->setObjectName(QLatin1String("itemLibrarySearchInputSpacer"));
    spacer->setFixedHeight(4);

    QGridLayout *layout = new QGridLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addWidget(tabBar, 0, 0, 1, 1);
    layout->addWidget(spacer, 1, 0);
    layout->addWidget(lineEditFrame, 2, 0, 1, 1);
    layout->addWidget(m_stackedWidget.data(), 3, 0, 1, 1);

    setResourcePath(QDir::currentPath());
    setSearchFilter(QString());

    /* style sheets */
    setStyleSheet(QLatin1String(Utils::FileReader::fetchQrc(":/qmldesigner/stylesheet.css")));
    m_resourcesView->setStyleSheet(
            QLatin1String(Utils::FileReader::fetchQrc(":/qmldesigner/scrollbar.css")));
}