@thi.ng/color-palettes 1.3.0 → 1.4.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-06-21T19:34:38Z
3
+ - **Last updated**: 2024-07-06T12:02:18Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,14 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [1.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/color-palettes@1.4.0) (2024-06-29)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add filteredThemes() ([58a9712](https://github.com/thi-ng/umbrella/commit/58a9712))
17
+ - update asCSS/Int/LCH/RGB() args ([9aa2f43](https://github.com/thi-ng/umbrella/commit/9aa2f43))
18
+ - accept existing themes (array of colors) as arg (in addition to theme IDs)
19
+
12
20
  ## [1.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/color-palettes@1.3.0) (2024-06-21)
13
21
 
14
22
  #### 🚀 Features
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 193 standalone projects, maintained as part
10
+ > This is one of 188 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -281,7 +281,7 @@ For Node.js REPL:
281
281
  const cp = await import("@thi.ng/color-palettes");
282
282
  ```
283
283
 
284
- Package sizes (brotli'd, pre-treeshake): ESM: 4.80 KB
284
+ Package sizes (brotli'd, pre-treeshake): ESM: 4.92 KB
285
285
 
286
286
  ## Dependencies
287
287
 
package/filter.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ColorPredicate, ThemeColor, ThemePredicate } from "./api.js";
1
+ import type { ColorPredicate, Theme, ThemeColor, ThemePredicate } from "./api.js";
2
2
  /**
3
3
  * Higher order theme filter. Takes a predicate function which will be applied
4
4
  * to a single {@link ThemeColor}. Returns a new function which accepts a single
@@ -106,4 +106,20 @@ export declare const proximityLCH: (color: ThemeColor, eps: number, threshold?:
106
106
  * @param threshold
107
107
  */
108
108
  export declare const proximityRGB: (color: ThemeColor, eps: number, threshold?: number) => ThemePredicate;
109
+ /**
110
+ * Similar to {@link rgbThemes}, {@link themeIDs} etc., but for filtering custom
111
+ * themes (i.e. arrays of colors), using provided filter predicate(s) or theme
112
+ * indices. This way the composable filter predicates of this package can also
113
+ * be used for user-defined themes (i.e. not limited to those presets included
114
+ * in this package)
115
+ *
116
+ * @remarks
117
+ * If `idOnly` is enabled/true, only the IDs/indices of matching themes are
118
+ * returned (rather than the corresponding themes themselves).
119
+ *
120
+ * @param preds
121
+ * @param themes
122
+ */
123
+ export declare function filteredThemes<T extends Theme>(preds: ThemePredicate[] | number[], themes: T[]): IterableIterator<T>;
124
+ export declare function filteredThemes<T extends Theme>(preds: ThemePredicate[] | number[], themes: T[], idOnly: true): IterableIterator<number>;
109
125
  //# sourceMappingURL=filter.d.ts.map
package/filter.js CHANGED
@@ -44,10 +44,22 @@ const proximityRGB = (color, eps, threshold) => {
44
44
  };
45
45
  const __isLCH = (x) => !isPrimitive(x) && x.mode === "lch";
46
46
  const __isRGB = (x) => !isPrimitive(x) && x.mode === "srgb";
47
+ function* filteredThemes(preds, themes, idOnly = false) {
48
+ if (preds.length && typeof preds[0] === "function") {
49
+ const pred = compFilter(...preds);
50
+ for (let i = 0; i < themes.length; i++) {
51
+ const theme = themes[i];
52
+ if (pred(theme)) yield idOnly ? i : theme;
53
+ }
54
+ } else {
55
+ for (let id of preds) yield idOnly ? id : themes[id];
56
+ }
57
+ }
47
58
  export {
48
59
  chroma,
49
60
  compFilter,
50
61
  defFilter,
62
+ filteredThemes,
51
63
  hue,
52
64
  luma,
53
65
  proximityLCH,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/color-palettes",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Collection of 200+ image based color themes & composable theme query filters",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -38,18 +38,18 @@
38
38
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@thi.ng/api": "^8.11.3",
42
- "@thi.ng/base-n": "^2.7.15",
43
- "@thi.ng/checks": "^3.6.5",
44
- "@thi.ng/color": "^5.6.46",
45
- "@thi.ng/errors": "^2.5.8",
46
- "@thi.ng/hex": "^2.3.47"
41
+ "@thi.ng/api": "^8.11.5",
42
+ "@thi.ng/base-n": "^2.7.17",
43
+ "@thi.ng/checks": "^3.6.7",
44
+ "@thi.ng/color": "^5.6.48",
45
+ "@thi.ng/errors": "^2.5.10",
46
+ "@thi.ng/hex": "^2.3.49"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@microsoft/api-extractor": "^7.47.0",
50
- "esbuild": "^0.21.5",
51
- "typedoc": "^0.25.13",
52
- "typescript": "^5.5.2"
50
+ "esbuild": "^0.23.0",
51
+ "typedoc": "^0.26.3",
52
+ "typescript": "^5.5.3"
53
53
  },
54
54
  "keywords": [
55
55
  "css",
@@ -90,5 +90,5 @@
90
90
  "parent": "@thi.ng/color",
91
91
  "year": 2021
92
92
  },
93
- "gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n"
93
+ "gitHead": "70f493d9ccc97c5df5dda7e808e96cd1691097fc\n"
94
94
  }
package/theme.d.ts CHANGED
@@ -1,37 +1,44 @@
1
- import type { LCHTheme, RGBTheme, ThemePredicate } from "./api.js";
1
+ import type { LCHTheme, RGBTheme, Theme, ThemePredicate } from "./api.js";
2
2
  /**
3
- * Returns theme for given ID as CSS hex colors. If `reverse` is true (default:
4
- * false), the theme colors will be returned in reverse order.
3
+ * Returns given `theme` (array of colors or theme preset ID) as CSS hex colors.
4
+ * If `reverse` is true (default: false), the theme colors will be returned in
5
+ * reverse order.
5
6
  *
6
- * @param id
7
+ * @param theme
7
8
  * @param reverse
8
9
  */
9
- export declare const asCSS: (id: number, reverse?: boolean) => string[];
10
+ export declare const asCSS: (theme: number | Theme, reverse?: boolean) => string[];
10
11
  /**
11
- * Returns theme for given ID as packed ARGB integers (alpha channel will always
12
- * be set to 0xff). If `reverse` is true (default: false), the theme colors will
13
- * be returned in reverse order.
12
+ * Returns given `theme` (array of colors or theme preset ID) as packed ARGB
13
+ * integers. If `reverse` is true (default: false), the theme colors will be
14
+ * returned in reverse order.
14
15
  *
15
- * @param id
16
+ * @remarks
17
+ * If given a theme ID, the alpha channel of all colors will always be set to
18
+ * `0xff`.
19
+ *
20
+ * @param theme
16
21
  * @param reverse
17
22
  */
18
- export declare const asInt: (id: number, reverse?: boolean) => number[];
23
+ export declare const asInt: (theme: number | Theme, reverse?: boolean) => number[];
19
24
  /**
20
- * Returns theme for given ID as thi.ng/color LCH color vectors. If `reverse` is
21
- * true (default: false), the theme colors will be returned in reverse order.
25
+ * Returns given `theme` (array of colors or theme preset ID) as thi.ng/color
26
+ * LCH color vectors. If `reverse` is true (default: false), the theme colors
27
+ * will be returned in reverse order.
22
28
  *
23
- * @param id
29
+ * @param theme
24
30
  * @param reverse
25
31
  */
26
- export declare const asLCH: (id: number, reverse?: boolean) => LCHTheme;
32
+ export declare const asLCH: (theme: number | Theme, reverse?: boolean) => LCHTheme;
27
33
  /**
28
- * Returns theme for given ID as thi.ng/color sRGB color vectors. If `reverse`
29
- * is true (default: false), the theme colors will be returned in reverse order.
34
+ * Returns given `theme` (array of colors or theme preset ID) sRGB color
35
+ * vectors. If `reverse` is true (default: false), the theme colors will be
36
+ * returned in reverse order.
30
37
  *
31
- * @param id
38
+ * @param theme
32
39
  * @param reverse
33
40
  */
34
- export declare const asRGB: (id: number, reverse?: boolean) => RGBTheme;
41
+ export declare const asRGB: (theme: number | Theme, reverse?: boolean) => RGBTheme;
35
42
  /**
36
43
  * Yields iterator of CSS themes (via {@link asCSS}). Yields all
37
44
  * themes unless specific theme IDs or filter predicates are provided.
package/theme.js CHANGED
@@ -1,47 +1,67 @@
1
+ import { isNumber } from "@thi.ng/checks/is-number";
2
+ import { css } from "@thi.ng/color/css/css";
3
+ import { argb32 } from "@thi.ng/color/int/int";
1
4
  import { lch } from "@thi.ng/color/lch/lch";
2
5
  import { srgb } from "@thi.ng/color/srgb/srgb";
3
6
  import { assert } from "@thi.ng/errors/assert";
4
7
  import { U24 } from "@thi.ng/hex";
5
8
  import { BINARY, NUM_THEMES } from "./binary.js";
6
9
  import { compFilter } from "./filter.js";
7
- const asCSS = (id, reverse = false) => {
8
- __ensureID(id);
9
- const theme = [];
10
- id *= 18;
11
- for (let i = 0; i < 6; i++, id += 3) {
12
- theme.push(
13
- "#" + U24(BINARY[id] << 16 | BINARY[id + 1] << 8 | BINARY[id + 2])
14
- );
10
+ const asCSS = (theme, reverse = false) => {
11
+ let res;
12
+ if (isNumber(theme)) {
13
+ __ensureID(theme);
14
+ theme *= 18;
15
+ res = [];
16
+ for (let i = 0; i < 6; i++, theme += 3) {
17
+ res.push("#" + U24(__read(theme)));
18
+ }
19
+ } else {
20
+ res = theme.map((x) => css(x));
21
+ }
22
+ return reverse ? res.reverse() : res;
23
+ };
24
+ const asInt = (theme, reverse = false) => {
25
+ let res;
26
+ if (isNumber(theme)) {
27
+ __ensureID(theme);
28
+ theme *= 18;
29
+ res = [];
30
+ for (let i = 0; i < 6; i++, theme += 3) {
31
+ res.push(__read(theme));
32
+ }
33
+ } else {
34
+ res = theme.map((x) => argb32(x).deref()[0]);
15
35
  }
16
- return reverse ? theme.reverse() : theme;
36
+ return reverse ? res.reverse() : res;
17
37
  };
18
- const asInt = (id, reverse = false) => {
19
- __ensureID(id);
20
- const theme = [];
21
- id *= 18;
22
- for (let i = 0; i < 6; i++, id += 3) {
23
- theme.push(
24
- (4278190080 | BINARY[id] << 16 | BINARY[id + 1] << 8 | BINARY[id + 2]) >>> 0
25
- );
38
+ const asLCH = (theme, reverse = false) => {
39
+ if (isNumber(theme)) {
40
+ return asRGB(theme, reverse).map((x) => lch(x));
26
41
  }
27
- return reverse ? theme.reverse() : theme;
42
+ const res = theme.map((x) => lch(x));
43
+ return reverse ? res.reverse() : res;
28
44
  };
29
- const asLCH = (id, reverse = false) => asRGB(id, reverse).map((x) => lch(x));
30
- const asRGB = (id, reverse = false) => {
31
- __ensureID(id);
32
- const theme = [];
33
- id *= 18;
34
- for (let i = 0; i < 6; i++, id += 3) {
35
- theme.push(
36
- srgb(
37
- BINARY[id] / 255,
38
- BINARY[id + 1] / 255,
39
- BINARY[id + 2] / 255,
40
- 1
41
- )
42
- );
45
+ const asRGB = (theme, reverse = false) => {
46
+ let res;
47
+ if (isNumber(theme)) {
48
+ __ensureID(theme);
49
+ theme *= 18;
50
+ res = [];
51
+ for (let i = 0; i < 6; i++, theme += 3) {
52
+ res.push(
53
+ srgb(
54
+ BINARY[theme] / 255,
55
+ BINARY[theme + 1] / 255,
56
+ BINARY[theme + 2] / 255,
57
+ 1
58
+ )
59
+ );
60
+ }
61
+ } else {
62
+ res = theme.map((x) => srgb(x));
43
63
  }
44
- return reverse ? theme.reverse() : theme;
64
+ return reverse ? res.reverse() : res;
45
65
  };
46
66
  const cssThemes = (...preds) => __themes(asCSS, preds);
47
67
  const intThemes = (...preds) => __themes(asInt, preds);
@@ -62,6 +82,7 @@ function* __themes(fn, preds, idOnly = false) {
62
82
  }
63
83
  }
64
84
  const __ensureID = (id) => assert(id >= 0 && id < NUM_THEMES, `invalid theme ID`);
85
+ const __read = (i) => (4278190080 | BINARY[i] << 16 | BINARY[i + 1] << 8 | BINARY[i + 2]) >>> 0;
65
86
  export {
66
87
  asCSS,
67
88
  asInt,