@@ -198,9 +198,135 @@ The ``getImageData`` method is responsible for converting the data from the canv
198
198
## Examples
199
199
200
200
### Selecting the best color space match for the user agent's display device
201
- <pre >
202
- var colorSpace = window.matchMedia("(color-gamut: p3)").matches ? "display-p3" : "srgb";
203
- </pre >
201
+
202
+ This example selects a wide color gamut canvas only if the underlying display has a P3 gamut or larger.
203
+
204
+ ``` html
205
+ // Note that tha gamut is p3, but the color space is 'display-p3'.
206
+ var matchingColorSpace = window.matchMedia(
207
+ '(color-gamut: p3)').matches ? 'display-p3' : 'srgb';
208
+ var canvas = document.getElementById('Canvas');
209
+ var context = canvas.getContext('2d', {colorSpace:matchingColorSpace});
210
+ ```
211
+
212
+ ### Drawing wide color gamut content to a 2D Canvas
213
+
214
+ This example shows drawing a wide color gamut to an sRGB and a Display-P3 canvas, and discusses the different expected results.
215
+
216
+ ``` html
217
+ // Let |myWCGImage| be a loaded wide color gamut Image.
218
+ var myWCGImage;
219
+
220
+ // Let |defaultCanvas| be a default (sRGB) canvas. This code will draw the
221
+ // specified image, but will clip it to the sRGB color gamut.
222
+ var defaultCanvas = document.getElementById('DefaultCanvas');
223
+ var defaultCtx = defaultCanvas.getContext('2d');
224
+ defaultCtx.drawImage(myWCGImage, 0, 0, image.width, image.height)
225
+
226
+ // Let |wcgCanvas| be a Display-P3 canvas. This code will draw the specified
227
+ // image, which will not be clipped to the sRGB color gamut (it will be
228
+ // restricted only to the P3 color gamut).
229
+ var wcgCanvas = document.getElementById('DefaultCanvas');
230
+ var wcgCtx = wcgCanvas.getContext('2d', {colorSpace:'display-p3'});
231
+ wcgCtx.drawImage(myWCGImage, 0, 0, image.width, image.height)
232
+ ```
233
+
234
+ ### Drawing and retrieving content using `` ImageData ``
235
+
236
+ This example shows use of the new `` ImageData `` color management APIs.
237
+
238
+ ``` html
239
+ var canvas = document.getElementById('Canvas');
240
+ var context = defaultCanvas.getContext('2d', {colorSpace:'display-p3'});
241
+
242
+ // Creating a ImageData without specifying ImageDataSettings will create an
243
+ // sRGB ImageData. This will draw the color sRGB-red to the canvas.
244
+ var srgbImageData = new ImageData(1, 1);
245
+ srgbImageData.data[0] = srgbImageData.data[3] = 255;
246
+ srgbImageData.data[1] = srgbImageData.data[2] = 0;
247
+ context.putImageData(srgbImageData, 0, 0);
248
+
249
+ // This will draw P3-red to the canvas.
250
+ var p3ImageData = new ImageData(1, 1, {colorSpace:'display-p3'});
251
+ p3ImageData.data[0] = p3ImageData.data[3] = 255;
252
+ p3ImageData.data[1] = p3ImageData.data[2] = 0;
253
+ context.putImageData(p3ImageData, 0, 0);
254
+
255
+ // The color space of an ImageData can be retrieved through its
256
+ // ImageDataSettings.
257
+ console.log(srgbImageData.getSettings().colorSpace);
258
+ console.log(p3ImageData.getSettings().colorSpace);
259
+
260
+ // Reading back an ImageData without specifying a color space will retrieve
261
+ // pixel values converted to sRGB. Because this will clamp values in Display
262
+ // P3 to sRGB // |readSrgbImageData.data| will be [255,0,0,255, 255,0,0,255];
263
+ var readSrgbImageData = context.getImageData(0, 0, 2, 1);
264
+ console.log(readSrgbImageData.data);
265
+
266
+ // Reading back an ImageData specifying a color space will retrieve the pixel
267
+ // values converted to that color space. |readP3ImageData.data| will be
268
+ // [234,51,35,255, 255,0,0,255].
269
+ var readP3ImageData = context.getImageData(0, 0, 2, 1,
270
+ {colorSpace:'display-p3'});
271
+ console.log(readP3ImageData.data);
272
+ ```
273
+
274
+ ### Drawing WebGL wide color gamut content
275
+
276
+ This example shows a WebGL application that clears the screen to Display P3 red.
277
+
278
+ ``` html
279
+ var canvas = document.getElementById('Canvas');
280
+ var gl = defaultCanvas.getContext('webgl2');
281
+ gl.colorSpace = 'display-p3';
282
+ gl.clearColor(1.0, 0.0, 0.0, 1.0);
283
+ gl.clear(gl.COLOR_BUFFER_BIT);
284
+ ```
285
+
286
+ ### Uploading an image as Display-P3 pixels
287
+
288
+ This example shows uploading an image and converting its color profile.
289
+ Note that this example assumes the conversion behavior of `` BROWSER_DEFAULT_WEBGL `` .
290
+ Also note that in this example the color conversion happens synchronously at the moment that `` texImage2D `` is called, which may not be ideal.
291
+
292
+ ``` html
293
+ // Let |myImage| be an image that has some (unknown) color profile.
294
+ var myImage;
295
+ var canvas = document.getElementById('Canvas');
296
+ var gl = defaultCanvas.getContext('webgl2');
297
+
298
+ // Let texInP3 be the image converted to Display-P3.
299
+ var texInP3 = gl.createTexture();
300
+ gl.bindTexture(gl.TEXTURE_2D, texInP3);
301
+ gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL,
302
+ gl.BROWSER_DEFAULT_WEBGL);
303
+ gl.colorSpace = 'display-p3';
304
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, image.width, image.height, 0,
305
+ gl.RGBA, gl.UNSIGNED_BYTE, image);
306
+ ```
307
+
308
+ In this example, the `` ImageBitmap `` API is used to allow the color conversion of the image to be uploaded in Display-P3 asynchronously at decode time.
309
+
310
+ ``` html
311
+ // Let |myImageUrl| be the URL for an image that has some (unknown) color
312
+ // profile.
313
+ var myImageUrl;
314
+ var canvas = document.getElementById('Canvas');
315
+ var gl = defaultCanvas.getContext('webgl2');
316
+
317
+ fetch(myImageUrl).then(function(response) {
318
+ return response.blob();
319
+ }).then(function(blob) {
320
+ return createImageBitmap(blob, 0, 0, 1000, 1000,
321
+ {colorSpace:'display-p3'});
322
+ }).then(function(bitmap) {
323
+ const texInP3 = gl.createTexture();
324
+ gl.bindTexture(gl.TEXTURE_2D, texInP3);
325
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, bitmap.width, bitmap.height, 0,
326
+ gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
327
+
328
+ });
329
+ ```
204
330
205
331
## Adoption
206
332
Lack of color management and color interoperability is a longstanding complaint about the canvas API.
0 commit comments