Changeset 59768 in webkit


Ignore:
Timestamp:
May 19, 2010 9:11:08 AM (14 years ago)
Author:
chang.shu@nokia.com
Message:

2010-05-19 Andreas Kling <andreas.kling@nokia.com>

Reviewed by Darin Adler.

CanvasRenderingContext2D's property getters that return colors should
serialize them in accordance with the HTML5 spec (4.8.11.1.4)
Setters should ignore invalid colors.

Also, shadowColor should initially be transparent black (4.8.11.1.6)

https://bugs.webkit.org/show_bug.cgi?id=38845

Refactored CanvasStyle to store an RGBA32 instead of RGBA components and/or the color string.

Spec links:
http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
http://www.whatwg.org/specs/web-apps/current-work/#dom-context-2d-shadowcolor

  • html/canvas/CanvasRenderingContext2D.cpp: (WebCore::CanvasRenderingContext2D::State::State): (WebCore::CanvasRenderingContext2D::shadowColor): (WebCore::CanvasRenderingContext2D::setShadowColor): (WebCore::CanvasRenderingContext2D::setShadow): (WebCore::CanvasRenderingContext2D::clearShadow): (WebCore::CanvasRenderingContext2D::applyShadow):
  • html/canvas/CanvasRenderingContext2D.h:
  • html/canvas/CanvasStyle.cpp: (WebCore::CanvasStyle::CanvasStyle): (WebCore::CanvasStyle::create): (WebCore::CanvasStyle::applyStrokeColor): (WebCore::CanvasStyle::applyFillColor):
  • html/canvas/CanvasStyle.h: (WebCore::CanvasStyle::create): (WebCore::CanvasStyle::color): (WebCore::CanvasStyle::): (WebCore::CanvasStyle::CMYKAValues::CMYKAValues):
  • platform/graphics/Color.cpp: (WebCore::Color::serialized):
  • platform/graphics/Color.h:

2010-05-19 Andreas Kling <andreas.kling@nokia.com>

Reviewed by Darin Adler.

Added a test to verify proper color serialization in CanvasRenderingContext2D's getters.
https://bugs.webkit.org/show_bug.cgi?id=38845

Spec link:
http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color

  • fast/canvas/canvas-color-serialization-expected.txt: Added.
  • fast/canvas/canvas-color-serialization.html: Added.
  • fast/canvas/canvas-set-properties-with-non-invertible-ctm-expected.txt: Updated baseline.
  • fast/canvas/script-tests/canvas-color-serialization.js: Added. (trySettingStrokeStyle): (trySettingFillStyle): (trySettingShadowColor): (trySettingColor): (trySettingStrokeColorWithSetter): (trySettingFillColorWithSetter): (trySettingShadowWithSetter): (trySettingColorWithSetter): (trySettingFillColorRGBA): (trySettingStrokeColorRGBA): (trySettingShadowRGBA): (trySettingRGBA): (trySettingFillColorCMYKA): (trySettingStrokeColorCMYKA): (trySettingShadowCMYKA): (trySettingCMYKA): (trySettingFillColorWithOverrideAlpha): (trySettingStrokeColorWithOverrideAlpha): (trySettingShadowWithOverrideAlpha): (trySettingColorWithOverrideAlpha): (trySettingFillColorGrayLevel): (trySettingStrokeColorGrayLevel): (trySettingShadowGrayLevel): (trySettingGrayLevel): (trySettingFillColorGrayLevelWithAlpha): (trySettingStrokeColorGrayLevelWithAlpha): (trySettingShadowGrayLevelWithAlpha): (trySettingGrayLevelWithAlpha): (tryClearShadowAfterSettingColor): (checkDefaultValue):
  • fast/canvas/script-tests/canvas-set-properties-with-non-invertible-ctm.js:
  • fast/dom/canvasContext2d-element-attribute-js-null-expected.txt: Updated baseline.
  • fast/dom/canvasContext2d-element-attribute-js-null.html:
Location:
trunk
Files:
3 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r59767 r59768  
     12010-05-19  Andreas Kling  <andreas.kling@nokia.com>
     2
     3        Reviewed by Darin Adler.
     4
     5        Added a test to verify proper color serialization in CanvasRenderingContext2D's getters.
     6        https://bugs.webkit.org/show_bug.cgi?id=38845
     7
     8        Spec link:
     9        http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
     10
     11        * fast/canvas/canvas-color-serialization-expected.txt: Added.
     12        * fast/canvas/canvas-color-serialization.html: Added.
     13        * fast/canvas/canvas-set-properties-with-non-invertible-ctm-expected.txt: Updated baseline.
     14        * fast/canvas/script-tests/canvas-color-serialization.js: Added.
     15        (trySettingStrokeStyle):
     16        (trySettingFillStyle):
     17        (trySettingShadowColor):
     18        (trySettingColor):
     19        (trySettingStrokeColorWithSetter):
     20        (trySettingFillColorWithSetter):
     21        (trySettingShadowWithSetter):
     22        (trySettingColorWithSetter):
     23        (trySettingFillColorRGBA):
     24        (trySettingStrokeColorRGBA):
     25        (trySettingShadowRGBA):
     26        (trySettingRGBA):
     27        (trySettingFillColorCMYKA):
     28        (trySettingStrokeColorCMYKA):
     29        (trySettingShadowCMYKA):
     30        (trySettingCMYKA):
     31        (trySettingFillColorWithOverrideAlpha):
     32        (trySettingStrokeColorWithOverrideAlpha):
     33        (trySettingShadowWithOverrideAlpha):
     34        (trySettingColorWithOverrideAlpha):
     35        (trySettingFillColorGrayLevel):
     36        (trySettingStrokeColorGrayLevel):
     37        (trySettingShadowGrayLevel):
     38        (trySettingGrayLevel):
     39        (trySettingFillColorGrayLevelWithAlpha):
     40        (trySettingStrokeColorGrayLevelWithAlpha):
     41        (trySettingShadowGrayLevelWithAlpha):
     42        (trySettingGrayLevelWithAlpha):
     43        (tryClearShadowAfterSettingColor):
     44        (checkDefaultValue):
     45        * fast/canvas/script-tests/canvas-set-properties-with-non-invertible-ctm.js:
     46        * fast/dom/canvasContext2d-element-attribute-js-null-expected.txt: Updated baseline.
     47        * fast/dom/canvasContext2d-element-attribute-js-null.html:
     48
    1492010-05-19  Andreas Kling  <andreas.kling@nokia.com>
    250
  • trunk/LayoutTests/fast/canvas/canvas-set-properties-with-non-invertible-ctm-expected.txt

    r39508 r59768  
    44
    55
    6 PASS ctx.fillStyle is "green"
     6PASS ctx.fillStyle is "#008000"
    77PASS imageData.data[0] is 0
    88PASS imageData.data[1] is 128
    99PASS imageData.data[2] is 0
    10 PASS ctx.strokeStyle is "green"
     10PASS ctx.strokeStyle is "#008000"
    1111PASS imageData.data[0] is 0
    1212PASS imageData.data[1] is 128
  • trunk/LayoutTests/fast/canvas/script-tests/canvas-set-properties-with-non-invertible-ctm.js

    r48550 r59768  
    1717ctx.scale(0, 0);
    1818ctx.fillStyle = "green";
    19 shouldBe('ctx.fillStyle', '"green"');
     19shouldBe('ctx.fillStyle', '"#008000"');
    2020ctx.setTransform(1, 0, 0, 1, 0, 0);
    2121ctx.fillRect(0,0,100,100);
     
    2929ctx.scale(0, 0);
    3030ctx.strokeStyle = "green";
    31 shouldBe('ctx.strokeStyle', '"green"');
     31shouldBe('ctx.strokeStyle', '"#008000"');
    3232ctx.lineWidth = 100;
    3333ctx.setTransform(1, 0, 0, 1, 0, 0);
  • trunk/LayoutTests/fast/dom/canvasContext2d-element-attribute-js-null-expected.txt

    r30635 r59768  
    44TEST SUCCEEDED: The value was the string 'butt'. [tested CanvasRenderingContext2D.lineCap]
    55TEST SUCCEEDED: The value was the string 'miter'. [tested CanvasRenderingContext2D.lineJoin]
    6 TEST SUCCEEDED: The value was the empty string. [tested CanvasRenderingContext2D.shadowColor]
     6TEST SUCCEEDED: The value was the string 'rgba(0, 0, 0, 0.0)'. [tested CanvasRenderingContext2D.shadowColor]
    77
    88
  • trunk/LayoutTests/fast/dom/canvasContext2d-element-attribute-js-null.html

    r21504 r59768  
    5555                        {name: 'lineCap', expectedNull: 'butt'},
    5656                        {name: 'lineJoin', expectedNull: 'miter'},
    57                         {name: 'shadowColor', expectedNull: ''}                       
     57                        {name: 'shadowColor', expectedNull: 'rgba(0, 0, 0, 0.0)'}
    5858                    ]
    5959                }
  • trunk/WebCore/ChangeLog

    r59767 r59768  
     12010-05-19  Andreas Kling  <andreas.kling@nokia.com>
     2
     3        Reviewed by Darin Adler.
     4
     5        CanvasRenderingContext2D's property getters that return colors should
     6        serialize them in accordance with the HTML5 spec (4.8.11.1.4)
     7        Setters should ignore invalid colors.
     8
     9        Also, shadowColor should initially be transparent black (4.8.11.1.6)
     10
     11        https://bugs.webkit.org/show_bug.cgi?id=38845
     12
     13        Refactored CanvasStyle to store an RGBA32 instead of RGBA components and/or the color string.
     14
     15        Spec links:
     16        http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
     17        http://www.whatwg.org/specs/web-apps/current-work/#dom-context-2d-shadowcolor
     18
     19        * html/canvas/CanvasRenderingContext2D.cpp:
     20        (WebCore::CanvasRenderingContext2D::State::State):
     21        (WebCore::CanvasRenderingContext2D::shadowColor):
     22        (WebCore::CanvasRenderingContext2D::setShadowColor):
     23        (WebCore::CanvasRenderingContext2D::setShadow):
     24        (WebCore::CanvasRenderingContext2D::clearShadow):
     25        (WebCore::CanvasRenderingContext2D::applyShadow):
     26        * html/canvas/CanvasRenderingContext2D.h:
     27        * html/canvas/CanvasStyle.cpp:
     28        (WebCore::CanvasStyle::CanvasStyle):
     29        (WebCore::CanvasStyle::create):
     30        (WebCore::CanvasStyle::applyStrokeColor):
     31        (WebCore::CanvasStyle::applyFillColor):
     32        * html/canvas/CanvasStyle.h:
     33        (WebCore::CanvasStyle::create):
     34        (WebCore::CanvasStyle::color):
     35        (WebCore::CanvasStyle::):
     36        (WebCore::CanvasStyle::CMYKAValues::CMYKAValues):
     37        * platform/graphics/Color.cpp:
     38        (WebCore::Color::serialized):
     39        * platform/graphics/Color.h:
     40
    1412010-05-19  Andreas Kling  <andreas.kling@nokia.com>
    242
  • trunk/WebCore/html/canvas/CanvasRenderingContext2D.cpp

    r59613 r59768  
    11/*
    22 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
    3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
     3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
    44 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
    55 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
     
    119119
    120120CanvasRenderingContext2D::State::State()
    121     : m_strokeStyle(CanvasStyle::create("#000000"))
    122     , m_fillStyle(CanvasStyle::create("#000000"))
     121    : m_strokeStyle(CanvasStyle::create(Color::black))
     122    , m_fillStyle(CanvasStyle::create(Color::black))
    123123    , m_lineWidth(1)
    124124    , m_lineCap(ButtCap)
     
    126126    , m_miterLimit(10)
    127127    , m_shadowBlur(0)
    128     , m_shadowColor("black")
     128    , m_shadowColor(Color::transparent)
    129129    , m_globalAlpha(1)
    130130    , m_globalComposite(CompositeSourceOver)
     
    316316String CanvasRenderingContext2D::shadowColor() const
    317317{
    318     // FIXME: What should this return if you called setShadow with a non-string color?
    319     return state().m_shadowColor;
     318    return Color(state().m_shadowColor).serialized();
    320319}
    321320
    322321void CanvasRenderingContext2D::setShadowColor(const String& color)
    323322{
    324     state().m_shadowColor = color;
     323    if (!CSSParser::parseColor(state().m_shadowColor, color))
     324        return;
     325
    325326    applyShadow();
    326327}
     
    815816    state().m_shadowOffset = FloatSize(width, height);
    816817    state().m_shadowBlur = blur;
    817     state().m_shadowColor = "";
     818    state().m_shadowColor = Color::transparent;
    818819    applyShadow();
    819820}
     
    821822void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color)
    822823{
     824    if (!CSSParser::parseColor(state().m_shadowColor, color))
     825        return;
     826
    823827    state().m_shadowOffset = FloatSize(width, height);
    824828    state().m_shadowBlur = blur;
    825     state().m_shadowColor = color;
    826829    applyShadow();
    827830}
     
    831834    state().m_shadowOffset = FloatSize(width, height);
    832835    state().m_shadowBlur = blur;
    833     state().m_shadowColor = "";
    834 
    835     GraphicsContext* c = drawingContext();
    836     if (!c)
    837         return;
    838 
    839     RGBA32 rgba = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f);
    840     c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace);
     836    state().m_shadowColor = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f);
     837
     838    GraphicsContext* c = drawingContext();
     839    if (!c)
     840        return;
     841
     842    c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace);
    841843}
    842844
    843845void CanvasRenderingContext2D::setShadow(float width, float height, float blur, const String& color, float alpha)
    844846{
     847    RGBA32 rgba;
     848
     849    if (!CSSParser::parseColor(rgba, color))
     850        return;
     851
     852    state().m_shadowColor = colorWithOverrideAlpha(rgba, alpha);
    845853    state().m_shadowOffset = FloatSize(width, height);
    846854    state().m_shadowBlur = blur;
    847     state().m_shadowColor = color;
    848 
    849     GraphicsContext* c = drawingContext();
    850     if (!c)
    851         return;
    852 
    853     RGBA32 rgba = 0; // default is transparent black
    854     if (!state().m_shadowColor.isEmpty())
    855         CSSParser::parseColor(rgba, state().m_shadowColor);
    856     c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(colorWithOverrideAlpha(rgba, alpha)), DeviceColorSpace);
     855
     856    GraphicsContext* c = drawingContext();
     857    if (!c)
     858        return;
     859
     860    c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace);
    857861}
    858862
     
    861865    state().m_shadowOffset = FloatSize(width, height);
    862866    state().m_shadowBlur = blur;
    863     state().m_shadowColor = "";
    864 
    865     GraphicsContext* c = drawingContext();
    866     if (!c)
    867         return;
    868 
    869     RGBA32 rgba = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha);
    870     c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace);
     867    state().m_shadowColor = makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha);
     868
     869    GraphicsContext* c = drawingContext();
     870    if (!c)
     871        return;
     872
     873    c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace);
    871874}
    872875
     
    875878    state().m_shadowOffset = FloatSize(width, height);
    876879    state().m_shadowBlur = blur;
    877     state().m_shadowColor = "";
    878 
    879     GraphicsContext* c = drawingContext();
    880     if (!c)
    881         return;
    882 
    883     RGBA32 rgba = makeRGBA32FromFloats(r, g, b, a); // default is transparent black
    884     c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace);
     880    state().m_shadowColor = makeRGBA32FromFloats(r, g, b, a);
     881
     882    GraphicsContext* c = drawingContext();
     883    if (!c)
     884        return;
     885
     886    c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace);
    885887}
    886888
     
    889891    state().m_shadowOffset = FloatSize(width, height);
    890892    state().m_shadowBlur = blur;
    891     state().m_shadowColor = "";
     893    state().m_shadowColor = makeRGBAFromCMYKA(c, m, y, k, a);
    892894
    893895    GraphicsContext* dc = drawingContext();
     
    902904    CGColorRelease(shadowColor);
    903905#else
    904     dc->setShadow(IntSize(width, -height), blur, Color(c, m, y, k, a), DeviceColorSpace);
     906    dc->setShadow(IntSize(width, -height), blur, state().m_shadowColor, DeviceColorSpace);
    905907#endif
    906908}
     
    910912    state().m_shadowOffset = FloatSize();
    911913    state().m_shadowBlur = 0;
    912     state().m_shadowColor = "";
     914    state().m_shadowColor = Color::transparent;
    913915    applyShadow();
    914916}
     
    920922        return;
    921923
    922     RGBA32 rgba = 0; // default is transparent black
    923     if (!state().m_shadowColor.isEmpty())
    924         CSSParser::parseColor(rgba, state().m_shadowColor);
    925924    float width = state().m_shadowOffset.width();
    926925    float height = state().m_shadowOffset.height();
    927     c->setShadow(IntSize(width, -height), state().m_shadowBlur, Color(rgba), DeviceColorSpace);
     926    c->setShadow(IntSize(width, -height), state().m_shadowBlur, state().m_shadowColor, DeviceColorSpace);
    928927}
    929928
  • trunk/WebCore/html/canvas/CanvasRenderingContext2D.h

    r59613 r59768  
    2929#include "AffineTransform.h"
    3030#include "CanvasRenderingContext.h"
     31#include "Color.h"
    3132#include "FloatSize.h"
    3233#include "Font.h"
     
    220221            FloatSize m_shadowOffset;
    221222            float m_shadowBlur;
    222             String m_shadowColor;
     223            RGBA32 m_shadowColor;
    223224            float m_globalAlpha;
    224225            CompositeOperator m_globalComposite;
  • trunk/WebCore/html/canvas/CanvasStyle.cpp

    r56155 r59768  
    11/*
    22 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
    3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
     3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
    44 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
    55 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
     
    4949namespace WebCore {
    5050
    51 CanvasStyle::CanvasStyle(const String& color)
    52     : m_type(ColorString)
    53     , m_color(color)
     51CanvasStyle::CanvasStyle(RGBA32 rgba)
     52    : m_type(RGBA)
     53    , m_rgba(rgba)
    5454{
    5555}
    5656
    5757CanvasStyle::CanvasStyle(float grayLevel)
    58     : m_type(GrayLevel)
    59     , m_alpha(1)
    60     , m_grayLevel(grayLevel)
    61 {
    62 }
    63 
    64 CanvasStyle::CanvasStyle(const String& color, float alpha)
    65     : m_type(ColorStringWithAlpha)
    66     , m_color(color)
    67     , m_alpha(alpha)
     58    : m_type(RGBA)
     59    , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, 1.0f))
    6860{
    6961}
    7062
    7163CanvasStyle::CanvasStyle(float grayLevel, float alpha)
    72     : m_type(GrayLevel)
    73     , m_alpha(alpha)
    74     , m_grayLevel(grayLevel)
     64    : m_type(RGBA)
     65    , m_rgba(makeRGBA32FromFloats(grayLevel, grayLevel, grayLevel, alpha))
    7566{
    7667}
     
    7869CanvasStyle::CanvasStyle(float r, float g, float b, float a)
    7970    : m_type(RGBA)
    80     , m_alpha(a)
    81     , m_red(r)
    82     , m_green(g)
    83     , m_blue(b)
     71    , m_rgba(makeRGBA32FromFloats(r, g, b, a))
    8472{
    8573}
     
    8775CanvasStyle::CanvasStyle(float c, float m, float y, float k, float a)
    8876    : m_type(CMYKA)
    89     , m_alpha(a)
    90     , m_cyan(c)
    91     , m_magenta(m)
    92     , m_yellow(y)
    93     , m_black(k)
     77    , m_rgba(makeRGBAFromCMYKA(c, m, y, k, a))
     78    , m_cmyka(c, m, y, k, a)
    9479{
    9580}
    9681
    9782CanvasStyle::CanvasStyle(PassRefPtr<CanvasGradient> gradient)
    98     : m_type(gradient ? Gradient : ColorString)
     83    : m_type(Gradient)
    9984    , m_gradient(gradient)
    10085{
     
    10287
    10388CanvasStyle::CanvasStyle(PassRefPtr<CanvasPattern> pattern)
    104     : m_type(pattern ? ImagePattern : ColorString)
     89    : m_type(ImagePattern)
    10590    , m_pattern(pattern)
    10691{
     92}
     93
     94PassRefPtr<CanvasStyle> CanvasStyle::create(const String& color)
     95{
     96    RGBA32 rgba;
     97    if (!CSSParser::parseColor(rgba, color))
     98        return 0;
     99    return adoptRef(new CanvasStyle(rgba));
     100}
     101
     102PassRefPtr<CanvasStyle> CanvasStyle::create(const String& color, float alpha)
     103{
     104    RGBA32 rgba;
     105    if (!CSSParser::parseColor(rgba, color))
     106        return 0;
     107    return adoptRef(new CanvasStyle(colorWithOverrideAlpha(rgba, alpha)));
     108}
     109
     110PassRefPtr<CanvasStyle> CanvasStyle::create(PassRefPtr<CanvasGradient> gradient)
     111{
     112    if (!gradient)
     113        return 0;
     114    return adoptRef(new CanvasStyle(gradient));
     115}
     116PassRefPtr<CanvasStyle> CanvasStyle::create(PassRefPtr<CanvasPattern> pattern)
     117{
     118    if (!pattern)
     119        return 0;
     120    return adoptRef(new CanvasStyle(pattern));
    107121}
    108122
     
    112126        return;
    113127    switch (m_type) {
    114         case ColorString: {
    115             Color c = Color(m_color);
    116             if (c.isValid()) {
    117                 context->setStrokeColor(c.rgb(), DeviceColorSpace);
    118                 break;
    119             }
    120             RGBA32 color = 0; // default is transparent black
    121             if (CSSParser::parseColor(color, m_color))
    122                 context->setStrokeColor(color, DeviceColorSpace);
    123             break;
    124         }
    125         case ColorStringWithAlpha: {
    126             Color c = Color(m_color);
    127             if (c.isValid()) {
    128                 context->setStrokeColor(colorWithOverrideAlpha(c.rgb(), m_alpha), DeviceColorSpace);
    129                 break;
    130             }
    131             RGBA32 color = 0; // default is transparent black
    132             if (CSSParser::parseColor(color, m_color))
    133                 context->setStrokeColor(colorWithOverrideAlpha(color, m_alpha), DeviceColorSpace);
    134             break;
    135         }
    136         case GrayLevel:
    137             // We're only supporting 255 levels of gray here.  Since this isn't
    138             // even part of HTML5, I don't expect anyone will care.  If they do
    139             // we'll make a fancier Color abstraction.
    140             context->setStrokeColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha), DeviceColorSpace);
    141             break;
    142         case RGBA:
    143             context->setStrokeColor(Color(m_red, m_green, m_blue, m_alpha), DeviceColorSpace);
    144             break;
    145         case CMYKA: {
    146             // FIXME: Do this through platform-independent GraphicsContext API.
    147             // We'll need a fancier Color abstraction to support CYMKA correctly
     128    case RGBA:
     129        context->setStrokeColor(m_rgba, DeviceColorSpace);
     130        break;
     131    case CMYKA: {
     132        // FIXME: Do this through platform-independent GraphicsContext API.
     133        // We'll need a fancier Color abstraction to support CMYKA correctly
    148134#if PLATFORM(CG)
    149             CGContextSetCMYKStrokeColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
     135        CGContextSetCMYKStrokeColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
    150136#elif PLATFORM(QT)
    151             QPen currentPen = context->platformContext()->pen();
    152             QColor clr;
    153             clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
    154             currentPen.setColor(clr);
    155             context->platformContext()->setPen(currentPen);
     137        QPen currentPen = context->platformContext()->pen();
     138        QColor clr;
     139        clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
     140        currentPen.setColor(clr);
     141        context->platformContext()->setPen(currentPen);
    156142#else
    157             context->setStrokeColor(Color(m_cyan, m_magenta, m_yellow, m_black, m_alpha), DeviceColorSpace);
     143        context->setStrokeColor(m_rgba, DeviceColorSpace);
    158144#endif
    159             break;
    160         }
    161         case Gradient:
    162             context->setStrokeGradient(canvasGradient()->gradient());
    163             break;
    164         case ImagePattern:
    165             context->setStrokePattern(canvasPattern()->pattern());
    166             break;
     145        break;
     146    }
     147    case Gradient:
     148        context->setStrokeGradient(canvasGradient()->gradient());
     149        break;
     150    case ImagePattern:
     151        context->setStrokePattern(canvasPattern()->pattern());
     152        break;
    167153    }
    168154}
     
    173159        return;
    174160    switch (m_type) {
    175         case ColorString: {
    176             RGBA32 rgba = 0; // default is transparent black
    177             if (CSSParser::parseColor(rgba, m_color))
    178                 context->setFillColor(rgba, DeviceColorSpace);
    179             break;
    180         }
    181         case ColorStringWithAlpha: {
    182             RGBA32 color = 0; // default is transparent black
    183             if (CSSParser::parseColor(color, m_color))
    184                 context->setFillColor(colorWithOverrideAlpha(color, m_alpha), DeviceColorSpace);
    185             break;
    186         }
    187         case GrayLevel:
    188             // We're only supporting 255 levels of gray here.  Since this isn't
    189             // even part of HTML5, I don't expect anyone will care.  If they do
    190             // we'll make a fancier Color abstraction.
    191             context->setFillColor(Color(m_grayLevel, m_grayLevel, m_grayLevel, m_alpha), DeviceColorSpace);
    192             break;
    193         case RGBA:
    194             context->setFillColor(Color(m_red, m_green, m_blue, m_alpha), DeviceColorSpace);
    195             break;
    196         case CMYKA: {
    197             // FIXME: Do this through platform-independent GraphicsContext API.
    198             // We'll need a fancier Color abstraction to support CYMKA correctly
     161    case RGBA:
     162        context->setFillColor(m_rgba, DeviceColorSpace);
     163        break;
     164    case CMYKA: {
     165        // FIXME: Do this through platform-independent GraphicsContext API.
     166        // We'll need a fancier Color abstraction to support CMYKA correctly
    199167#if PLATFORM(CG)
    200             CGContextSetCMYKFillColor(context->platformContext(), m_cyan, m_magenta, m_yellow, m_black, m_alpha);
     168        CGContextSetCMYKFillColor(context->platformContext(), m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
    201169#elif PLATFORM(QT)
    202             QBrush currentBrush = context->platformContext()->brush();
    203             QColor clr;
    204             clr.setCmykF(m_cyan, m_magenta, m_yellow, m_black, m_alpha);
    205             currentBrush.setColor(clr);
    206             context->platformContext()->setBrush(currentBrush);
     170        QBrush currentBrush = context->platformContext()->brush();
     171        QColor clr;
     172        clr.setCmykF(m_cmyka.c, m_cmyka.m, m_cmyka.y, m_cmyka.k, m_cmyka.a);
     173        currentBrush.setColor(clr);
     174        context->platformContext()->setBrush(currentBrush);
    207175#else
    208             context->setFillColor(Color(m_cyan, m_magenta, m_yellow, m_black, m_alpha), DeviceColorSpace);
     176        context->setFillColor(m_rgba, DeviceColorSpace);
    209177#endif
    210             break;
    211         }
    212         case Gradient:
    213             context->setFillGradient(canvasGradient()->gradient());
    214             break;
    215         case ImagePattern:
    216             context->setFillPattern(canvasPattern()->pattern());
    217             break;
     178        break;
     179    }
     180    case Gradient:
     181        context->setFillGradient(canvasGradient()->gradient());
     182        break;
     183    case ImagePattern:
     184        context->setFillPattern(canvasPattern()->pattern());
     185        break;
    218186    }
    219187}
  • trunk/WebCore/html/canvas/CanvasStyle.h

    r49734 r59768  
    11/*
    22 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
     3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
    34 *
    45 * Redistribution and use in source and binary forms, with or without
     
    2728#define CanvasStyle_h
    2829
     30#include "Color.h"
    2931#include "PlatformString.h"
    3032
     
    3739    class CanvasStyle : public RefCounted<CanvasStyle> {
    3840    public:
    39         static PassRefPtr<CanvasStyle> create(const String& color) { return adoptRef(new CanvasStyle(color)); }
     41        static PassRefPtr<CanvasStyle> create(RGBA32 rgba) { return adoptRef(new CanvasStyle(rgba)); }
     42        static PassRefPtr<CanvasStyle> create(const String& color);
     43        static PassRefPtr<CanvasStyle> create(const String& color, float alpha);
    4044        static PassRefPtr<CanvasStyle> create(float grayLevel) { return adoptRef(new CanvasStyle(grayLevel)); }
    41         static PassRefPtr<CanvasStyle> create(const String& color, float alpha) { return adoptRef(new CanvasStyle(color, alpha)); }
    4245        static PassRefPtr<CanvasStyle> create(float grayLevel, float alpha) { return adoptRef(new CanvasStyle(grayLevel, alpha)); }
    4346        static PassRefPtr<CanvasStyle> create(float r, float g, float b, float a) { return adoptRef(new CanvasStyle(r, g, b, a)); }
    4447        static PassRefPtr<CanvasStyle> create(float c, float m, float y, float k, float a) { return adoptRef(new CanvasStyle(c, m, y, k, a)); }
    45         static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasGradient> gradient) { return adoptRef(new CanvasStyle(gradient)); }
    46         static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasPattern> pattern) { return adoptRef(new CanvasStyle(pattern)); }
     48        static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasGradient> gradient);
     49        static PassRefPtr<CanvasStyle> create(PassRefPtr<CanvasPattern> pattern);
    4750
    48         String color() const { return m_color; }
     51        String color() const { return Color(m_rgba).serialized(); }
    4952        CanvasGradient* canvasGradient() const { return m_gradient.get(); }
    5053        CanvasPattern* canvasPattern() const { return m_pattern.get(); }
     
    5457
    5558    private:
    56         CanvasStyle(const String& color);
     59        CanvasStyle(RGBA32 rgba);
    5760        CanvasStyle(float grayLevel);
    58         CanvasStyle(const String& color, float alpha);
    5961        CanvasStyle(float grayLevel, float alpha);
    6062        CanvasStyle(float r, float g, float b, float a);
     
    6365        CanvasStyle(PassRefPtr<CanvasPattern>);
    6466
    65         enum Type { ColorString, ColorStringWithAlpha, GrayLevel, RGBA, CMYKA, Gradient, ImagePattern };
     67        enum Type { RGBA, CMYKA, Gradient, ImagePattern };
    6668
    6769        Type m_type;
    6870
    69         String m_color;
     71        RGBA32 m_rgba;
     72
    7073        RefPtr<CanvasGradient> m_gradient;
    7174        RefPtr<CanvasPattern> m_pattern;
    7275
    73         float m_alpha;
    74 
    75         float m_grayLevel;
    76 
    77         float m_red;
    78         float m_green;
    79         float m_blue;
    80 
    81         float m_cyan;
    82         float m_magenta;
    83         float m_yellow;
    84         float m_black;
     76        struct CMYKAValues {
     77            CMYKAValues() {}
     78            CMYKAValues(float cyan, float magenta, float yellow, float black, float alpha) : c(cyan), m(magenta), y(yellow), k(black), a(alpha) {}
     79            float c;
     80            float m;
     81            float y;
     82            float k;
     83            float a;
     84        } m_cmyka;
    8585    };
    8686
  • trunk/WebCore/platform/graphics/Color.cpp

    r44503 r59768  
    179179}
    180180
     181String Color::serialized() const
     182{
     183    if (alpha() == 0xFF)
     184        return String::format("#%02x%02x%02x", red(), green(), blue());
     185
     186    // Match Gecko ("0.0" for zero, 5 decimals for anything else)
     187    if (!alpha())
     188        return String::format("rgba(%u, %u, %u, 0.0)", red(), green(), blue());
     189
     190    return String::format("rgba(%u, %u, %u, %.5f)", red(), green(), blue(), alpha() / 255.0f);
     191}
     192
    181193String Color::name() const
    182194{
  • trunk/WebCore/platform/graphics/Color.h

    r55633 r59768  
    8181    explicit Color(const String&);
    8282    explicit Color(const char*);
    83    
     83
     84    // Returns the color serialized according to HTML5
     85    // - http://www.whatwg.org/specs/web-apps/current-work/#serialization-of-a-color
     86    String serialized() const;
     87
    8488    String name() const;
    8589    void setNamedColor(const String&);
Note: See TracChangeset for help on using the changeset viewer.