Skip to content

Commit 073f754

Browse files
committed
[css-color-4] Added an HTML-Compatible hex serialization for sRGB, ##10550
1 parent c955f5a commit 073f754

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

css-color-4/Overview.bs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6006,6 +6006,10 @@ In this section, the strings used in the specification and the corresponding cha
60066006
<td>" "</td>
60076007
<td>U+0020 SPACE</td>
60086008
</tr>
6009+
<tr>
6010+
<td>"#"</td>
6011+
<td>U+0023 NUMBER SIGN</td>
6012+
</tr>
60096013
<tr>
60106014
<td>","</td>
60116015
<td>U+002C COMMA</td>
@@ -6192,6 +6196,67 @@ Serializing sRGB values</h3>
61926196
the declared, computed and used value
61936197
is the corresponding sRGB value.
61946198

6199+
<h4 id="HTML-compatible-serialization-of-srgb">HTML-compatible serialization of sRGB values</h4>
6200+
6201+
If the following conditions are all true:
6202+
6203+
<ol>
6204+
<li>The color space is sRGB</li>
6205+
<li>The alpha is 1</li>
6206+
<li>There are no [=missing=] values</li>
6207+
<li>The RGB component values are internally represented as integers between 0 and 255 inclusive (i.e. 8-bit unsigned integer)</li>
6208+
<li>HTMLCompatible serialization is requested</li>
6209+
</ol>
6210+
6211+
Then corresponding sRGB values are serialized in 6-digit [=hex color notation=] as follows:
6212+
6213+
A seven-character string consisting of the character "#", followed immediately by the two-digit hexadecimal representations of the red component, the green component, and the blue component, in that order, using <a href="https://infra.spec.whatwg.org/#ascii-lower-hex-digit">ASCII lower hex digits</a>. No spaces are permitted.
6214+
6215+
<div class="example" id="ex-canvas-srgb">
6216+
For example, the pixel value from a canvas 2D context
6217+
using the sRGB colorspace is returned as a Uint8ClampedArray:
6218+
6219+
<pre lang-js>
6220+
imageData = context.getImageData(0, 0, 1, 1);
6221+
console.log(imageData.colorSpace); // "srgb"
6222+
console.log([...imageData.data]); // [37, 164, 87, 255]
6223+
</pre>
6224+
6225+
The color space is sRGB, the representation is 8 bits per component, the data format does not produce ''none'' values nor does it support extended range values, and the alpha is 1 (255/255).
6226+
6227+
The HTML-compatible serialization is the string "#25a457" (not "#25A457").
6228+
</div>
6229+
6230+
Otherwise, for sRGB the <a href="#css-serialization-of-srgb">CSS serialization of sRGB values is used</a> and for other color spaces, the relevant <a href="#serializing-color-values">serialization</a> of the <<color>> value.
6231+
6232+
<div class="example" id="ex-canvas-p3">
6233+
For example, the pixel value from a canvas 2D context,
6234+
this time using the display-p3 colorspace is returned,
6235+
again as a Uint8ClampedArray:
6236+
6237+
<pre lang-js>
6238+
imageData = context.getImageData(0, 0, 1, 1);
6239+
console.log(imageData.colorSpace); // "display-p3"
6240+
console.log([...imageData.data]); // [80, 162, 95, 255]
6241+
</pre>
6242+
6243+
The CSS serialization is the string "color(display-p3 0.3137 0.6353 0.3725)".
6244+
</div>
6245+
6246+
<div class="example" id="ex-srgb-alpha">
6247+
For example, a color picker has returned the following values,
6248+
in the sRGB colorspace:
6249+
6250+
<pre lang-js>
6251+
[37, 164, 87, 237]
6252+
</pre>
6253+
6254+
The alpha is not 1, so the CSS serialization is the string
6255+
"rgba(37, 164, 87, 0.93)".
6256+
</div>
6257+
6258+
<h4 id="css-serialization-of-srgb">CSS serialization of sRGB values</h4>
6259+
61956260
Corresponding sRGB values use either the ''rgb()'' or ''rgba()'' form
61966261
(depending on whether the (clamped) alpha is exactly 1, or not),
61976262
with all <a href="https://infra.spec.whatwg.org/#ascii-lowercase">ASCII lowercase</a>

0 commit comments

Comments
 (0)