@ljhaesler/color-handler 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +145 -0
- package/package.json +15 -0
package/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Color } from "pixi.js";
|
|
2
|
+
|
|
3
|
+
export class ColorHandler extends Map {
|
|
4
|
+
constructor() {
|
|
5
|
+
super();
|
|
6
|
+
this._associations = null;
|
|
7
|
+
this._colors = null;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
get associations() {
|
|
11
|
+
return this._associations;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
setOptions(options) {
|
|
15
|
+
if (
|
|
16
|
+
typeof options !== "object" ||
|
|
17
|
+
options === null ||
|
|
18
|
+
Array.isArray(options)
|
|
19
|
+
)
|
|
20
|
+
throw new Error("setOptions: expected a plain object");
|
|
21
|
+
|
|
22
|
+
this._associations = Object.keys(options);
|
|
23
|
+
this._colors = this._parseColorsFromOptions(Object.values(options));
|
|
24
|
+
this._sync();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
setColors(colorSets) {
|
|
28
|
+
if (typeof colorSets === "string")
|
|
29
|
+
this._colors = this._parseColorsFromString(colorSets);
|
|
30
|
+
else if (Array.isArray(colorSets))
|
|
31
|
+
this._colors = this._parseColorsFromArray(colorSets);
|
|
32
|
+
else throw new Error("setColors: expected a string or array");
|
|
33
|
+
|
|
34
|
+
this._trySync();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
setAssociations(associations) {
|
|
38
|
+
if (typeof associations === "string")
|
|
39
|
+
this._associations = this._parseAssociationsFromString(associations);
|
|
40
|
+
else if (Array.isArray(associations))
|
|
41
|
+
this._associations = this._parseAssociationsFromArray(associations);
|
|
42
|
+
else throw new Error("setAssociations: expected a string or array");
|
|
43
|
+
|
|
44
|
+
this._trySync();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getColorSet(association) {
|
|
48
|
+
return this.get(association);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
reorder(options) {
|
|
52
|
+
let order;
|
|
53
|
+
if (typeof options === "string")
|
|
54
|
+
order = this._parseAssociationsFromString(options);
|
|
55
|
+
else if (Array.isArray(options))
|
|
56
|
+
order = this._parseAssociationsFromArray(options);
|
|
57
|
+
else throw new Error("reorder: expected a string or array");
|
|
58
|
+
|
|
59
|
+
if (order.length !== this.size)
|
|
60
|
+
throw new Error(
|
|
61
|
+
"reorder: number of associations must match the number of color sets",
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
for (const o of order) {
|
|
65
|
+
if (!this.has(o)) throw new Error(`reorder: unknown association "${o}"`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const reordered = new Map();
|
|
69
|
+
for (const o of order) reordered.set(o, this.get(o));
|
|
70
|
+
|
|
71
|
+
this.clear();
|
|
72
|
+
for (const [k, v] of reordered) this.set(k, v);
|
|
73
|
+
|
|
74
|
+
this._associations = [...this.keys()];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_trySync() {
|
|
78
|
+
if (
|
|
79
|
+
this._colors &&
|
|
80
|
+
this._associations &&
|
|
81
|
+
this._colors.length === this._associations.length
|
|
82
|
+
) {
|
|
83
|
+
this._sync();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_sync() {
|
|
88
|
+
this.clear();
|
|
89
|
+
for (let i = 0; i < this._associations.length; i++) {
|
|
90
|
+
this.set(this._associations[i], this._colors[i]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_parseColorsFromString(colorSets) {
|
|
95
|
+
return colorSets
|
|
96
|
+
.split("/")
|
|
97
|
+
.map((set) => set.split(",").map((color) => new Color(color.trim())));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
_parseColorsFromArray(colorSets) {
|
|
101
|
+
return colorSets.map((set) =>
|
|
102
|
+
set.map((color) => {
|
|
103
|
+
if (typeof color !== "string")
|
|
104
|
+
throw new Error(
|
|
105
|
+
"_parseColorsFromArray: expected each color to be a string",
|
|
106
|
+
);
|
|
107
|
+
return new Color(color.trim());
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_parseAssociationsFromString(associations) {
|
|
113
|
+
return associations.split(",").map((association) => association.trim());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_parseAssociationsFromArray(associations) {
|
|
117
|
+
return associations.map((association) => {
|
|
118
|
+
if (typeof association !== "string")
|
|
119
|
+
throw new Error(
|
|
120
|
+
"_parseAssociationsFromArray: expected each association to be a string",
|
|
121
|
+
);
|
|
122
|
+
return association.trim();
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_parseColorsFromOptions(colorSets) {
|
|
127
|
+
const colors = [];
|
|
128
|
+
|
|
129
|
+
for (const colorSet of colorSets) {
|
|
130
|
+
if (typeof colorSet === "string") {
|
|
131
|
+
colors.push(
|
|
132
|
+
colorSet.split(",").map((color) => new Color(color.trim())),
|
|
133
|
+
);
|
|
134
|
+
} else if (Array.isArray(colorSet)) {
|
|
135
|
+
colors.push(colorSet.map((color) => new Color(color)));
|
|
136
|
+
} else {
|
|
137
|
+
throw new Error(
|
|
138
|
+
"_parseColorsFromOptions: expected each color set to be a string or array",
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return colors;
|
|
144
|
+
}
|
|
145
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ljhaesler/color-handler",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"author": "ljhaesler",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"pixi.js": "^8.19.0"
|
|
14
|
+
}
|
|
15
|
+
}
|