hdr-canvas 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,27 @@
1
1
  import { ColorTypes } from 'colorjs.io';
2
2
 
3
+ // See https://github.com/microsoft/TypeScript/blob/main/src/lib/dom.generated.d.ts
4
+ /// <reference lib="dom" />
5
+
6
+ type HDRHTMLCanvasOptionsType = "mode";
7
+ type HDRHTMLCanvasOptions = { [key in HDRHTMLCanvasOptionsType]?: string };
8
+
9
+ interface HDRHTMLCanvasElement extends HTMLCanvasElement {
10
+ configureHighDynamicRange(options: HDRHTMLCanvasOptions): void;
11
+ _getContext(contextId: string, options?: object): RenderingContext | null;
12
+ }
13
+
14
+ interface HDRImageData {
15
+ readonly colorSpace: PredefinedColorSpace;
16
+ readonly data: Uint8ClampedArray | Uint16Array;
17
+ readonly height: number;
18
+ readonly width: number;
19
+ }
20
+
21
+ // See https://github.com/w3c/ColorWeb-CG/blob/main/hdr_html_canvas_element.md
22
+ // "rec2100-display-linear" is left out beacause of mapping issues
23
+ type HDRPredefinedColorSpace = "display-p3" | "srgb" | "rec2100-hlg" | "rec2100-pq";
24
+
3
25
  type Uint16ImagePixelCallback = (red: number, green: number, blue: number, alpha: number) => Uint16Array;
4
26
  declare class Uint16Image {
5
27
  height: number;
@@ -5750,6 +5750,18 @@ Color.extend(interpolation);
5750
5750
  Color.extend(contrastMethods);
5751
5751
 
5752
5752
  class Uint16Image {
5753
+ height;
5754
+ width;
5755
+ data;
5756
+ static DEFAULT_COLORSPACE = "rec2100-hlg";
5757
+ static SDR_MULTIPLIER = 2 ** 16 - 1;
5758
+ static COLORSPACES = {
5759
+ "rec2100-hlg": "rec2100hlg",
5760
+ "display-p3": "p3",
5761
+ srgb: "sRGB",
5762
+ "rec2100-pq": "rec2100pq"
5763
+ };
5764
+ colorSpace;
5753
5765
  constructor(width, height, colorspace) {
5754
5766
  if (colorspace === undefined || colorspace === null) {
5755
5767
  this.colorSpace = Uint16Image.DEFAULT_COLORSPACE;
@@ -5791,10 +5803,12 @@ class Uint16Image {
5791
5803
  if (this.data === undefined || this.data === null) {
5792
5804
  return null;
5793
5805
  }
5794
- return new ImageData(this.data, this.width, this.height, { colorSpace: this.colorSpace });
5806
+ return new ImageData(this.data, this.width, this.height, {
5807
+ colorSpace: this.colorSpace
5808
+ });
5795
5809
  }
5796
5810
  static convertPixelToRec2100_hlg(pixel) {
5797
- const colorJScolorSpace = (Uint16Image.COLORSPACES["rec2100-hlg"]);
5811
+ const colorJScolorSpace = Uint16Image.COLORSPACES["rec2100-hlg"];
5798
5812
  const srgbColor = new Color("srgb", Array.from(pixel.slice(0, 3)).map((band) => {
5799
5813
  return band / 255;
5800
5814
  }), pixel[3] / 255);
@@ -5829,11 +5843,11 @@ class Uint16Image {
5829
5843
  const { width, height } = bitmap;
5830
5844
  const offscreen = new OffscreenCanvas(width, height);
5831
5845
  const ctx = offscreen.getContext("2d");
5832
- ctx === null || ctx === void 0 ? void 0 : ctx.drawImage(bitmap, 0, 0);
5846
+ ctx?.drawImage(bitmap, 0, 0);
5833
5847
  return ctx;
5834
5848
  })
5835
5849
  .then((ctx) => {
5836
- return ctx === null || ctx === void 0 ? void 0 : ctx.getImageData(0, 0, ctx === null || ctx === void 0 ? void 0 : ctx.canvas.width, ctx === null || ctx === void 0 ? void 0 : ctx.canvas.height);
5850
+ return ctx?.getImageData(0, 0, ctx?.canvas.width, ctx?.canvas.height);
5837
5851
  });
5838
5852
  }
5839
5853
  static fromImageData(imageData) {
@@ -5876,22 +5890,13 @@ class Uint16Image {
5876
5890
  return i;
5877
5891
  }
5878
5892
  }
5879
- Uint16Image.DEFAULT_COLORSPACE = "rec2100-hlg";
5880
- Uint16Image.SDR_MULTIPLIER = 2 ** 16 - 1;
5881
- Uint16Image.COLORSPACES = {
5882
- "rec2100-hlg": "rec2100hlg",
5883
- "display-p3": "p3",
5884
- srgb: "sRGB",
5885
- "rec2100-pq": "rec2100pq",
5886
- };
5887
5893
 
5888
5894
  function checkHDR() {
5889
5895
  try {
5890
5896
  const bitsPerChannel = screen.colorDepth / 3;
5891
5897
  const hdrSupported = bitsPerChannel > 8;
5892
5898
  const dynamicRangeHighMQ = window.matchMedia("(dynamic-range: high)").matches;
5893
- const colorGamutMQ = window.matchMedia("(color-gamut: rec2020)").matches ||
5894
- window.matchMedia("(color-gamut: p3)").matches;
5899
+ const colorGamutMQ = window.matchMedia("(color-gamut: rec2020)").matches || window.matchMedia("(color-gamut: p3)").matches;
5895
5900
  if (colorGamutMQ && dynamicRangeHighMQ) {
5896
5901
  if (bitsPerChannel !== Math.round(bitsPerChannel)) {
5897
5902
  return false;
@@ -5917,17 +5922,17 @@ function checkHDRCanvas() {
5917
5922
  if (!canvas.getContext) {
5918
5923
  return false;
5919
5924
  }
5920
- const ctx = (canvas.getContext("2d", {
5925
+ const ctx = canvas.getContext("2d", {
5921
5926
  colorSpace: colorSpace,
5922
- pixelFormat: "float16",
5923
- }));
5927
+ pixelFormat: "float16"
5928
+ });
5924
5929
  if (ctx === null) {
5925
5930
  return false;
5926
5931
  }
5927
5932
  return true;
5928
5933
  }
5929
5934
  catch (e) {
5930
- console.error("Bad canvas ColorSpace test", e);
5935
+ console.error("Bad canvas ColorSpace test - make sure that the Chromium browser flag 'enable-experimental-web-platform-features' has been enabled");
5931
5936
  return false;
5932
5937
  }
5933
5938
  }