You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
APIs for Parsing and Manipulating Colors</span><aclass=self-linkhref=#apis></a></h2>
2591
2591
2592
-
<pclass=issueid=issue-dc028349><aclass=self-linkhref=#issue-dc028349></a>This section is in the process of being written and cleaned up.</p>
2592
+
<p>In many applications, authors have to use and manipulate colors.
2593
+
CSS allows many different useful syntaxes for colors,
2594
+
but these are only usable in stylesheets,
2595
+
not in script.</p>
2596
+
2597
+
<p>This section defines a number of APIs for creating and manipulating colors in script:</p>
2598
+
2599
+
<ul>
2600
+
<li>
2601
+
It allows CSS colors to be parsed into JS objects—<wbr>use the <code>CSSColor.parse("rebeccapurple")</code> method to get a corresponding color object,
2602
+
or one of the specific color constructors,
2603
+
like <code>new HSLColor("rebeccapurple")</code>,
2604
+
to get back a specific type of color class.
2605
+
2606
+
<li>
2607
+
It allows colors to be manipulated in any of the syntaxes that CSS exposes:
2608
+
<adata-link-type=idlhref=#dom-rgbcolortitle=rgbcolor>RGBColor</a> for RGBA colors, with components ranging from 0-1;
2609
+
<adata-link-type=idlhref=#dom-hexcolortitle=hexcolor>HexColor</a> for RGBA colors, with components ranging from 0-255;
2610
+
<adata-link-type=idlhref=#dom-hslcolortitle=hslcolor>HSLColor</a> for HSLA colors, corresponding to the <aclass=cssdata-link-type=maybehref=#funcdef-hslatitle=hsla()>hsla()</a> syntax;
2611
+
and <adata-link-type=idlhref=#dom-cmykcolortitle=cmykcolor>CMYKColor</a> for CMYK colors, corresponding to the <aclass=cssdata-link-type=maybehref=#funcdef-device-cmyktitle=device-cmyk()>device-cmyk()</a> syntax.
2612
+
2613
+
<p>All of these expose their components as attributes you can manipulate directly,
2614
+
and colors can be easily converted between any of the classes with methods like <code>toRGB()</code> and <code>toHSL()</code>.</p>
2615
+
2616
+
<li>
2617
+
It allows colors to be automatically stringified back into a form that CSS understands,
2618
+
so you can write code like:
2619
+
2620
+
<pre>var c = new HSLColor("red");
2621
+
c.l += .1;
2622
+
el.style.backgroundColor = c;
2623
+
// c is automatically turned into the string "rgba(100%, 20%, 20%, 100%)"
2624
+
</pre>
2625
+
2626
+
<p>If you wish, you can control what type of syntax it stringifies as,
2627
+
like <code>c.toString("hsl")</code>.</p>
2628
+
2629
+
<li>
2630
+
It’s explicitly designed to be easy to extend with new methods and new color types,
2631
+
and gives guidance to help you do it correctly (see <adata-section="" href=#api-extending>§15.5
Really? Shouldn’t video be consistent with images?
4429
4470
Or do implementations really do this differently?
4430
4471
4431
-
<ahref=#issue-9c7b92e0> ↵ </a></div><divclass=issue>This section is in the process of being written and cleaned up.<ahref=#issue-dc028349> ↵ </a></div>
0 commit comments