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

// sRGB(226,31,31,128)
var e_sRGB_TransparentRed = [0.886230, 0.121521, 0.121521, 0.501953];
// sRGB(226,31,31,128)
var e_sRGB_TransparentGreen = [0.121521, 0.886230, 0.121521, 0.501953];
// sRGB(226,31,31,128)
var e_sRGB_TransparentBlue = [0.121521, 0.121521, 0.886230, 0.501953];
// sRGB(226,31,31,128)
var e_sRGB_TransparentBlack = [0.121521, 0.121521, 0.121521, 0.501953];

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_equals(actual.length, expected.length);
      for (var j = 0; j < actual.length; j++)
        assert_approx_equals(actual[j], expected[j], tolerance);
    }
}

function drawSRGBImageOnExtendedSRGBCanvas(source)
{
  var canvas = document.createElement('canvas');
  canvas.width = 20;
  canvas.height = 20;
  var ctx = canvas.getContext('2d',
      {colorSpace: 'srgb', pixelFormat:'float16'});
  ctx.drawImage(source, 0, 0);
  var tests = [{x: 5, y: 5, color: e_sRGB_TransparentRed},
               {x: 15, y: 5, color: e_sRGB_TransparentGreen},
               {x: 5, y: 15, color: e_sRGB_TransparentBlue},
               {x: 15, y: 15, color: e_sRGB_TransparentBlack}];
  testPixels(ctx, tests, {colorSpace: 'srgb', storageFormat:'float32'});
}

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

</script>

