Ejemplo n.º 1
0
void CanvasStyle::applyStrokeColor(GraphicsContext* context)
{
    if (!context)
        return;
    switch (m_type) {
        case ColorString: {
            Color c = Color(m_color);
            if (c.isValid()) {
                context->setStrokeColor(c.rgb(), DeviceColorSpace);
                break;
            }
            RGBA32 color = 0; // default is transparent black
            if (CSSParser::parseColor(color, m_color))
                context->setStrokeColor(color, DeviceColorSpace);
            break;
        }
        case ColorStringWithAlpha: {
            Color c = Color(m_color);
            if (c.isValid()) {
                context->setStrokeColor(colorWithOverrideAlpha(c.rgb(), m_alpha), DeviceColorSpace);
                break;
            }
            RGBA32 color = 0; // default is transparent black
            if (CSSParser::parseColor(color, m_color))
                context->setStrokeColor(colorWithOverrideAlpha(color, m_alpha), DeviceColorSpace);
            break;
        }
        case GrayLevel:
            // We're only supporting 255 levels of gray here.  Since this isn't
            // even part of HTML5, I don't expect anyone will care.  If they do
            // we'll make a fancier Color abstraction.
            context->setStrokeColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha), DeviceColorSpace);
            break;
        case RGBA:
            context->setStrokeColor(Color(m_red, m_green, m_blue, m_alpha), DeviceColorSpace);
            break;
        case CMYKA: {
            // FIXME: Do this through platform-independent GraphicsContext API.
            // We'll need a fancier Color abstraction to support CYMKA correctly
#if PLATFORM(CG)
            CGContextSetCMYKStrokeColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
#elif PLATFORM(QT)
            QPen currentPen = context->platformContext()->pen();
            QColor clr;
            clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
            currentPen.setColor(clr);
            context->platformContext()->setPen(currentPen);
#else
            context->setStrokeColor(Color(m_cyan, m_magenta, m_yellow, m_black, m_alpha), DeviceColorSpace);
#endif
            break;
        }
        case Gradient:
            context->setStrokeGradient(canvasGradient()->gradient());
            break;
        case ImagePattern:
            context->setStrokePattern(canvasPattern()->pattern());
            break;
    }
}
Ejemplo n.º 2
0
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();
}
Ejemplo n.º 3
0
static QColor qcolorFromCGColor(CGColorRef cgcolor)
{
    QColor pc;
    CGColorSpaceModel model = CGColorSpaceGetModel(CGColorGetColorSpace(cgcolor));
    const CGFloat *components = CGColorGetComponents(cgcolor);
    if (model == kCGColorSpaceModelRGB) {
        pc.setRgbF(components[0], components[1], components[2], components[3]);
    } else if (model == kCGColorSpaceModelCMYK) {
        pc.setCmykF(components[0], components[1], components[2], components[3]);
    } else if (model == kCGColorSpaceModelMonochrome) {
        pc.setRgbF(components[0], components[0], components[0], components[1]);
    } else {
        // Colorspace we can't deal with.
        qWarning("Qt: qcolorFromCGColor: cannot convert from colorspace model: %d", model);
        Q_ASSERT(false);
    }
    return pc;
}
Ejemplo n.º 4
0
void CanvasStyle::applyFillColor(GraphicsContext* context) const
{
    if (!context)
        return;
    switch (m_type) {
    case RGBA:
        context->setFillColor(m_rgba, ColorSpaceDeviceRGB);
        break;
    case CMYKA: {
        // FIXME: Do this through platform-independent GraphicsContext API.
        // We'll need a fancier Color abstraction to support CMYKA correctly
#if USE(CG)
        CGContextSetCMYKFillColor(context->platformContext(), m_cmyka->c, m_cmyka->m, m_cmyka->y, m_cmyka->k, m_cmyka->a);
#elif PLATFORM(QT)
        QBrush currentBrush = context->platformContext()->brush();
        QColor clr;
        clr.setCmykF(m_cmyka->c, m_cmyka->m, m_cmyka->y, m_cmyka->k, m_cmyka->a);
        currentBrush.setColor(clr);
        context->platformContext()->setBrush(currentBrush);
#else
        context->setFillColor(m_cmyka->rgba, ColorSpaceDeviceRGB);
#endif
        break;
    }
    case Gradient:
        context->setFillGradient(canvasGradient()->gradient());
        break;
    case ImagePattern:
        context->setFillPattern(canvasPattern()->pattern());
        break;
    case CurrentColor:
    case CurrentColorWithOverrideAlpha:
    case Invalid:
        ASSERT_NOT_REACHED();
        break;
    }
}
Ejemplo n.º 5
0
void CanvasStyle::applyStrokeColor(GraphicsContext* context)
{
    if (!context)
        return;
    switch (m_type) {
    case RGBA:
        context->setStrokeColor(m_rgba, ColorSpaceDeviceRGB);
        break;
    case CMYKA: {
        // FIXME: Do this through platform-independent GraphicsContext API.
        // We'll need a fancier Color abstraction to support CMYKA correctly
#if PLATFORM(CG)
        CGContextSetCMYKStrokeColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
#elif PLATFORM(QT)
        QPen currentPen = context->platformContext()->pen();
        QColor clr;
        clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
        currentPen.setColor(clr);
        context->platformContext()->setPen(currentPen);
#else
        context->setStrokeColor(m_rgba, ColorSpaceDeviceRGB);
#endif
        break;
    }
    case Gradient:
        context->setStrokeGradient(canvasGradient()->gradient());
        break;
    case ImagePattern:
        context->setStrokePattern(canvasPattern()->pattern());
        break;
    case CurrentColor:
    case CurrentColorWithOverrideAlpha:
        ASSERT_NOT_REACHED();
        break;
    }
}
Ejemplo n.º 6
0
void tst_QColor::setCmyk()
{
    QColor color;

    for (int A = 0; A <= USHRT_MAX; ++A) {
        {
            // 0-255
            int a = A >> 8;
            color.setCmyk(0, 0, 0, 0, a);
            QCOMPARE(color.alpha(), a);

            int c, m, y, k, a2;
            color.getCmyk(&c, &m, &y, &k, &a2);
            QCOMPARE(a2, a);
        }

        {
            // 0.0-1.0
            qreal a = A / qreal(USHRT_MAX);
            color.setCmykF(0.0, 0.0, 0.0, 0.0, a);
            QCOMPARE(color.alphaF(), a);

            qreal c, m, y, k, a2;
            color.getCmykF(&c, &m, &y, &k, &a2);
            QCOMPARE(a2, a);
        }
    }

    for (int C = 0; C <= USHRT_MAX; ++C) {
        {
            // 0-255
            int c = C >> 8;
            color.setCmyk(c, 0, 0, 0, 0);
            QCOMPARE(color.cyan(), c);

            int c2, m, y, k, a;
            color.getCmyk(&c2, &m, &y, &k, &a);
            QCOMPARE(c2, c);
        }

        {
            // 0.0-1.0
            qreal c = C / qreal(USHRT_MAX);
            color.setCmykF(c, 0.0, 0.0, 0.0, 0.0);
            QCOMPARE(color.cyanF(), c);

            qreal c2, m, y, k, a;
            color.getCmykF(&c2, &m, &y, &k, &a);
            QCOMPARE(c2, c);
        }
    }

    for (int M = 0; M <= USHRT_MAX; ++M) {
        {
            // 0-255
            int m = M >> 8;
            color.setCmyk(0, m, 0, 0, 0);
            QCOMPARE(color.magenta(), m);

            int c, m2, y, k, a;
            color.getCmyk(&c, &m2, &y, &k, &a);
            QCOMPARE(m2, m);
        }

        {
            // 0.0-1.0
            qreal m = M / qreal(USHRT_MAX);
            color.setCmykF(0.0, m, 0.0, 0.0, 0.0);
            QCOMPARE(color.magentaF(), m);

            qreal c, m2, y, k, a;
            color.getCmykF(&c, &m2, &y, &k, &a);
            QCOMPARE(m2, m);
        }
    }

    for (int Y = 0; Y <= USHRT_MAX; ++Y) {
        {
            // 0-255
            int y = Y >> 8;
            color.setCmyk(0, 0, y, 0, 0);
            QCOMPARE(color.yellow(), y);

            int c, m, y2, k, a;
            color.getCmyk(&c, &m, &y2, &k, &a);
            QCOMPARE(y2, y);
        }

        {
            // 0.0-1.0
            qreal y = Y / qreal(USHRT_MAX);
            color.setCmykF(0.0, 0.0, y, 0.0, 0.0);
            QCOMPARE(color.yellowF(), y);

            qreal c, m, y2, k, a;
            color.getCmykF(&c, &m, &y2, &k, &a);
            QCOMPARE(y2, y);
        }
    }

    for (int K = 0; K <= USHRT_MAX; ++K) {
        {
            // 0-255
            int k = K >> 8;
            color.setCmyk(0, 0, 0, k, 0);
            QCOMPARE(color.black(), k);

            int c, m, y, k2, a;
            color.getCmyk(&c, &m, &y, &k2, &a);
            QCOMPARE(k2, k);
        }

        {
            // 0.0-1.0
            qreal k = K / qreal(USHRT_MAX);
            color.setCmykF(0.0, 0.0, 0.0, k, 0.0);
            QCOMPARE(color.blackF(), k);

            qreal c, m, y, k2, a;
            color.getCmykF(&c, &m, &y, &k2, &a);
            QCOMPARE(k2, k);
        }
    }
}