Skip to content

Commit 50c304f

Browse files
annevkdizhang168
authored andcommitted
Update 2D canvas color serialization
Although fillStyle, strokeStyle, and shadowColor setters accepted all kinds of CSS color values, those could not be serialized. Update that by relying on CSS Color for serialization instead, which now has an HTML-compatible serialization method to preserve compatibility with 2D canvas and <input type=color> for certain colors. While here, also link the algorithm to be used for color space conversion and correct the reference for 'relative-colorimetric'. Tests: web-platform-tests/wpt#47148. Fixes whatwg#8917.
1 parent 39aa58a commit 50c304f

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

source

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3697,8 +3697,10 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
36973697
<li><dfn data-x-href="https://drafts.csswg.org/css-color/#transparent-black">transparent black</dfn></li>
36983698
<li><dfn data-x-href="https://drafts.csswg.org/css-color/#valdef-color-srgb">'srgb'</dfn> color space</li>
36993699
<li><dfn data-x-href="https://drafts.csswg.org/css-color/#valdef-color-display-p3">'display-p3'</dfn> color space</li>
3700-
<li><dfn data-x-href="https://drafts.csswg.org/css-color/#valdef-color-profile-rendering-intent-relative-colorimetric">'relative-colorimetric'</dfn> rendering intent</li>
3700+
<li><dfn data-x-href="https://drafts.csswg.org/css-color-5/#valdef-color-profile-rendering-intent-relative-colorimetric">'relative-colorimetric'</dfn> rendering intent</li>
37013701
<li><dfn id="parsed-as-a-css-color-value" data-x-href="https://drafts.csswg.org/css-color/#parse-a-css-color-value">parse a CSS &lt;color> value</dfn></li>
3702+
<li><!--en-GB--><dfn id="serialisation-of-a-color" data-x-href="https://drafts.csswg.org/css-color/#serializing-color-values">serialize a CSS &lt;color> value</dfn> including <dfn data-x-href="https://drafts.csswg.org/css-color/#color-serialization-html-compatible-serialization-is-requested">HTML-compatible serialization is requested</dfn></li>
3703+
<li><dfn data-x-href="https://drafts.csswg.org/css-color/#color-conversion">Converting Colors</dfn></li>
37023704
</ul>
37033705

37043706
<p>The following terms are defined in <cite>CSS Images</cite>: <ref>CSSIMAGES</ref></p>
@@ -65181,9 +65183,8 @@ context.fillRect(100,0,50,50); // only this square remains</code></pre>
6518165183
data-x="dom-PredefinedColorSpace-display-p3">display-p3</code></dfn>" value indicates the
6518265184
<span>'display-p3'</span> color space.</p>
6518365185

65184-
<p class="note">Algorithms for converting between color spaces are found in the <a
65185-
href="https://drafts.csswg.org/css-color/#predefined">Predefined color spaces</a> section of
65186-
<cite>CSS Color</cite>. <ref>CSSCOLOR</ref></p>
65186+
<p class="note">The algorithm for converting between color spaces can be found in the
65187+
<span>Converting Colors</span> section of <cite>CSS Color</cite>. <ref>CSSCOLOR</ref></p>
6518765188

6518865189
<hr>
6518965190

@@ -67879,8 +67880,9 @@ try {
6787967880

6788067881
<ol>
6788167882
<li><p>If <span>this</span>'s <span data-x="concept-CanvasFillStrokeStyles-fill-style">fill
67882-
style</span> is a CSS color, then return the <span data-x="serialization of a
67883-
color">serialization</span> of that color.</p></li>
67883+
style</span> is a CSS color, then return the <span data-x="serialize a CSS &lt;color>
67884+
value">serialization</span> of that color with <span data-x="HTML-compatible serialization is
67885+
requested">HTML-compatible serialization requested</span>.</p></li>
6788467886

6788567887
<li><p>Return <span>this</span>'s <span data-x="concept-CanvasFillStrokeStyles-fill-style">fill
6788667888
style</span>.</p></li>
@@ -67923,8 +67925,9 @@ try {
6792367925

6792467926
<ol>
6792567927
<li><p>If <span>this</span>'s <span data-x="concept-CanvasFillStrokeStyles-stroke-style">stroke
67926-
style</span> is a CSS color, then return the <span data-x="serialization of a
67927-
color">serialization</span> of that color.</p></li>
67928+
style</span> is a CSS color, then return the <span data-x="serialize a CSS &lt;color>
67929+
value">serialization</span> of that color with <span data-x="HTML-compatible serialization is
67930+
requested">HTML-compatible serialization requested</span>.</p></li>
6792867931

6792967932
<li><p>Return <span>this</span>'s <span data-x="concept-CanvasFillStrokeStyles-stroke-style">stroke
6793067933
style</span>.</p></li>
@@ -67963,26 +67966,6 @@ try {
6796367966
style</span> to the given value.</p></li>
6796467967
</ol>
6796567968

67966-
<p>The <!--en-GB--><dfn id="serialisation-of-a-color">serialization of a color</dfn> for a color
67967-
value is a string, computed as follows: if it has alpha equal to 1.0, then the string is a
67968-
lowercase six-digit hex value, prefixed with a "#" character (U+0023 NUMBER SIGN), with the first
67969-
two digits representing the red component, the next two digits representing the green component,
67970-
and the last two digits representing the blue component, the digits being <span data-x="ASCII
67971-
lower hex digit">ASCII lower hex digits</span>. Otherwise, the color value has alpha less than
67972-
1.0, and the string is the color value in the CSS <code data-x="">rgba()</code>
67973-
functional-notation format: "<code data-x="">rgba</code>" (U+0072 U+0067 U+0062
67974-
U+0061) followed by a U+0028 LEFT PARENTHESIS, a base-ten integer in the range 0-255 representing
67975-
the red component (using <span>ASCII digits</span> in the shortest form possible), a literal
67976-
U+002C COMMA and U+0020 SPACE, an integer for the green component, a comma and a space, an integer
67977-
for the blue component, another comma and space, a U+0030 DIGIT ZERO, if the alpha value is
67978-
greater than zero then a U+002E FULL STOP (representing the decimal point), if the alpha value is
67979-
greater than zero then one or more <span>ASCII digits</span> representing the fractional part of
67980-
the alpha<!-- value with no trailing zeros (implied by next sentence)-->, and finally a U+0029
67981-
RIGHT PARENTHESIS. User agents must express the fractional part of the alpha value, if any, with
67982-
the level of precision necessary for the alpha value, when reparsed, to be interpreted as the same
67983-
alpha value.</p>
67984-
<!-- if people complain this is unreadable, expand it into a <dl> with two nested <ol>s -->
67985-
6798667969
</div>
6798767970

6798867971
<hr>
@@ -69836,8 +69819,10 @@ console.log(pixels.data[2]);
6983669819

6983769820
<p>The <dfn attribute for="CanvasShadowStyles"><code
6983869821
data-x="dom-context-2d-shadowColor">shadowColor</code></dfn> getter steps are to return the <span
69839-
data-x="serialization of a color">serialization</span> of <span>this</span>'s <span
69840-
data-x="concept-CanvasShadowStyles-shadow-color">shadow color</span>.</p>
69822+
data-x="serialize a CSS &lt;color> value">serialization</span> of <span>this</span>'s <span
69823+
data-x="concept-CanvasShadowStyles-shadow-color">shadow color</span> with <span
69824+
data-x="HTML-compatible serialization is requested">HTML-compatible serialization
69825+
requested</span>.</p>
6984169826

6984269827
<p>The <code data-x="dom-context-2d-shadowColor">shadowColor</code> setter steps are:</p>
6984369828

@@ -71073,17 +71058,18 @@ interface <dfn interface>OffscreenCanvasRenderingContext2D</dfn> {
7107371058
backing store. The default backing store color space for all canvas APIs is
7107471059
<span>'srgb'</span>.</p>
7107571060

71076-
<p>Color space conversion must be applied to the canvas's backing store when rendering the canvas
71077-
to the output device. This color space conversion must be identical to the color space conversion
71078-
that would be applied to an <code>img</code> element with a color profile that specifies the same
71079-
<span data-x="concept-canvas-color-space">color space</span> as the canvas's backing store.</p>
71061+
<p><span data-x="Converting Colors">Color space conversion</span> must be applied to the canvas's
71062+
backing store when rendering the canvas to the output device. This color space conversion must be
71063+
identical to the color space conversion that would be applied to an <code>img</code> element with
71064+
a color profile that specifies the same <span data-x="concept-canvas-color-space">color
71065+
space</span> as the canvas's backing store.</p>
7108071066

71081-
<p>When drawing content to a 2D context, all inputs must be converted to the <span
71082-
data-x="concept-canvas-color-space">context's color space</span> before drawing. Interpolation of
71083-
gradient color stops must be performed on color values after conversion to the <span
71084-
data-x="concept-canvas-color-space">context's color space</span>. Alpha blending must be performed
71085-
on values after conversion to the <span data-x="concept-canvas-color-space">context's color
71086-
space</span>.</p>
71067+
<p>When drawing content to a 2D context, all inputs must be <span data-x="Converting
71068+
Colors">converted</span> to the <span data-x="concept-canvas-color-space">context's color
71069+
space</span> before drawing. Interpolation of gradient color stops must be performed on color
71070+
values after conversion to the <span data-x="concept-canvas-color-space">context's color
71071+
space</span>. Alpha blending must be performed on values after conversion to the <span
71072+
data-x="concept-canvas-color-space">context's color space</span>.</p>
7108771073

7108871074
<p class="note">There do not exist any inputs to a 2D context for which the color space is
7108971075
undefined. The color space for CSS colors is defined in <cite>CSS Color</cite>. The color space
@@ -71132,8 +71118,8 @@ interface <dfn interface>OffscreenCanvasRenderingContext2D</dfn> {
7113271118

7113371119
<p>For image types that support color profiles, the serialized image must include a color profile
7113471120
indicating the color space of the underlying bitmap. For image types that do not support color
71135-
profiles, the serialized image must be converted to the <span>'srgb'</span> color space using
71136-
<span>'relative-colorimetric'</span> rendering intent.</p>
71121+
profiles, the serialized image must be <span data-x="Converting Colors">converted</span> to the
71122+
<span>'srgb'</span> color space using <span>'relative-colorimetric'</span> rendering intent.</p>
7113771123

7113871124
<p class="note">Thus, in the 2D context, calling the <code
7113971125
data-x="dom-context-2d-drawImage">drawImage()</code> method to render the output of the <code

0 commit comments

Comments
 (0)