├── CONTRIBUTING.md ├── Canvas2DColorManagementExplainer.md ├── CanvasColorSpaceProposal.md ├── LICENSE.md ├── README.md └── w3c.json /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Web Incubator Community Group 2 | 3 | This repository is being used for work in the W3C Web Incubator Community Group, governed by 4 | the [W3C Community License Agreement 5 | (CLA)](http://www.w3.org/community/about/agreements/cla/). To make substantive 6 | contributions, you must join the CG. 7 | 8 | If you are not the sole contributor to a contribution (pull request), please 9 | identify all 10 | contributors in the pull request comment. 11 | 12 | To add a contributor (other than yourself, that's automatic), mark them one per 13 | line as follows: 14 | 15 | ``` 16 | +@github_username 17 | ``` 18 | 19 | If you added a contributor by mistake, you can remove them in a comment with: 20 | 21 | ``` 22 | -@github_username 23 | ``` 24 | 25 | If you are making a pull request on behalf of someone else but you had no part 26 | in designing the 27 | feature, you can remove yourself with the above syntax. 28 | 29 | -------------------------------------------------------------------------------- /Canvas2DColorManagementExplainer.md: -------------------------------------------------------------------------------- 1 | # Canvas 2D Color Management Explainer 2 | 3 | This explainer summarizes color management features added to 2D canvases. 4 | 5 | ## Background and related specifications 6 | 7 | The features described are those added in [this WhatWG PR](https://github.com/whatwg/javascript/pull/6562). 8 | 9 | This work is derived from an initial more wide ranging proposal, developed by the W3C's ColorWeb CG, which may be found [here](https://github.com/WICG/canvas-color-space/blob/main/CanvasColorSpaceProposal.md). 10 | This proposal was revised during WhatWG review. 11 | 12 | The related [CSS Media Queries Level 5](https://www.w3.org/TR/mediaqueries-5/#color-gamut) specification defines the `color-gamut` media query to determine the capabilities of the current display device. 13 | 14 | The related [CSS Color Module Level 4](https://www.w3.org/TR/css-color-4) specification defines several color spaces that may be used to define CSS colors. 15 | 16 | ## Summary of changes 17 | 18 | Formalization of existing functionality: 19 | 20 | * This formalizes the convention that content rendered by a `CanvasRenderingContext2D` is in the sRGB color space by default. 21 | * This formalizes the convention that the pixels specified by an `ImageData` are in the sRGB color space by default. 22 | * This formalizes the convention that `CanvasRenderingContext2D` is color managed, meaning that all inputs are converted to the context's color space when drawing. 23 | * This formalizes the convention that content (images, in particular) that does not specify a color space should be interpreted as being in the sRGB color space. 24 | * This formalizes that the `toDataURL` and `toBlob` methods of `HTMLCanvasElement` are to create representations that match the canvas's context's color space as accurately as possible. 25 | * This formalizes that the `getImageData` and `createImageData` methods of `CanvasRenderingContext2D` are to create an `ImageData` object that is in the same color space as the context on which the method was called. 26 | 27 | New functionality: 28 | 29 | * This adds a parameter whereby a `CanvasRenderingContext2DSettings` can specify a color space other than sRGB. 30 | * This adds a parameter whereby an `ImageData` can specify a color space. 31 | * This adds color spaces of `"srgb"` (for sRGB) and `"display-p3"` (for Display P3) as options for the above parameters. 32 | 33 | ## New APIs 34 | 35 | In this section we cover all new enums, interfaces, and methods added by this feature. 36 | 37 | ### `PredefinedColorSpace` 38 | 39 | The color spaces in this proposal are available in the new `PredefinedColorSpace` enum. 40 | The names in this enum are chosen to the names in the [predefined color spaces](https://www.w3.org/TR/css-color-4/#predefined) section of the [CSS Color Module Level 4](https://www.w3.org/TR/css-color-4) specification. 41 | 42 | ```idl 43 | enum PredefinedColorSpace { 44 | "srgb", // default 45 | "display-p3", 46 | }; 47 | ``` 48 | 49 | ### `CanvasRenderingContext2DSettings` 50 | 51 | To create a `CanvasRenderingContext2D` that does not use the default color space of sRGB, the color space may be specified in the `CanvasRenderingContext2DSettings` used to create the context. 52 | This dictionary is already used to specify if the context's pixel format should include an alpha channel, among other features. 53 | Similarly to the alpha channel, the color space of an existing `CanvasRenderingContext2D` may be queried using the [`getContextAttributes`](https://javascript.spec.whatwg.org/multipage/canvas.javascript#dom-context-2d-canvas-getcontextattributes) method, which will return a `CanvasRenderingContext2DSettings`. 54 | 55 | ```idl 56 | partial dictionary CanvasRenderingContext2DSettings { 57 | PredefinedColorSpaceEnum colorSpace = "srgb"; 58 | }; 59 | ``` 60 | 61 | ### `ImageDataSettings` 62 | 63 | To create an `ImageData` that does not use the default color space of sRGB, the color space may be specified in a new `ImageDataSettings` dictionary. 64 | Note that this dictionary does not specify a default color space. 65 | 66 | ```idl 67 | dictionary ImageDataSettings { 68 | PredefinedColorSpaceEnum colorSpace; 69 | }; 70 | ``` 71 | 72 | ### `ImageData` 73 | 74 | The `ImageData` interface's constructors are updated to optionally take this `ImageDataSettings` argument. 75 | If this argument is not specified, or if it is specified but its `colorSpace` entry is not specified, then the `ImageData` will default to sRGB. 76 | 77 | ```idl 78 | partial interface ImageData { 79 | constructor(unsigned long sw, 80 | unsigned long sh, 81 | optional ImageDataSettings settings = {}); 82 | constructor(Uint8ClampedArray data, 83 | unsigned long sw, 84 | optional unsigned long sh, 85 | optional ImageDataSettings settings = {}); 86 | }; 87 | ``` 88 | 89 | The `ImageData` interface is updated to include a `PredefinedColorSpace` attribute. 90 | 91 | ```idl 92 | partial interface ImageData { 93 | readonly attribute PredefinedColorSpace colorSpace; 94 | }; 95 | ``` 96 | 97 | ### `CanvasRenderingContext2D` 98 | 99 | The `CanvasRenderingContext2D` interface is updated to include an optional `ImageDataSettings` argument to the `createImageData` and `getImageData` functions. 100 | If this argument is not specified, or if it is specified but its `colorSpace` entry is not specified, then the `ImageData`'s color space will be set to that context's color space. 101 | The `getImageData` method will convert pixel values from the context's color space to the result's color space, if needed. 102 | 103 | ```idl 104 | partial interface CanvasRenderingContext2D { 105 | ImageData createImageData(long sw, 106 | long sh, 107 | optional ImageDataSettings settings = {}); 108 | ImageData getImageData(long sx, 109 | long sy, 110 | long sw, 111 | long sh, 112 | optional ImageDataSettings settings = {}); 113 | } 114 | ``` 115 | 116 | ## Formalized and new behaviors 117 | 118 | In this section we discuss new behaviors, and formalize behaviors that were previously undefined. 119 | 120 | ### Default color space for `CanvasRenderingContext2D` 121 | 122 | The default color space for content rendered using `CanvasRenderingContext2D` has, by convention but not by specification, been sRGB. 123 | This is now formalized by the fact that `CanvasRenderingContext2DSettings` specifies a default value of `"srgb"` for `colorSpace`. 124 | 125 | ### Default color space for `ImageData` 126 | 127 | The default color space for `ImageData` has historically been sRGB (perhaps more accurately, its behaviors have been consistent with it being sRGB). 128 | As mentioned earlier, this is now formalized by the [ImageData initialization algorithm](https://html.spec.whatwg.org/multipage/canvas.html#initialize-an-imagedata-object). 129 | 130 | As mentioned earlier, if an `ImageDataSettings` does not specify `colorSpace` in a call to `getImageData` or `createImageData`, the resulting `ImageData` will [default to](https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createimagedata) the color space of the context. 131 | This means that doing a "round trip" using `getImageData` and then `putImageData` will lose no fidelity. 132 | 133 | ### Color conversions 134 | 135 | When drawing content into a `CanvasRenderingContext2D`, that content is converted from its source color space to the context's color space. 136 | This is now [formalized](https://html.spec.whatwg.org/multipage/canvas.html#colour-spaces-and-colour-correction). 137 | Historically, by convention but not by specification, (in WebKit and Chromium), this has meant converting to sRGB. 138 | 139 | Content that does not specify a color space [is to be interpreted](https://drafts.csswg.org/css-color/#untagged) as being in the sRGB color space. 140 | This language is now [re-emphasized](https://whatpr.org/html/6466/canvas.html#colour-spaces-and-colour-correction) in non-normative text. 141 | 142 | The color space conversion algorithm for all conversions has historically been relative colorimetric intent. 143 | This is now formalized. 144 | Relative colorimetric intent is described in detail [here](https://drafts.csswg.org/css-color/#valdef-color-profile-rendering-intent-relative-colorimetric). 145 | It is the simplest (or next-to-simplest) color conversion algorithm. 146 | When converting from a wider gamut space to a more narrow gamut space, any colors that do not fit in the narrow gamut space are clipped. 147 | 148 | ### Creating images with `toDataURL` and `toBlob` 149 | 150 | Historically, `toDataURL` and `toDataBlob` did not specify a color profile in the resulting image. 151 | 152 | This behavior is now changed to [specify](https://html.spec.whatwg.org/multipage/canvas.html#serialising-bitmaps-to-a-file) that the resulting image, when allowed by the image format, should include a color profile indicating the source's color space. 153 | 154 | This means that doing a "round trip" using `toDataURL` or `toBlob` and then drawing the result using `drawImage` will lose no fidelity (up to compression artifacts). 155 | 156 | ## Related rendering contexts 157 | 158 | This section discusses other rendering contexts besides `CanvasRenderingContext2D`. 159 | 160 | ### `ImageBitmapRenderingContext` (no changes) 161 | 162 | By default, displaying content as an `ImageBitmap` that is provided to an `ImageBitmapRenderingContext` should be identical to displaying that content in an element (e.g, as a `` or `