@ljhaesler/color-handler
v1.0.7
Published
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.
Readme
@ljhaesler/color-handler
A utility class for managing named associations to sets of pixi.js Color objects. Extends Map, so all standard Map methods are available.
Installation
npm install @ljhaesler/color-handlerUsage
setOptions(options)
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.
import { ColorHandler } from "@ljhaesler/color-handler";
const handler = new ColorHandler();
handler.setOptions({
spring: "red, pink",
summer: ["yellow", "green"],
autumn: "orange, brown",
winter: ["white", "blue"],
});
handler.getColorSet("spring"); // [Color("red"), Color("pink")]setColors(colorSets) + setAssociations(associations)
You can set colors and associations separately — _sync() is called automatically once both are set and their lengths match.
String format — associations are comma-separated; color sets are comma-separated and delimited by /:
handler.setAssociations("spring, summer, autumn, winter");
handler.setColors("red,pink / yellow,green / orange,brown / white,blue");Array format:
handler.setAssociations(["spring", "summer", "autumn", "winter"]);
handler.setColors([
["red", "pink"],
["yellow", "green"],
["orange", "brown"],
["white", "blue"],
]);getColorSet(association)
Returns the array of Color objects for a given association.
handler.getColorSet("summer"); // [Color("yellow"), Color("green")]reorder(options)
Reorders the internal map entries. Accepts the same string or array formats as setAssociations. Every existing association must be included — no more, no less.
handler.reorder("winter, autumn, summer, spring");
[...handler.keys()]; // ["winter", "autumn", "summer", "spring"]associations
Read-only property that returns the current list of association names.
handler.associations; // ["spring", "summer", "autumn", "winter"]API
| Method / Property | Description |
|---|---|
| setOptions(options) | Set associations and colors from a plain object in one call |
| setColors(colorSets) | Set color sets (string or array) |
| setAssociations(associations) | Set association names (string or array) |
| getColorSet(association) | Get the Color[] for a given association |
| reorder(options) | Reorder map entries (string or array) |
| associations | Returns the current association names |
License
ISC
