Skip to content

Commit 6485b0e

Browse files
committed
[css-color] Add CMYKColor class.
1 parent 983dcfb commit 6485b0e

2 files changed

Lines changed: 364 additions & 99 deletions

File tree

css-color/Overview.bs

Lines changed: 163 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,14 +1671,7 @@ CMYK Colors: the ''device-cmyk()'' function</h2>
16711671

16721672
The sixth argument specifies the fallback color,
16731673
used when the user agent doesn't know how to accurately transform the CMYK color to RGB.
1674-
If omitted, it defaults to an RGBA color calculated as follows:
1675-
1676-
<ul>
1677-
<li><code>red = 1.0 - min(1.0, cyan + black)</code>
1678-
<li><code>green = 1.0 - min(1.0, magenta + black)</code>
1679-
<li><code>blue = 1.0 - min(1.0, yellow + black)</code>
1680-
<li>Alpha remains as specified.
1681-
</ul>
1674+
If omitted, it defaults to the CMYK color <a>naively converted to RGBA</a>.
16821675

16831676
Typically, print-based applications will actually store the used colors as CMYK,
16841677
and send them to the printer in that form.
@@ -1710,6 +1703,52 @@ CMYK Colors: the ''device-cmyk()'' function</h2>
17101703
to avoid any color-matching difficulties.
17111704
</div>
17121705

1706+
<h3 id='cmyk-rgb'>
1707+
Converting Between CMYK and RGB-Based Colors</h3>
1708+
1709+
While most colors defined in this specification are directly compatible with RGBA,
1710+
and thus can be mechanically and consistently converted back and forth with it,
1711+
CMYK colors are not necessarily directly compatible;
1712+
a given CMYK color
1713+
might map to various RGBA colors
1714+
depending on the physical characteristics of the output device.
1715+
1716+
Ideally, the user agent will be aware of the output device's color profiles for RGBA and CMYK.
1717+
If this is true,
1718+
then the user agent must convert between CMYK and RGBA colors (and vice versa)
1719+
by first converting the color into an appropriate device-independent color space,
1720+
such as CIELab,
1721+
and then converting into the output color space,
1722+
using the appropriate color profiles for each operation.
1723+
1724+
This is not always possible, however.
1725+
In that case, the user agent must use the following naive conversion algorithms.
1726+
1727+
To <dfn title="naively convert from CMYK to RGBA | naively converted to RGBA">naively convert from CMYK to RGBA</dfn>:
1728+
1729+
If a fallback color was specified,
1730+
return that color
1731+
(converting it to RGB as well, if necessary).
1732+
Otherwise:
1733+
1734+
<ul>
1735+
<li><code>red = 1.0 - min(1.0, cyan + black)</code>
1736+
<li><code>green = 1.0 - min(1.0, magenta + black)</code>
1737+
<li><code>blue = 1.0 - min(1.0, yellow + black)</code>
1738+
<li>Alpha is same as for input color.
1739+
</ul>
1740+
1741+
To <dfn title="naively convert from RGBA to CMYK | naively converted to CMYK">naively convert from RGBA to CMYK</dfn>:
1742+
1743+
<ul>
1744+
<li>k = 1 - max(r, g, b)
1745+
<li>c = (1 - r - k) / (1 - k), or 0 if k is 1
1746+
<li>m = (1 - g - k) / (1 - k), or 0 if k is 1
1747+
<li>y = (1 - b - k) / (1 - k), or 0 if k is 1
1748+
<li>alpha is the same as the input color
1749+
<li>fallback color must be set to the input color
1750+
</ul>
1751+
17131752

17141753
<h2 id='modifying-colors'>
17151754
Modifying Colors: the ''color()'' function</h2>
@@ -2487,21 +2526,21 @@ The RGBColor Object</h3>
24872526
</pre>
24882527

24892528
<dl dfn-for="RGBColor">
2490-
<dt><dfn constructor title="RGBColor(r, g, b, a)">RGBColor</dfn>(double r, double g, double b, optional double a=1)
2491-
<dd>
2492-
Constructs an instance of the <a idl>RGBColor</a> class,
2493-
initializing the <a idl for=RGBColor>r</a>, <a idl for=RGBColor>g</a>, <a idl for=RGBColor>b</a>, and <a idl for=RGBColor>a</a> attributes of the instance to the passed arguments of the same name.
2529+
<dt><dfn constructor title="RGBColor(r, g, b, a) | RGBColor(r, g, b)">RGBColor</dfn>(double r, double g, double b, optional double a=1)
2530+
<dd link-for="RGBColor">
2531+
Constructs an instance of the <a idl for>RGBColor</a> class,
2532+
initializing the <a idl>r</a>, <a idl>g</a>, <a idl>b</a>, and <a idl>a</a> attributes of the instance to the passed arguments of the same name.
24942533

2495-
Note: The <a idl>RGBColor</a> class's attributes have a normal range of 0 to 1,
2534+
Note: The <a idl for>RGBColor</a> class's attributes have a normal range of 0 to 1,
24962535
not 0 to 255.
2497-
See the <a idl>HexColor</a> class if you prefer working in the 0-255 range.
2536+
See the <a idl for>HexColor</a> class if you prefer working in the 0-255 range.
24982537

24992538
<dt><dfn constructor title="RGBColor(color) | RGBColor()">RGBColor</dfn>(optional RGBColorInit color)
2500-
<dd>
2501-
Constructs an instance of the <a idl>RGBColor</a> class,
2502-
initializing the <a idl for=RGBColor>r</a>, <a idl for=RGBColor>g</a>, <a idl for=RGBColor>b</a>, and <a idl for=RGBColor>a</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
2539+
<dd link-for="RGBColor">
2540+
Constructs an instance of the <a idl for>RGBColor</a> class,
2541+
initializing the <a idl>r</a>, <a idl>g</a>, <a idl>b</a>, and <a idl>a</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
25032542

2504-
<dt><dfn constructor title="RGBColor(string)">RGBColor</dfn>(DOMString css)
2543+
<dt><dfn constructor title="RGBColor(css)">RGBColor</dfn>(DOMString css)
25052544
<dd>
25062545
<a>Parse a component value</a> from the passed string.
25072546
If this operation succeeds and the result is a valid CSS color,
@@ -2594,16 +2633,8 @@ The RGBColor Object</h3>
25942633
the 8-digit syntax must be used.
25952634

25962635
When serializing in the ''device-cmyk()'' syntax,
2597-
the various components must be set as follows:
2598-
2599-
<ul>
2600-
<li>k = 1 - max(r, g, b)
2601-
<li>c = (1 - r - k) / (1 - k), or 0 if k is 1
2602-
<li>m = (1 - g - k) / (1 - k), or 0 if k is 1
2603-
<li>y = (1 - b - k) / (1 - k), or 0 if k is 1
2604-
<li>alpha is the same as the input color
2605-
<li>fallback color must be set to the input color, serialized in the ''rgba()'' syntax
2606-
</ul>
2636+
the color must be <a>naively converted to CMYK</a>,
2637+
and the fallback color must be serialized in the ''rgba()'' syntax.
26072638

26082639
<dt><dfn attribute>defaultStringifier</dfn> <span attribute-info for="RGBColor/defaultStringifier"></span>
26092640
<dd>
@@ -2638,17 +2669,17 @@ The HSLColor Class</h3>
26382669
</pre>
26392670

26402671
<dl dfn-for="HSLColor">
2641-
<dt><dfn constructor title="HSLColor(h, s, l, a)">HSLColor</dfn>(double h, double s, double l, optional double a=1)
2642-
<dd>
2643-
Constructs an instance of the <a idl>HSLColor</a> class,
2644-
initializing the <a idl for=HSLColor>h</a>, <a idl for=HSLColor>s</a>, <a idl for=HSLColor>l</a>, and <a idl for=HSLColor>a</a> attributes of the instance to the passed arguments of the same name.
2672+
<dt><dfn constructor title="HSLColor(h, s, l, a) | HSLColor(h, s, l)">HSLColor</dfn>(double h, double s, double l, optional double a=1)
2673+
<dd link-for="HSLColor">
2674+
Constructs an instance of the <a idl for>HSLColor</a> class,
2675+
initializing the <a idl>h</a>, <a idl>s</a>, <a idl>l</a>, and <a idl>a</a> attributes of the instance to the passed arguments of the same name.
26452676

26462677
<dt><dfn constructor title="HSLColor(color) | HSLColor()">HSLColor</dfn>(optional HSLColorInit color)
2647-
<dd>
2648-
Constructs an instance of the <a idl>HSLColor</a> class,
2649-
initializing the <a idl for=HSLColor>h</a>, <a idl for=HSLColor>s</a>, <a idl for=HSLColor>l</a>, and <a idl for=HSLColor>a</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
2678+
<dd link-for="HSLColor">
2679+
Constructs an instance of the <a idl for>HSLColor</a> class,
2680+
initializing the <a idl>h</a>, <a idl>s</a>, <a idl>l</a>, and <a idl>a</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
26502681

2651-
<dt><dfn constructor title="HSLColor(string)">HSLColor</dfn>(DOMString css)
2682+
<dt><dfn constructor title="HSLColor(css)">HSLColor</dfn>(DOMString css)
26522683
<dd>
26532684
<a>Parse a component value</a> from the passed string.
26542685
If this operation succeeds and the result is a valid CSS color,
@@ -2732,17 +2763,17 @@ The HexColor Class</h3>
27322763
</pre>
27332764

27342765
<dl dfn-for="HexColor">
2735-
<dt><dfn constructor title="HexColor(r, g, b, a)">HexColor</dfn>([Clamp] octet r, [Clamp] octet g, [Clamp] octet b, optional [Clamp] octet a=255)
2736-
<dd>
2737-
Constructs an instance of the <a idl>HexColor</a> class,
2738-
initializing the <a idl for=HexColor>r</a>, <a idl for=HexColor>g</a>, <a idl for=HexColor>b</a>, and <a idl for=HexColor>a</a> attributes of the instance to the passed arguments of the same name.
2766+
<dt><dfn constructor title="HexColor(r, g, b, a) | HexColor(r, g, b)">HexColor</dfn>([Clamp] octet r, [Clamp] octet g, [Clamp] octet b, optional [Clamp] octet a=255)
2767+
<dd link-for="HexColor">
2768+
Constructs an instance of the <a idl for>HexColor</a> class,
2769+
initializing the <a idl>r</a>, <a idl>g</a>, <a idl>b</a>, and <a idl>a</a> attributes of the instance to the passed arguments of the same name.
27392770

27402771
<dt><dfn constructor title="HexColor(color) | HexColor()">HSLColor</dfn>(optional HexColorInit color)
2741-
<dd>
2742-
Constructs an instance of the <a idl>HexColor</a> class,
2743-
initializing the <a idl for=HexColor>r</a>, <a idl for=HexColor>g</a>, <a idl for=HexColor>b</a>, and <a idl for=HexColor>a</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
2772+
<dd link-for="HexColor">
2773+
Constructs an instance of the <a idl for>HexColor</a> class,
2774+
initializing the <a idl>r</a>, <a idl>g</a>, <a idl>b</a>, and <a idl>a</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
27442775

2745-
<dt><dfn constructor title="HexColor(string)">HexColor</dfn>(DOMString css)
2776+
<dt><dfn constructor title="HexColor(css)">HexColor</dfn>(DOMString css)
27462777
<dd>
27472778
<a>Parse a component value</a> from the passed string.
27482779
If this operation succeeds and the result is a valid CSS color,
@@ -2786,7 +2817,94 @@ The HexColor Class</h3>
27862817
<h3 id='api-CMYKColor'>
27872818
The CMYKColor Class</h3>
27882819

2789-
Issue: Fill this in.
2820+
<pre class='idl'>
2821+
[Constructor(double c, double m, double y, double k, optional double a=1, optional fallback RGBColor?=null),
2822+
Constructor(optional CMYKColorInit color),
2823+
Constructor(DOMString css)]
2824+
interface CMYKColor : CSSColor {
2825+
attribute double c;
2826+
attribute double m;
2827+
attribute double y;
2828+
attribute double k;
2829+
attribute double a;
2830+
attribute RGBColor? fallback;
2831+
2832+
RGBColor toRGB();
2833+
2834+
static attribute ColorStringifiers stringifiers;
2835+
attribute ColorStringifierType defaultStringifier;
2836+
};
2837+
2838+
dictionary CMYKColorInit {
2839+
double c = 0;
2840+
double m = 0;
2841+
double y = 0;
2842+
double k = 0;
2843+
double a = 1;
2844+
RGBColor? fallback = null;
2845+
};
2846+
</pre>
2847+
2848+
<dl dfn-for="CMYKColor">
2849+
<dt><dfn constructor title="CMYKColor(c, m, y, k, a, fallback) | CMYKColor(c, m, y, k, a) | CMYKColor(c, m, y, k)">CMYKColor</dfn>(double c, double m, double y, double k, optional double a=1, optional RGBColor? fallback=null)
2850+
<dd link-for="CMYKColor">
2851+
Constructs an instance of the <a idl for>CMYKColor</a> class,
2852+
initializing the <a idl>c</a>, <a idl>m</a>, <a idl>y</a>, <a idl>k</a>, <a idl>a</a>, and <a idl>fallback</a> attributes of the instance to the passed arguments of the same name.
2853+
2854+
<dt><dfn constructor title="CMYKColor(color) | CMYKColor()">CMYKColor</dfn>(optional HexColorInit color)
2855+
<dd link-for="CMYKColor">
2856+
Constructs an instance of the <a idl for>CMYKColor</a> class,
2857+
initializing the <a idl>c</a>, <a idl>m</a>, <a idl>y</a>, <a idl>k</a>, <a idl>a</a>, and <a idl>fallback</a> attributes of the instance to the passed dictionary attributes of the same name (or their defaulted values).
2858+
2859+
<dt><dfn constructor title="CMYKColor(css)">CMYKColor</dfn>(DOMString css)
2860+
<dd>
2861+
<a>Parse a component value</a> from the passed string.
2862+
If this operation succeeds and the result is a valid ''device-cmyk()'' function,
2863+
construct an instance of the <a idl>CMYKColor</a> class
2864+
with attributes initialized to represent that color.
2865+
2866+
Otherwise, if the operation succeeds and the result is any other type of valid CSS color,
2867+
construct an instance of the <a idl>CMYKColor</a> class
2868+
with attributes initialized to represent that color
2869+
using the
2870+
2871+
Otherwise, throw an XXXError.
2872+
2873+
<dt><dfn method>toRGB()</dfn>
2874+
<dd>
2875+
Defined as follows in ECMAScript:
2876+
2877+
<pre>
2878+
function() {
2879+
const r = 1 - Math.min(1, this.c + this.k);
2880+
const g = 1 - Math.min(1, this.m + this.k);
2881+
const b = 1 - Math.min(1, this.y + this.k);
2882+
return new RGBColor(r, g, b, this.a);
2883+
}
2884+
</pre>
2885+
2886+
<dt><dfn attribute>c</dfn> <span attribute-info for="CMYKColor/c"></span>
2887+
<dt><dfn attribute>m</dfn> <span attribute-info for="CMYKColor/m"></span>
2888+
<dt><dfn attribute>y</dfn> <span attribute-info for="CMYKColor/y"></span>
2889+
<dt><dfn attribute>k</dfn> <span attribute-info for="CMYKColor/k"></span>
2890+
<dt><dfn attribute>a</dfn> <span attribute-info for="CMYKColor/a"></span>
2891+
<dt><dfn attribute>fallback</dfn> <span attribute-info for="CMYKColor/fallback"></span>
2892+
<dd link-for="CMYKColor">
2893+
The <a idl>c</a>, <a idl>m</a>, <a idl>y</a>, <a idl>k</a>, <a idl>a</a>, and <a idl>fallback</a> attributes
2894+
represent the red, blue, green, and alpha channels of the CMYK color
2895+
that the <a idl for>CMYKColor</a> instance represents.
2896+
2897+
Note: The <a idl for>CMYKColor</a> class's c, m, y, k, and a attributes have a normal range of 0 to 1.
2898+
Its fallback attribute is always an RGBColor, or null (indicating it should convert itself to an RGBColor per <a section href="#cmyk-rgb"></a>).
2899+
2900+
<dt><dfn attribute>stringifiers</dfn> <span attribute-info for="CMYKColor/stringifiers"></span>
2901+
<dd>
2902+
Initialized to an empty object.
2903+
2904+
<dt><dfn attribute>defaultStringifier</dfn> <span attribute-info for="CMYKColor/defaultStringifier"></span>
2905+
<dd>
2906+
The <a idl for=HexColor>defaultStringifier</a> attribute must be initially set to "cmyk".
2907+
</dl>
27902908

27912909

27922910

0 commit comments

Comments
 (0)