@ljhaesler/color-handler 1.0.0 → 1.0.2
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/README.md +93 -0
- package/index.js +7 -5
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# @ljhaesler/color-handler
|
|
2
|
+
|
|
3
|
+
A utility class for managing named associations to sets of [pixi.js](https://pixijs.com/) `Color` objects. Extends `Map`, so all standard Map methods are available.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ljhaesler/color-handler
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### `setOptions(options)`
|
|
14
|
+
|
|
15
|
+
The quickest way to set up a `ColorHandler` in one call. Pass a plain object where each key is an association name and each value is either a comma-separated color string or an array of color strings.
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { ColorHandler } from "@ljhaesler/color-handler";
|
|
19
|
+
|
|
20
|
+
const handler = new ColorHandler();
|
|
21
|
+
|
|
22
|
+
handler.setOptions({
|
|
23
|
+
spring: "red, pink",
|
|
24
|
+
summer: ["yellow", "green"],
|
|
25
|
+
autumn: "orange, brown",
|
|
26
|
+
winter: ["white", "blue"],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
handler.getColorSet("spring"); // [Color("red"), Color("pink")]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### `setColors(colorSets)` + `setAssociations(associations)`
|
|
33
|
+
|
|
34
|
+
You can set colors and associations separately — `_sync()` is called automatically once both are set and their lengths match.
|
|
35
|
+
|
|
36
|
+
**String format** — associations are comma-separated; color sets are comma-separated and delimited by `/`:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
handler.setAssociations("spring, summer, autumn, winter");
|
|
40
|
+
handler.setColors("red,pink / yellow,green / orange,brown / white,blue");
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Array format:**
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
handler.setAssociations(["spring", "summer", "autumn", "winter"]);
|
|
47
|
+
handler.setColors([
|
|
48
|
+
["red", "pink"],
|
|
49
|
+
["yellow", "green"],
|
|
50
|
+
["orange", "brown"],
|
|
51
|
+
["white", "blue"],
|
|
52
|
+
]);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `getColorSet(association)`
|
|
56
|
+
|
|
57
|
+
Returns the array of `Color` objects for a given association.
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
handler.getColorSet("summer"); // [Color("yellow"), Color("green")]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### `reorder(options)`
|
|
64
|
+
|
|
65
|
+
Reorders the internal map entries. Accepts the same string or array formats as `setAssociations`. Every existing association must be included — no more, no less.
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
handler.reorder("winter, autumn, summer, spring");
|
|
69
|
+
[...handler.keys()]; // ["winter", "autumn", "summer", "spring"]
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### `associations`
|
|
73
|
+
|
|
74
|
+
Read-only property that returns the current list of association names.
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
handler.associations; // ["spring", "summer", "autumn", "winter"]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## API
|
|
81
|
+
|
|
82
|
+
| Method / Property | Description |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `setOptions(options)` | Set associations and colors from a plain object in one call |
|
|
85
|
+
| `setColors(colorSets)` | Set color sets (string or array) |
|
|
86
|
+
| `setAssociations(associations)` | Set association names (string or array) |
|
|
87
|
+
| `getColorSet(association)` | Get the `Color[]` for a given association |
|
|
88
|
+
| `reorder(options)` | Reorder map entries (string or array) |
|
|
89
|
+
| `associations` | Returns the current association names |
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
ISC
|
package/index.js
CHANGED
|
@@ -75,11 +75,13 @@ export class ColorHandler extends Map {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
_trySync() {
|
|
78
|
-
if (
|
|
79
|
-
this._colors
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
if (this._colors && this._associations) {
|
|
79
|
+
if (this._colors.length !== this._associations.length) {
|
|
80
|
+
console.warn(
|
|
81
|
+
`ColorHandler: sync skipped — ${this._colors.length} color set(s) but ${this._associations.length} association(s)`,
|
|
82
|
+
);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
83
85
|
this._sync();
|
|
84
86
|
}
|
|
85
87
|
}
|