UNPKG

2.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createInterpolateRound = exports.createInterpolateValue = exports.createInterpolateColor = exports.createInterpolateNumber = void 0;
4const color_1 = require("./color");
5/**
6 * 返回一个线性插值器,接受数字
7 * @param a 任意值
8 * @param b 任意值
9 * @returns 线性插值器
10 */
11const createInterpolateNumber = (a, b) => {
12 return (t) => a * (1 - t) + b * t;
13};
14exports.createInterpolateNumber = createInterpolateNumber;
15const createInterpolateColor = (a, b) => {
16 const c1 = (0, color_1.string2rbg)(a);
17 const c2 = (0, color_1.string2rbg)(b);
18 if (c1 === null || c2 === null)
19 return c1 ? () => a : () => b;
20 return (t) => {
21 const values = new Array(4);
22 for (let i = 0; i < 4; i += 1) {
23 const from = c1[i];
24 const to = c2[i];
25 values[i] = from * (1 - t) + to * t;
26 }
27 const [r, g, b, a] = values;
28 return `rgba(${Math.round(r)}, ${Math.round(g)}, ${Math.round(b)}, ${a})`;
29 };
30};
31exports.createInterpolateColor = createInterpolateColor;
32/**
33 * 返回一个线性插值器,接受数字和颜色
34 * @param a 任意值
35 * @param b 任意值
36 * @returns 线性插值器
37 */
38const createInterpolateValue = (a, b) => {
39 if (typeof a === 'number' && typeof b === 'number')
40 return (0, exports.createInterpolateNumber)(a, b);
41 if (typeof a === 'string' && typeof b === 'string')
42 return (0, exports.createInterpolateColor)(a, b);
43 return () => a;
44};
45exports.createInterpolateValue = createInterpolateValue;
46/**
47 * 返回一个 round 线性差值器,对输出值进行四舍五入
48 * @param a 任意值
49 * @param b 任意值
50 * @returns 线性插值器
51 */
52const createInterpolateRound = (a, b) => {
53 const interpolateNumber = (0, exports.createInterpolateNumber)(a, b);
54 return (t) => Math.round(interpolateNumber(t));
55};
56exports.createInterpolateRound = createInterpolateRound;
57//# sourceMappingURL=interpolate.js.map
\No newline at end of file