chromium/third_party/blink/web_tests/fast/canvas/color-space/canvas-drawImage-p3.html

<!DOCTYPE HTML>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>

// Reference values generated by:
// https://fiddle.skia.org/c/8e7238b69744678e75b3e34715b29e0b

// sRGB(155,27,27,255)
var p3OpaqueRed = [0.55664, 0.15686, 0.13330, 1.0];
// sRGB(27,155,27,255)
var p3OpaqueGreen = [0.28613, 0.59961, 0.20386, 1.0];
// sRGB(27,27,155,255)
var p3OpaqueBlue = [0.10583, 0.10583, 0.58398, 1.0];
// sRGB(27,27,27,255)
var p3OpaqueBlack = [0.10583, 0.10583, 0.10583, 1.0];

function testPixels(ctx, tests, imageSetting)
{
    var actual, expected, tolerance = 0.01;
    for (var i = 0; i < tests.length; i++) {
      actual = ctx.getImageData(tests[i].x, tests[i].y, 1, 1, imageSetting).data;
      expected = tests[i].color;
      assert_true(actual.length === expected.length);
      for (var j = 0; j < actual.length; j++)
        assert_approx_equals(actual[j], expected[j], tolerance);
    }
}

function drawSRGBImageOnP3Canvas(source)
{
  var canvas = document.createElement('canvas');
  canvas.width = 20;
  canvas.height = 20;
  var ctx = canvas.getContext('2d',
      {colorSpace: 'display-p3', pixelFormat:'float16'});
  ctx.drawImage(source, 0, 0);
  var tests = [{x: 5, y: 5, color: p3OpaqueRed},
               {x: 15, y: 5, color: p3OpaqueGreen},
               {x: 5, y: 15, color: p3OpaqueBlue},
               {x: 15, y: 15, color: p3OpaqueBlack}];
  testPixels(ctx, tests, {colorSpace: 'display-p3', storageFormat:'float32'});
}

promise_test(function() {
  return new Promise((resolve, reject) => {
    var image = new Image();
    image.onload = function() {
      resolve(image);
    }
    image.src = 'resources/pattern-srgb.png'
  }).then(drawSRGBImageOnP3Canvas);
}, 'Draw SRGB image on a P3 canvas and read back the P3 pixels.');

</script>