QColor ColorRangeBase::toColor(const QVariant &v, ColorRangeBase::ColorModel colormodel)
{
    if ( v.type() == QVariant::Color)
        return QColor(v.value<QColor>());
    else if ( v.type() == QVariant::String){
        QRegExp separ("[(]|,|[)]");
        QStringList parts = (v.toString()).split(separ);
        if(parts.last().isEmpty())
            parts.removeLast();
        QColor clr;
        bool ok1,ok2,ok3,ok4,ok5 =true;
        if ( parts.size() >= 5){
            double component1 = parts[1].toDouble(&ok1);
            double component2 = parts[2].toDouble(&ok2);
            double component3 = parts[3].toDouble(&ok3);
            double component4 = parts[4].toDouble(&ok4);
            double component5 =  parts.size()== 6 ? parts[5].toDouble(&ok5) : rUNDEF;
            if(! (ok1 && ok2 && ok3 && ok4 && ok5))
                return QColor();

            bool isFractional =  component1 <= 1 && component2 <= 1 && component3 <= 1 && component4 <= 1;
            if ( parts[0].toLower() == "rgba"){
                if ( isFractional){
                   clr.setRgbF(component1,component2, component3);
                   clr.setAlphaF(component4);
                }
                else{
                    clr.setRgb(component1,component2, component3);
                    clr.setAlpha(component4);
                }
            }else if ( parts[0].toLower() == "hsla"){
                if ( isFractional){
                   clr.setHslF(component1,component2, component3);
                   clr.setAlphaF(component4);
                }
                else{
                    clr.setHsl(component1,component2, component3);
                    clr.setAlpha(component4);
                }

            } else if ( parts[0].toLower() == "cmyka" && parts.size() == 6){
                if ( isFractional){
                   clr.setCmykF(component1,component2, component3, component4);
                   clr.setAlphaF(component5);
                }
                else{
                    clr.setCmyk(component1,component2, component3, component4);
                    clr.setAlpha(component5);
                }
            }
            return clr;
        }
    } else if( v.type() == QVariant::ULongLong){
        return ColorRangeBase::toColor(v.toULongLong(),colormodel);
    } else if( v.type() == QVariant::Double){
        return ColorRangeBase::toColor(v.toULongLong(),colormodel);
    }

    return QColor();
}
Exemple #2
0
QColor tintColor( const QColor & color, qreal tintfactor )
{
    QColor retColor;
    const qreal  nonTindedPart = 1.0 - tintfactor;
    qreal luminance = 0.0;
    qreal sat = 0.0;
    qreal hue = 0.0;
    color.getHslF( &hue, &sat, &luminance );
    luminance = luminance * tintfactor + nonTindedPart;
    retColor.setHslF( hue, sat, luminance );
//     const int tintedColor = 255 * nonTindedPart;
//     retColor.setRed( tintedColor + tintfactor * color.red() );
//     retColor.setGreen( tintedColor + tintfactor * color.green() );
//     retColor.setBlue( tintedColor + tintfactor * color.blue() );
    return retColor;
}
Exemple #3
0
MinoAnimation::MinoAnimation(QObject *parent) :
    MinoPersistentObject(parent),
    _group(NULL),
    _enabled(false),
    _currentRandY(0)
{
    Q_ASSERT(parent);
    if(MinoAnimationGroup* mag = qobject_cast<MinoAnimationGroup*>(parent))
    {
        setGroup(mag);
        _scene = Minotor::minotor()->scene();
        _boundingRect = Minotor::minotor()->displayRect();
    }
    _color = new MinoPropertyColor(this);
    QColor randColor;
    randColor.setHslF(qrandF(), 1.0, 0.5);
    _color->setColor(randColor);

    _beatFactor = new MinoPropertyBeat(this);
}
void Gradient::paintEvent(QPaintEvent *)
{
    qreal rad,angle;
    QColor color;

    //fill widget's background
    //image->fill(bg_color);
    //color map 4 bytes * count of pixels
    unsigned int * pix = (unsigned int *) image->bits();

    //int radMax = 100;
    for (int x = 0; x <  big_radius; ++x) {
        for (int y = 0; y <  big_radius; ++y) {
            qreal xf = x - big_radius;
            qreal yf = big_radius - y;
            rad = sqrt(xf * xf + yf * yf) / big_radius;
            if (rad > 1.0) continue;
                angle = (1.0 - atan2(yf, xf) / M_PI) ;
            if (angle < 0 || angle > 1) continue;

            rad = sqrt(xf * xf + yf * yf) / med_radius;

            if ( rad > 1.0 )
                color.setHsvF(angle*2.f, 1.0, 1.0);
            else
                color.setHslF(hue, 1, angle*2.f);

            pix[(y + wd_size.y() - big_radius)* wd_size.x() + x + ( wd_size.x() - big_radius) ] = color.rgba();
         }
    }

//    QImage image
    QPainter painter( this );
    painter.drawImage(0,0,(*image));
    painter.drawEllipse(xy_mouse,5,5);

}
Exemple #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);
        }
    }
}
QColor parseColor(ColorFormat format, const QRegularExpressionMatch &match)
{
    QColor ret;

    if (format == ColorFormat::QCssRgbUCharFormat) {
        int r = match.captured(1).toInt();
        int g = match.captured(2).toInt();
        int b = match.captured(3).toInt();

        ret.setRgb(r, g, b);

        QString possibleAlpha = match.captured(4);
        if (!possibleAlpha.isNull()) {
            qreal a = possibleAlpha.toDouble();

            ret.setAlphaF(a);
        }
    }
    if (format == ColorFormat::QCssRgbPercentFormat) {
        qreal r = match.captured(1).remove(QChar::fromLatin1('%')).toDouble() / 100;
        qreal g = match.captured(2).remove(QChar::fromLatin1('%')).toDouble() / 100;
        qreal b = match.captured(3).remove(QChar::fromLatin1('%')).toDouble() / 100;

        ret.setRgbF(r, g, b);

        QString possibleAlpha = match.captured(4);
        if (!possibleAlpha.isNull()) {
            qreal a = possibleAlpha.toDouble();

            ret.setAlphaF(a);
        }
    }
    else if (format == ColorFormat::QssHsvFormat) {
        int h = match.captured(1).toInt();
        int s = match.captured(2).toInt();
        int v = match.captured(3).toInt();

        ret.setHsv(h, s, v);

        QString possibleAlpha = match.captured(4);
        if (!possibleAlpha.isNull()) {
            qreal a = possibleAlpha.remove(QChar::fromLatin1('%')).toDouble() / 100;

            ret.setAlphaF(a);
        }
    }
    else if (format == ColorFormat::CssHslFormat) {
        int h = match.captured(1).toInt();
        int s = match.captured(2).remove(QChar::fromLatin1('%')).toInt() * 255 / 100;
        int l = match.captured(3).remove(QChar::fromLatin1('%')).toInt() * 255 / 100;

        ret.setHsl(h, s, l);

        QString possibleAlpha = match.captured(4);
        if (!possibleAlpha.isNull()) {
            qreal a = possibleAlpha.toDouble();

            ret.setAlphaF(a);
        }
    }
    else if (format == ColorFormat::QmlRgbaFormat) {
        qreal r = match.captured(1).toDouble();
        qreal g = match.captured(2).toDouble();
        qreal b = match.captured(3).toDouble();
        qreal a = match.captured(4).toDouble();

        ret.setRgbF(r, g, b, a);
    }
    else if (format == ColorFormat::QmlHslaFormat) {
        qreal h = match.captured(1).toDouble();
        qreal s = match.captured(2).toDouble();
        qreal l = match.captured(3).toDouble();
        qreal a = match.captured(4).toDouble();

        ret.setHslF(h, s, l, a);
    }
    else if (format == ColorFormat::GlslFormat) {
        qreal r = match.captured(1).toDouble();
        qreal g = match.captured(2).toDouble();
        qreal b = match.captured(3).toDouble();

        ret.setRgbF(r, g, b);

        QString possibleAlpha = match.captured(4);
        if (!possibleAlpha.isNull()) {
            qreal a = possibleAlpha.toDouble();

            ret.setAlphaF(a);
        }
    }
    else if (format == ColorFormat::HexFormat) {
        ret.setNamedColor(match.captured());
    }

    Q_ASSERT_X(ret.isValid(), Q_FUNC_INFO, "The color cannot be invalid.");

    return ret;
}