summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/compositing/support/plus-lighter.js
blob: a5d1a34d7ff9afc19a4bdc2bde26a0c794a01cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { clamp01, multiplyAlpha, unmultiplyAlpha } from "./utils.js";

export function plusLighter(pixels) {
  if (pixels.length === 1) return pixels[0];

  return pixels.reduce((destination, source) => {
    const premultipliedSource = multiplyAlpha(source);
    const premultipliedDestination = multiplyAlpha(destination);
    const premultipliedResult = premultipliedDestination.map((channel, i) =>
      clamp01(channel + premultipliedSource[i])
    );
    return unmultiplyAlpha(premultipliedResult);
  });
}

export const tests = [
  // Each test is a list of colors to composite.
  // Each color is [r, g, b, a], unmultiplied, in the range 0-1.
  [
    [1, 0, 0, 0.5],
    [0, 0, 1, 0.5],
  ],
  [
    [1, 0, 0, 0.25],
    [0, 0, 1, 0.25],
  ],
  [
    [0.5, 0, 0, 0.5],
    [0, 0, 1, 0.5],
  ],
  // Test clamping
  [
    [1, 0, 0, 1],
    [0, 0, 1, 1],
  ],
  // Test more than two elements
  [
    [1, 0, 0, 0.25],
    [0, 0, 1, 0.25],
    [0, 1, 0, 0.25],
    [0.5, 0.4, 0.25, 0.25],
  ],
  // Test a single element
  [
    [0.5, 0, 0, 0.25],
  ],
];