UNPKG

2.1 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var color_1 = tslib_1.__importDefault(require("../tree/color"));
5// Color Blending
6// ref: http://www.w3.org/TR/compositing-1
7function colorBlend(mode, color1, color2) {
8 var ab = color1.alpha; // result
9 var // backdrop
10 cb;
11 var as = color2.alpha;
12 var // source
13 cs;
14 var ar;
15 var cr;
16 var r = [];
17 ar = as + ab * (1 - as);
18 for (var i = 0; i < 3; i++) {
19 cb = color1.rgb[i] / 255;
20 cs = color2.rgb[i] / 255;
21 cr = mode(cb, cs);
22 if (ar) {
23 cr = (as * cs + ab * (cb -
24 as * (cb + cs - cr))) / ar;
25 }
26 r[i] = cr * 255;
27 }
28 return new color_1.default(r, ar);
29}
30var colorBlendModeFunctions = {
31 multiply: function (cb, cs) {
32 return cb * cs;
33 },
34 screen: function (cb, cs) {
35 return cb + cs - cb * cs;
36 },
37 overlay: function (cb, cs) {
38 cb *= 2;
39 return (cb <= 1) ?
40 colorBlendModeFunctions.multiply(cb, cs) :
41 colorBlendModeFunctions.screen(cb - 1, cs);
42 },
43 softlight: function (cb, cs) {
44 var d = 1;
45 var e = cb;
46 if (cs > 0.5) {
47 e = 1;
48 d = (cb > 0.25) ? Math.sqrt(cb)
49 : ((16 * cb - 12) * cb + 4) * cb;
50 }
51 return cb - (1 - 2 * cs) * e * (d - cb);
52 },
53 hardlight: function (cb, cs) {
54 return colorBlendModeFunctions.overlay(cs, cb);
55 },
56 difference: function (cb, cs) {
57 return Math.abs(cb - cs);
58 },
59 exclusion: function (cb, cs) {
60 return cb + cs - 2 * cb * cs;
61 },
62 // non-w3c functions:
63 average: function (cb, cs) {
64 return (cb + cs) / 2;
65 },
66 negation: function (cb, cs) {
67 return 1 - Math.abs(cb + cs - 1);
68 }
69};
70for (var f in colorBlendModeFunctions) {
71 if (colorBlendModeFunctions.hasOwnProperty(f)) {
72 colorBlend[f] = colorBlend.bind(null, colorBlendModeFunctions[f]);
73 }
74}
75exports.default = colorBlend;
76//# sourceMappingURL=color-blending.js.map
\No newline at end of file