-
Notifications
You must be signed in to change notification settings - Fork 18
/
deserialize-gradient.ts
52 lines (48 loc) · 1.29 KB
/
deserialize-gradient.ts
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
48
49
50
51
52
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { isString } from "../../util/is-string";
import { EShape } from "../e-shape";
import { EShapeGradientLike, EShapeGradientPointLike } from "../e-shape-gradient";
import { EShapeResourceManagerDeserialization } from "../e-shape-resource-manager-deserialization";
const parse = (target: string): number[] | null => {
try {
return JSON.parse(target) as number[];
} catch (e) {
return null;
}
};
const toGradientLike = (target: string): EShapeGradientLike | undefined => {
const parsed = parse(target);
if (parsed == null || parsed.length < 7) {
return undefined;
} else {
const direction = parsed[0];
const points: EShapeGradientPointLike[] = [];
for (let i = 1, imax = parsed.length; i < imax; i += 3) {
points.push({
color: parsed[i + 0],
alpha: parsed[i + 1],
position: parsed[i + 2]
});
}
return {
points,
direction
};
}
};
export const deserializeGradient = (
index: number,
manager: EShapeResourceManagerDeserialization,
result: EShape
): void => {
const resources = manager.resources;
if (0 <= index && index < resources.length) {
const serialized = resources[index];
if (isString(serialized)) {
result.gradient = toGradientLike(serialized);
}
}
};