@thi.ng/color-palettes 0.9.9 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thi.ng/color-palettes",
3
- "version": "0.9.9",
4
- "description": "Collection of 190+ image based color palettes",
3
+ "version": "1.0.2",
4
+ "description": "Collection of 200+ image based color themes & composable theme query filters",
5
5
  "type": "module",
6
6
  "module": "./index.js",
7
7
  "typings": "./index.d.ts",
@@ -32,11 +32,20 @@
32
32
  "doc:stats": "tools:module-stats",
33
33
  "pub": "yarn npm publish --access public",
34
34
  "test": "testament test",
35
- "tool:swatches": "tools:node-esm tools/index.ts"
35
+ "build:binary": "tools:node-esm tools/encode.ts",
36
+ "tool:swatches": "tools:node-esm tools/swatches.ts"
37
+ },
38
+ "dependencies": {
39
+ "@thi.ng/api": "^8.7.2",
40
+ "@thi.ng/base-n": "^2.4.1",
41
+ "@thi.ng/checks": "^3.3.9",
42
+ "@thi.ng/color": "^5.3.2",
43
+ "@thi.ng/errors": "^2.2.11",
44
+ "@thi.ng/hex": "^2.3.6"
36
45
  },
37
46
  "devDependencies": {
38
47
  "@microsoft/api-extractor": "^7.34.2",
39
- "@thi.ng/testament": "^0.3.10",
48
+ "@thi.ng/testament": "^0.3.11",
40
49
  "rimraf": "^4.1.2",
41
50
  "tools": "^0.0.1",
42
51
  "typedoc": "^0.23.24",
@@ -66,11 +75,20 @@
66
75
  "exports": {
67
76
  ".": {
68
77
  "default": "./index.js"
78
+ },
79
+ "./api": {
80
+ "default": "./api.js"
81
+ },
82
+ "./filter": {
83
+ "default": "./filter.js"
84
+ },
85
+ "./theme": {
86
+ "default": "./theme.js"
69
87
  }
70
88
  },
71
89
  "thi.ng": {
72
90
  "parent": "@thi.ng/color",
73
91
  "year": 2021
74
92
  },
75
- "gitHead": "50ba9c87676fac60c46d2bc0e4d2c7711a374a68\n"
93
+ "gitHead": "7896250248d960109253215c75b115c46626293e\n"
76
94
  }
package/query.d.ts ADDED
@@ -0,0 +1,106 @@
1
+ import type { ColorPredicate, ThemeColor, ThemePredicate } from "./api.js";
2
+ /**
3
+ * Higher order theme filter. Takes a predicate function which will be applied
4
+ * to a single {@link ThemeColor}. Returns a new function which accepts a single
5
+ * {@link Theme} and returns true iff the given predicate succeeds for at least
6
+ * `threshold` colors in the theme (all colors by default).
7
+ *
8
+ * @param pred
9
+ * @param threshold
10
+ */
11
+ export declare const defFilter: (pred: ColorPredicate, threshold?: number) => ThemePredicate;
12
+ /**
13
+ * Higher order theme filter. Takes a number of theme predicate (e.g. those
14
+ * provided by this package) and returns new predicate function which only
15
+ * succeeds if *all* given predicates pass.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * // compose combined query filter
20
+ * const pastels = comp(
21
+ * // require all theme colors to max 25% chroma
22
+ * filterChroma(0, 0.25),
23
+ * // require at least 3 theme colors to be min 50% luma
24
+ * filterLuma(0.5, 1, 3)
25
+ * );
26
+ *
27
+ * [...cssThemes()].filter(pastels)
28
+ * // [
29
+ * // [ '#453f38', '#746b5d', '#b39777', '#c1c2b2', '#e3dccf', '#f1ede7' ],
30
+ * // [ '#857b84', '#b1a7b0', '#d0c7d0', '#e7e0e8', '#faeceb', '#e4e9fa' ]
31
+ * // ]
32
+ * ```
33
+ *
34
+ * @param filters
35
+ = */
36
+ export declare const comp: (...filters: ThemePredicate[]) => ThemePredicate;
37
+ /**
38
+ * Theme predicate which ensures colors are within the given normalized hue
39
+ * range ([0..1] interval). See {@link defFilter} for more details.
40
+ *
41
+ * @remarks
42
+ * Internally converts colors to LCH (unless already the case). Therefore also
43
+ * uses LCH hues (which are slightly offset compared to HSV/HSL).
44
+ *
45
+ * If `max < min`, the filter will consider hues in both the `[min..1] and
46
+ * [0..max]` intervals (i.e. angular wraparound on color wheel).
47
+ *
48
+ * @param min
49
+ * @param max
50
+ * @param threshold
51
+ */
52
+ export declare const filterHue: (min: number, max: number, threshold?: number) => ThemePredicate;
53
+ /**
54
+ * Theme predicate which ensures colors are within the given normalized chroma
55
+ * range ([0..1] interval). See {@link defFilter} for more details.
56
+ *
57
+ * @remarks
58
+ * Internally converts colors to LCH (unless already the case).
59
+ *
60
+ * @param min
61
+ * @param max
62
+ * @param threshold
63
+ */
64
+ export declare const filterChroma: (min: number, max: number, threshold?: number) => ThemePredicate;
65
+ /**
66
+ * Theme predicate which ensures colors are within the given normalized
67
+ * luminance range ([0..1] interval). See {@link defFilter} for more details.
68
+ *
69
+ * @param min
70
+ * @param max
71
+ * @param threshold
72
+ */
73
+ export declare const filterLuma: (min: number, max: number, threshold?: number) => ThemePredicate;
74
+ /**
75
+ * Theme predicate which ensures colors are within the given normalized distance
76
+ * in LCH space.
77
+ *
78
+ * @remarks
79
+ * Internally converts colors to LCH (unless already the case).
80
+ *
81
+ * See {@link defFilter} &
82
+ * [`distLch()`](https://docs.thi.ng/umbrella/color/functions/distLch.html) for
83
+ * more details.
84
+ *
85
+ * @param min
86
+ * @param max
87
+ * @param threshold
88
+ */
89
+ export declare const filterProximityLCH: (color: ThemeColor, eps: number, threshold?: number) => ThemePredicate;
90
+ /**
91
+ * Theme predicate which ensures colors are within the given normalized distance
92
+ * in RGB space.
93
+ *
94
+ * @remarks
95
+ * Internally converts colors to sRGB (unless already the case).
96
+ *
97
+ * See {@link defFilter} &
98
+ * [`distEucledian3()`](https://docs.thi.ng/umbrella/color/functions/distEucledian3.html)
99
+ * for more details.
100
+ *
101
+ * @param min
102
+ * @param max
103
+ * @param threshold
104
+ */
105
+ export declare const filterProximityRGB: (color: ThemeColor, eps: number, threshold?: number) => ThemePredicate;
106
+ //# sourceMappingURL=query.d.ts.map
package/query.js ADDED
@@ -0,0 +1,135 @@
1
+ import { distEucledian3, distLch } from "@thi.ng/color/distance";
2
+ import { lch } from "@thi.ng/color/lch/lch";
3
+ import { luminance } from "@thi.ng/color/luminance";
4
+ import { srgb } from "@thi.ng/color/srgb/srgb";
5
+ /**
6
+ * Higher order theme filter. Takes a predicate function which will be applied
7
+ * to a single {@link ThemeColor}. Returns a new function which accepts a single
8
+ * {@link Theme} and returns true iff the given predicate succeeds for at least
9
+ * `threshold` colors in the theme (all colors by default).
10
+ *
11
+ * @param pred
12
+ * @param threshold
13
+ */
14
+ export const defFilter = (pred, threshold) => (theme) => {
15
+ const $thresh = threshold ?? theme.length;
16
+ for (let i = theme.length, n = 0; i-- > 0;) {
17
+ if (pred(theme[i])) {
18
+ if (++n >= $thresh)
19
+ return true;
20
+ }
21
+ }
22
+ return false;
23
+ };
24
+ /**
25
+ * Higher order theme filter. Takes a number of theme predicate (e.g. those
26
+ * provided by this package) and returns new predicate function which only
27
+ * succeeds if *all* given predicates pass.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * // compose combined query filter
32
+ * const pastels = comp(
33
+ * // require all theme colors to max 25% chroma
34
+ * filterChroma(0, 0.25),
35
+ * // require at least 3 theme colors to be min 50% luma
36
+ * filterLuma(0.5, 1, 3)
37
+ * );
38
+ *
39
+ * [...cssThemes()].filter(pastels)
40
+ * // [
41
+ * // [ '#453f38', '#746b5d', '#b39777', '#c1c2b2', '#e3dccf', '#f1ede7' ],
42
+ * // [ '#857b84', '#b1a7b0', '#d0c7d0', '#e7e0e8', '#faeceb', '#e4e9fa' ]
43
+ * // ]
44
+ * ```
45
+ *
46
+ * @param filters
47
+ = */
48
+ export const comp = (...filters) => (theme) => filters.every((f) => f(theme));
49
+ /**
50
+ * Theme predicate which ensures colors are within the given normalized hue
51
+ * range ([0..1] interval). See {@link defFilter} for more details.
52
+ *
53
+ * @remarks
54
+ * Internally converts colors to LCH (unless already the case). Therefore also
55
+ * uses LCH hues (which are slightly offset compared to HSV/HSL).
56
+ *
57
+ * If `max < min`, the filter will consider hues in both the `[min..1] and
58
+ * [0..max]` intervals (i.e. angular wraparound on color wheel).
59
+ *
60
+ * @param min
61
+ * @param max
62
+ * @param threshold
63
+ */
64
+ export const filterHue = (min, max, threshold) => defFilter((col) => {
65
+ let hue = __isLCH(col) ? col[2] : lch(col)[2];
66
+ hue = hue - Math.floor(hue);
67
+ return min <= max ? hue >= min && hue <= max : hue >= min || hue <= max;
68
+ }, threshold);
69
+ /**
70
+ * Theme predicate which ensures colors are within the given normalized chroma
71
+ * range ([0..1] interval). See {@link defFilter} for more details.
72
+ *
73
+ * @remarks
74
+ * Internally converts colors to LCH (unless already the case).
75
+ *
76
+ * @param min
77
+ * @param max
78
+ * @param threshold
79
+ */
80
+ export const filterChroma = (min, max, threshold) => defFilter((col) => {
81
+ const sat = __isLCH(col) ? col[1] : lch(col)[1];
82
+ return sat >= min && sat <= max;
83
+ }, threshold);
84
+ /**
85
+ * Theme predicate which ensures colors are within the given normalized
86
+ * luminance range ([0..1] interval). See {@link defFilter} for more details.
87
+ *
88
+ * @param min
89
+ * @param max
90
+ * @param threshold
91
+ */
92
+ export const filterLuma = (min, max, threshold) => defFilter((col) => {
93
+ const l = luminance(col);
94
+ return l >= min && l <= max;
95
+ }, threshold);
96
+ /**
97
+ * Theme predicate which ensures colors are within the given normalized distance
98
+ * in LCH space.
99
+ *
100
+ * @remarks
101
+ * Internally converts colors to LCH (unless already the case).
102
+ *
103
+ * See {@link defFilter} &
104
+ * [`distLch()`](https://docs.thi.ng/umbrella/color/functions/distLch.html) for
105
+ * more details.
106
+ *
107
+ * @param min
108
+ * @param max
109
+ * @param threshold
110
+ */
111
+ export const filterProximityLCH = (color, eps, threshold) => {
112
+ const $color = __isLCH(color) ? color : lch(color);
113
+ return defFilter((x) => distLch($color, __isLCH(x) ? x : lch(x)) < eps, threshold);
114
+ };
115
+ /**
116
+ * Theme predicate which ensures colors are within the given normalized distance
117
+ * in RGB space.
118
+ *
119
+ * @remarks
120
+ * Internally converts colors to sRGB (unless already the case).
121
+ *
122
+ * See {@link defFilter} &
123
+ * [`distEucledian3()`](https://docs.thi.ng/umbrella/color/functions/distEucledian3.html)
124
+ * for more details.
125
+ *
126
+ * @param min
127
+ * @param max
128
+ * @param threshold
129
+ */
130
+ export const filterProximityRGB = (color, eps, threshold) => {
131
+ const $color = __isRGB(color) ? color : srgb(color);
132
+ return defFilter((x) => distEucledian3($color, __isRGB(x) ? x : srgb(x)) < eps, threshold);
133
+ };
134
+ const __isLCH = (x) => typeof x !== "string" && x.mode === "lch";
135
+ const __isRGB = (x) => typeof x !== "string" && x.mode === "srgb";
package/theme.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ import type { CSSTheme, IntTheme, LCHTheme, RGBTheme, ThemePredicate } from "./api.js";
2
+ /**
3
+ * Returns theme for given ID as CSS hex colors.
4
+ *
5
+ * @param id
6
+ */
7
+ export declare const asCSS: (id: number) => CSSTheme;
8
+ /**
9
+ * Returns theme for given ID as packed ARGB integers (alpha channel will always
10
+ * be set to 0xff).
11
+ *
12
+ * @param id
13
+ */
14
+ export declare const asInt: (id: number) => IntTheme;
15
+ /**
16
+ * Returns theme for given ID as thi.ng/color LCH color vectors.
17
+ *
18
+ * @param id
19
+ */
20
+ export declare const asLCH: (id: number) => LCHTheme;
21
+ /**
22
+ * Returns theme for given ID as thi.ng/color sRGB color vectors.
23
+ *
24
+ * @param id
25
+ */
26
+ export declare const asRGB: (id: number) => RGBTheme;
27
+ /**
28
+ * Yields iterator of CSS themes (via {@link asCSS}). Yields all
29
+ * themes unless specific theme IDs or filter predicates are provided.
30
+ *
31
+ * @param preds
32
+ */
33
+ export declare const cssThemes: (...preds: ThemePredicate[] | number[]) => Generator<CSSTheme, void, unknown>;
34
+ /**
35
+ * Yields iterator of packed ARGB integer themes (via {@link asInt}). Yields all
36
+ * themes unless specific theme IDs or filter predicates are provided.
37
+ *
38
+ * @param preds
39
+ */
40
+ export declare const intThemes: (...preds: ThemePredicate[] | number[]) => Generator<IntTheme, void, unknown>;
41
+ /**
42
+ * Yields iterator of LCH themes (via {@link asLCH}). Yields all
43
+ * themes unless specific theme IDs or filter predicates are provided.
44
+ *
45
+ * @param preds
46
+ */
47
+ export declare const lchThemes: (...preds: ThemePredicate[] | number[]) => Generator<LCHTheme, void, unknown>;
48
+ /**
49
+ * Yields iterator of RGB themes (via {@link asRGB}). Yields all
50
+ * themes unless specific theme IDs or filter predicates are provided.
51
+ *
52
+ * @param preds
53
+ */
54
+ export declare const rgbThemes: (...preds: ThemePredicate[] | number[]) => Generator<RGBTheme, void, unknown>;
55
+ //# sourceMappingURL=theme.d.ts.map
package/theme.js ADDED
@@ -0,0 +1,111 @@
1
+ import { lch } from "@thi.ng/color/lch/lch";
2
+ import { srgb } from "@thi.ng/color/srgb/srgb";
3
+ import { assert } from "@thi.ng/errors/assert";
4
+ import { U24 } from "@thi.ng/hex";
5
+ import { BINARY, NUM_THEMES } from "./binary.js";
6
+ import { compFilter } from "./filter.js";
7
+ /**
8
+ * Returns theme for given ID as CSS hex colors.
9
+ *
10
+ * @param id
11
+ */
12
+ export const asCSS = (id) => {
13
+ __ensureID(id);
14
+ const theme = [];
15
+ // (<any>theme).__id = id;
16
+ id *= 18;
17
+ for (let i = 0; i < 6; i++, id += 3) {
18
+ theme.push("#" +
19
+ U24((BINARY[id] << 16) | (BINARY[id + 1] << 8) | BINARY[id + 2]));
20
+ }
21
+ return theme;
22
+ };
23
+ /**
24
+ * Returns theme for given ID as packed ARGB integers (alpha channel will always
25
+ * be set to 0xff).
26
+ *
27
+ * @param id
28
+ */
29
+ export const asInt = (id) => {
30
+ __ensureID(id);
31
+ const theme = [];
32
+ id *= 18;
33
+ for (let i = 0; i < 6; i++, id += 3) {
34
+ theme.push((0xff000000 |
35
+ (BINARY[id] << 16) |
36
+ (BINARY[id + 1] << 8) |
37
+ BINARY[id + 2]) >>>
38
+ 0);
39
+ }
40
+ return theme;
41
+ };
42
+ /**
43
+ * Returns theme for given ID as thi.ng/color LCH color vectors.
44
+ *
45
+ * @param id
46
+ */
47
+ export const asLCH = (id) => asRGB(id).map((x) => lch(x));
48
+ /**
49
+ * Returns theme for given ID as thi.ng/color sRGB color vectors.
50
+ *
51
+ * @param id
52
+ */
53
+ export const asRGB = (id) => {
54
+ __ensureID(id);
55
+ const theme = [];
56
+ // (<any>theme).__id = id;
57
+ id *= 18;
58
+ for (let i = 0; i < 6; i++, id += 3) {
59
+ theme.push(srgb(BINARY[id] / 255, BINARY[id + 1] / 255, BINARY[id + 2] / 255, 1));
60
+ }
61
+ return theme;
62
+ };
63
+ /**
64
+ * Yields iterator of CSS themes (via {@link asCSS}). Yields all
65
+ * themes unless specific theme IDs or filter predicates are provided.
66
+ *
67
+ * @param preds
68
+ */
69
+ export const cssThemes = (...preds) => __themes(asCSS, preds);
70
+ /**
71
+ * Yields iterator of packed ARGB integer themes (via {@link asInt}). Yields all
72
+ * themes unless specific theme IDs or filter predicates are provided.
73
+ *
74
+ * @param preds
75
+ */
76
+ export const intThemes = (...preds) => __themes(asInt, preds);
77
+ /**
78
+ * Yields iterator of LCH themes (via {@link asLCH}). Yields all
79
+ * themes unless specific theme IDs or filter predicates are provided.
80
+ *
81
+ * @param preds
82
+ */
83
+ export const lchThemes = (...preds) => __themes(asLCH, preds);
84
+ /**
85
+ * Yields iterator of RGB themes (via {@link asRGB}). Yields all
86
+ * themes unless specific theme IDs or filter predicates are provided.
87
+ *
88
+ * @param preds
89
+ */
90
+ export const rgbThemes = (...preds) => __themes(asRGB, preds);
91
+ /** @internal */
92
+ function* __themes(fn, preds) {
93
+ if (preds.length && typeof preds[0] === "function") {
94
+ const pred = compFilter(...preds);
95
+ for (let i = 0; i < NUM_THEMES; i++) {
96
+ const theme = fn(i);
97
+ if (pred(theme))
98
+ yield theme;
99
+ }
100
+ }
101
+ else if (preds.length) {
102
+ for (let id of preds)
103
+ yield fn(id);
104
+ }
105
+ else {
106
+ for (let i = 0; i < NUM_THEMES; i++)
107
+ yield fn(i);
108
+ }
109
+ }
110
+ /** @internal */
111
+ const __ensureID = (id) => assert(id >= 0 && id < NUM_THEMES, `invalid theme ID`);
package/themes.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare const themeAsSRGB: (id: number) => number[][];
2
+ export declare const themeAsCSS: (id: number) => string[];
3
+ //# sourceMappingURL=themes.d.ts.map
package/themes.js ADDED
@@ -0,0 +1,24 @@
1
+ import { U24 } from "@thi.ng/hex";
2
+ import { BINARY } from "./binary.js";
3
+ export const themeAsSRGB = (id) => {
4
+ const theme = [];
5
+ id *= 18;
6
+ for (let i = 0; i < 6; i++, id += 3) {
7
+ theme.push([
8
+ BINARY[id] / 255,
9
+ BINARY[id + 1] / 255,
10
+ BINARY[id + 2] / 255,
11
+ 1,
12
+ ]);
13
+ }
14
+ return theme;
15
+ };
16
+ export const themeAsCSS = (id) => {
17
+ const theme = [];
18
+ id *= 18;
19
+ for (let i = 0; i < 6; i++, id += 3) {
20
+ theme.push("#" +
21
+ U24((BINARY[id] << 16) | (BINARY[id + 1] << 8) | BINARY[id + 2]));
22
+ }
23
+ return theme;
24
+ };