- name: 2d.color.type.u8srgb.to.u8p3.to.u8srgb
  desc: test display-p3 unorm8 canvas not storing 8-bit srgb data accurately
  attributes: '{colorType: "unorm8", colorSpace: "display-p3"}'
  code: |
    // Consider the color in sRGB:
    //   (5,250,128)/255
    // In Display P3 this is:
    //   (0.4504003868394956, 0.9659537930632748, 0.5523945982276097)
    // Quantized to 8-bit, this is:
    //   (115, 246, 141)/255
    // Converted back to sRGB this is:
    //   (8.909609662725, 249.672568247041, 128.228541784731)/255
    // Quantized to 8-bit, this is:
    //   (9, 250, 128)
    var input = new ImageData(new Uint8ClampedArray([5, 250, 128, 255]),
                              1, 1, {colorSpace: "srgb"});
    ctx.putImageData(input, 0, 0);
    var readback = ctx.getImageData(0, 0, 1, 1, {colorSpace:"srgb"});
    const kEpsilon = 2;
    assert_approx_equals(readback.data[0], 9, kEpsilon);
    assert_approx_equals(readback.data[1], 250, kEpsilon);
    assert_approx_equals(readback.data[2], 128, kEpsilon);

- name: 2d.color.type.u8srgb.to.f16p3.to.u8srgb
  desc: test display-p3 float16 canvas storing 8-bit srgb data accurately
  attributes: '{colorType: "float16", colorSpace: "display-p3"}'
  code: |
    // Consider the color in sRGB:
    //   (5,250,128)/255
    // In Display P3 this is:
    //   (0.450400386839, 0.9659537931, 0.55239459823)
    // Quantized to float16, that should evaluate to:
    //   (0x3735, 0x3BBA, 0x386B)
    //   (0.450439453125, 0.9658203125, 0.55224609375)
    // Converted back to sRGB this is:
    //   (5.342920953809, 249.965067892273, 127.959124250169)/255
    // Quantized to 8-bit, this is:
    //   (5, 250, 128)
    var input = new ImageData(new Uint8ClampedArray([5, 250, 128, 255]),
                              1, 1, {colorSpace: "srgb"});
    ctx.putImageData(input, 0, 0);
    var readback = ctx.getImageData(0, 0, 1, 1, {colorSpace:"srgb"});
    const kEpsilon = 2;
    assert_approx_equals(readback.data[0], 5, kEpsilon);
    assert_approx_equals(readback.data[1], 250, kEpsilon);
    assert_approx_equals(readback.data[2], 128, kEpsilon);

- name: 2d.color.type.u8p3.to.u8srgb.to.u8p3
  desc: test srgb unorm8 canvas storing 8-bit display-p3 data accurately
  attributes: '{colorType: "unorm8", colorSpace: "srgb"}'
  code: |
    // Consider the color in Display P3:
    //   (5,250,128)/255
    // In sRGB this is:
    //   (-0.4990661502955996, 0.9982902153915844, 0.43901323244980783)
    // Quantized to 8-bit, this is:
    //   (0, 255, 112)/255
    // Converted back to Display P3 this is:
    //   (116.892405498713, 251.242468747889, 129.109129531237)/255
    // Quantized to 8-bit, this is:
    //   (116, 251, 129)
    var input = new ImageData(new Uint8ClampedArray([5, 250, 128, 255]),
                              1, 1, {colorSpace: "display-p3"});
    ctx.putImageData(input, 0, 0);
    var readback = ctx.getImageData(0, 0, 1, 1, {colorSpace:"display-p3"});
    const kEpsilon = 2;
    assert_approx_equals(readback.data[0], 116, kEpsilon);
    assert_approx_equals(readback.data[1], 251, kEpsilon);
    assert_approx_equals(readback.data[2], 129, kEpsilon);

- name: 2d.color.type.u8p3.to.f16srgb.to.u8p3
  desc: test srgb float16 canvas storing 8-bit display-p3 data accurately
  attributes: '{colorType: "float16", colorSpace: "srgb"}'
  code: |
    // Consider the color in Display P3:
    //   (5,250,128)/255
    // In sRGB this is:
    //   (-0.4990661502955996, 0.9982902153915844, 0.43901323244980783)
    // Quantized to float16, this is:
    //   (0xB7FC, 0x3BFC, 0x3706)
    //   (-0.49902344,         0.9980469,          0.43896484)
    // Converted back to Display P3 this is:
    //   (4.783888129523, 249.938708036787, 127.979725000698)/255
    // Quantized to 8-bit, this is:
    //   (5, 250, 128)
    var input = new ImageData(new Uint8ClampedArray([5, 250, 128, 255]),
                              1, 1, {colorSpace: "display-p3"});
    ctx.putImageData(input, 0, 0);
    var readback = ctx.getImageData(0, 0, 1, 1, {colorSpace:"display-p3"});
    const kEpsilon = 2;
    assert_approx_equals(readback.data[0], 5, kEpsilon);
    assert_approx_equals(readback.data[1], 250, kEpsilon);
    assert_approx_equals(readback.data[2], 128, kEpsilon);
