@vizhub/scientific-colormaps 0.1.0 → 0.2.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/README.md +4 -4
- package/package.json +5 -1
- package/src/index.d.ts +66 -7
- package/src/index.js +77 -11
- package/src/schemes.js +10326 -3
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ An independent, dependency-free npm package of **Fabio Crameri's Scientific Colo
|
|
|
10
10
|
npm install @vizhub/scientific-colormaps
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
**Release status:**
|
|
13
|
+
**Release status:** Published on npm as `@vizhub/scientific-colormaps` (maintainer-confirmed November 23, 2026). Run `npm view @vizhub/scientific-colormaps version` to confirm the currently available registry version before announcing or demoing it. The package's declared source version is not a substitute for a live registry check.
|
|
14
14
|
|
|
15
15
|
## Use
|
|
16
16
|
|
|
@@ -64,12 +64,12 @@ npm pack --dry-run
|
|
|
64
64
|
|
|
65
65
|
No dependency installation or bundling is required. The package publishes directly from `src/` as ESM with bundled TypeScript declarations.
|
|
66
66
|
|
|
67
|
-
##
|
|
67
|
+
## Future release checklist
|
|
68
68
|
|
|
69
|
-
1. Confirm `@vizhub` npm organization ownership and publish permissions.
|
|
69
|
+
1. Confirm `@vizhub` npm organization ownership and publish permissions; check the already-published version with `npm view @vizhub/scientific-colormaps version` before bumping the package version.
|
|
70
70
|
2. Confirm upstream attribution and license terms in `NOTICE.md`.
|
|
71
71
|
3. Run `npm test && npm pack --dry-run` in the repository root and confirm the included file list.
|
|
72
|
-
4. Publish `npm publish --access public`
|
|
72
|
+
4. Publish a new, incremented version with `npm publish --access public` using trusted npm authentication, then confirm that the registry reports that version.
|
|
73
73
|
5. Configure npm trusted publishing and provenance if automated releases are desired.
|
|
74
74
|
|
|
75
75
|
This repository is the canonical standalone package. The corresponding draft in the VizHub application repository is superseded.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vizhub/scientific-colormaps",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Fabio Crameri's accessible scientific colormaps for JavaScript, D3, and TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,8 +28,12 @@
|
|
|
28
28
|
},
|
|
29
29
|
"scripts": {
|
|
30
30
|
"test": "node --test",
|
|
31
|
+
"prettier": "prettier --write \"src/**/*.{js,d.ts}\" \"test/**/*.js\" package.json",
|
|
31
32
|
"prepack": "npm test"
|
|
32
33
|
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"prettier": "^3.3.3"
|
|
36
|
+
},
|
|
33
37
|
"publishConfig": {
|
|
34
38
|
"access": "public"
|
|
35
39
|
},
|
package/src/index.d.ts
CHANGED
|
@@ -1,11 +1,70 @@
|
|
|
1
1
|
/** Canonical palette identifiers (case-insensitive when passed to functions). */
|
|
2
|
-
export type ColormapName =
|
|
3
|
-
|
|
2
|
+
export type ColormapName =
|
|
3
|
+
| "acton"
|
|
4
|
+
| "bam"
|
|
5
|
+
| "bamO"
|
|
6
|
+
| "bamako"
|
|
7
|
+
| "batlow"
|
|
8
|
+
| "batlowK"
|
|
9
|
+
| "batlowW"
|
|
10
|
+
| "berlin"
|
|
11
|
+
| "bilbao"
|
|
12
|
+
| "broc"
|
|
13
|
+
| "brocO"
|
|
14
|
+
| "buda"
|
|
15
|
+
| "bukavu"
|
|
16
|
+
| "cork"
|
|
17
|
+
| "corkO"
|
|
18
|
+
| "davos"
|
|
19
|
+
| "devon"
|
|
20
|
+
| "fes"
|
|
21
|
+
| "glasgow"
|
|
22
|
+
| "grayC"
|
|
23
|
+
| "hawaii"
|
|
24
|
+
| "imola"
|
|
25
|
+
| "lajolla"
|
|
26
|
+
| "lapaz"
|
|
27
|
+
| "lipari"
|
|
28
|
+
| "lisbon"
|
|
29
|
+
| "managua"
|
|
30
|
+
| "navia"
|
|
31
|
+
| "naviaW"
|
|
32
|
+
| "nuuk"
|
|
33
|
+
| "oleron"
|
|
34
|
+
| "oslo"
|
|
35
|
+
| "roma"
|
|
36
|
+
| "romaO"
|
|
37
|
+
| "tofino"
|
|
38
|
+
| "tokyo"
|
|
39
|
+
| "turku"
|
|
40
|
+
| "vanimo"
|
|
41
|
+
| "vik"
|
|
42
|
+
| "vikO";
|
|
43
|
+
export interface ColormapOptions {
|
|
44
|
+
readonly reverse?: boolean;
|
|
45
|
+
}
|
|
4
46
|
export declare const schemes: Readonly<Record<ColormapName, readonly string[]>>;
|
|
5
47
|
export declare const rawschemes: typeof schemes;
|
|
6
48
|
export declare const paletteNames: readonly ColormapName[];
|
|
7
|
-
export declare function getColormap(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export declare function
|
|
49
|
+
export declare function getColormap(
|
|
50
|
+
name: ColormapName | string,
|
|
51
|
+
options?: ColormapOptions,
|
|
52
|
+
): string[];
|
|
53
|
+
export declare function interpolateColormap(
|
|
54
|
+
name: ColormapName | string,
|
|
55
|
+
t: number,
|
|
56
|
+
options?: ColormapOptions,
|
|
57
|
+
): string;
|
|
58
|
+
export declare function createInterpolator(
|
|
59
|
+
name: ColormapName | string,
|
|
60
|
+
options?: ColormapOptions,
|
|
61
|
+
): (t: number) => string;
|
|
62
|
+
export declare function ramp(
|
|
63
|
+
colorsOrName: readonly string[] | ColormapName | string,
|
|
64
|
+
options?: ColormapOptions,
|
|
65
|
+
): (t: number) => string;
|
|
66
|
+
export declare function sampleColormap(
|
|
67
|
+
name: ColormapName | string,
|
|
68
|
+
count?: number,
|
|
69
|
+
options?: ColormapOptions,
|
|
70
|
+
): string[];
|
package/src/index.js
CHANGED
|
@@ -2,12 +2,24 @@ import { schemes, paletteNames } from "./schemes.js";
|
|
|
2
2
|
|
|
3
3
|
export { schemes, paletteNames } from "./schemes.js";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {{ reverse?: boolean }} ColormapOptions
|
|
7
|
+
*/
|
|
8
|
+
|
|
5
9
|
/** Compatibility alias for the original Observable notebook's raw lookup tables. */
|
|
6
10
|
export const rawschemes = schemes;
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
/** @type {Map<string, keyof typeof schemes>} */
|
|
13
|
+
const byLowerCase = new Map(
|
|
14
|
+
paletteNames.map((name) => [name.toLowerCase(), name]),
|
|
15
|
+
);
|
|
9
16
|
const hexPattern = /^#[0-9a-f]{6}$/i;
|
|
10
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} name
|
|
20
|
+
* @param {ColormapOptions} [options]
|
|
21
|
+
* @returns {{ colors: readonly string[], reverse: boolean }}
|
|
22
|
+
*/
|
|
11
23
|
function resolvePalette(name, options = {}) {
|
|
12
24
|
if (typeof name !== "string") {
|
|
13
25
|
throw new TypeError("Colormap name must be a string");
|
|
@@ -18,17 +30,38 @@ function resolvePalette(name, options = {}) {
|
|
|
18
30
|
if (key === undefined) {
|
|
19
31
|
throw new RangeError(`Unknown colormap: ${name}`);
|
|
20
32
|
}
|
|
21
|
-
return {
|
|
33
|
+
return {
|
|
34
|
+
colors: schemes[key],
|
|
35
|
+
reverse: Boolean(options.reverse) !== suffixReverse,
|
|
36
|
+
};
|
|
22
37
|
}
|
|
23
38
|
|
|
39
|
+
/**
|
|
40
|
+
* @param {string} hex
|
|
41
|
+
* @returns {[number, number, number]}
|
|
42
|
+
*/
|
|
24
43
|
function rgb(hex) {
|
|
25
|
-
return [
|
|
44
|
+
return [
|
|
45
|
+
parseInt(hex.slice(1, 3), 16),
|
|
46
|
+
parseInt(hex.slice(3, 5), 16),
|
|
47
|
+
parseInt(hex.slice(5, 7), 16),
|
|
48
|
+
];
|
|
26
49
|
}
|
|
27
50
|
|
|
51
|
+
/**
|
|
52
|
+
* @param {number} value
|
|
53
|
+
* @returns {string}
|
|
54
|
+
*/
|
|
28
55
|
function toHex(value) {
|
|
29
56
|
return Math.round(value).toString(16).padStart(2, "0");
|
|
30
57
|
}
|
|
31
58
|
|
|
59
|
+
/**
|
|
60
|
+
* @param {readonly string[]} colors
|
|
61
|
+
* @param {number} t
|
|
62
|
+
* @param {boolean} reverse
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
32
65
|
function interpolate(colors, t, reverse) {
|
|
33
66
|
if (!Number.isFinite(t)) {
|
|
34
67
|
throw new TypeError("Interpolation position must be a finite number");
|
|
@@ -45,45 +78,78 @@ function interpolate(colors, t, reverse) {
|
|
|
45
78
|
return "#" + a.map((x, i) => toHex(x + (b[i] - x) * amount)).join("");
|
|
46
79
|
}
|
|
47
80
|
|
|
48
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* Return a fresh copy of the 256-step lookup table; never mutates the exported schemes.
|
|
83
|
+
* @param {string} name
|
|
84
|
+
* @param {ColormapOptions} [options]
|
|
85
|
+
* @returns {string[]}
|
|
86
|
+
*/
|
|
49
87
|
export function getColormap(name, options) {
|
|
50
88
|
const { colors, reverse } = resolvePalette(name, options);
|
|
51
89
|
const copy = Array.from(colors);
|
|
52
90
|
return reverse ? copy.reverse() : copy;
|
|
53
91
|
}
|
|
54
92
|
|
|
55
|
-
/**
|
|
93
|
+
/**
|
|
94
|
+
* Interpolate a named palette in sRGB (piecewise-linear through its 256 samples).
|
|
95
|
+
* @param {string} name
|
|
96
|
+
* @param {number} t
|
|
97
|
+
* @param {ColormapOptions} [options]
|
|
98
|
+
* @returns {string}
|
|
99
|
+
*/
|
|
56
100
|
export function interpolateColormap(name, t, options) {
|
|
57
101
|
const { colors, reverse } = resolvePalette(name, options);
|
|
58
102
|
return interpolate(colors, t, reverse);
|
|
59
103
|
}
|
|
60
104
|
|
|
61
|
-
/**
|
|
105
|
+
/**
|
|
106
|
+
* Construct an interpolator suitable for d3.scaleSequential or Observable Plot.
|
|
107
|
+
* @param {string} name
|
|
108
|
+
* @param {ColormapOptions} [options]
|
|
109
|
+
* @returns {(t: number) => string}
|
|
110
|
+
*/
|
|
62
111
|
export function createInterpolator(name, options) {
|
|
63
112
|
const { colors, reverse } = resolvePalette(name, options);
|
|
64
113
|
return (t) => interpolate(colors, t, reverse);
|
|
65
114
|
}
|
|
66
115
|
|
|
67
|
-
/**
|
|
116
|
+
/**
|
|
117
|
+
* Notebook-style adapter: ramp(schemes.roma)(0.5), or ramp("roma")(0.5).
|
|
118
|
+
* @param {readonly string[] | string} colorsOrName
|
|
119
|
+
* @param {ColormapOptions} [options]
|
|
120
|
+
* @returns {(t: number) => string}
|
|
121
|
+
*/
|
|
68
122
|
export function ramp(colorsOrName, options = {}) {
|
|
69
123
|
if (typeof colorsOrName === "string") {
|
|
70
124
|
return createInterpolator(colorsOrName, options);
|
|
71
125
|
}
|
|
72
|
-
if (
|
|
73
|
-
|
|
126
|
+
if (
|
|
127
|
+
!Array.isArray(colorsOrName) ||
|
|
128
|
+
colorsOrName.length < 2 ||
|
|
129
|
+
!colorsOrName.every((c) => typeof c === "string" && hexPattern.test(c))
|
|
130
|
+
) {
|
|
131
|
+
throw new TypeError(
|
|
132
|
+
"ramp expects a palette name or an array of at least two #rrggbb colors",
|
|
133
|
+
);
|
|
74
134
|
}
|
|
75
135
|
const colors = Array.from(colorsOrName);
|
|
76
136
|
const reverse = Boolean(options.reverse);
|
|
77
137
|
return (t) => interpolate(colors, t, reverse);
|
|
78
138
|
}
|
|
79
139
|
|
|
80
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* Sample an evenly spaced discrete palette; for count=1 use the midpoint.
|
|
142
|
+
* @param {string} name
|
|
143
|
+
* @param {number} [count]
|
|
144
|
+
* @param {ColormapOptions} [options]
|
|
145
|
+
* @returns {string[]}
|
|
146
|
+
*/
|
|
81
147
|
export function sampleColormap(name, count = 256, options) {
|
|
82
148
|
if (!Number.isInteger(count) || count < 1 || count > 65536) {
|
|
83
149
|
throw new RangeError("Sample count must be an integer between 1 and 65536");
|
|
84
150
|
}
|
|
85
151
|
const interpolateValue = createInterpolator(name, options);
|
|
86
152
|
return Array.from({ length: count }, (_, index) =>
|
|
87
|
-
interpolateValue(count === 1 ? 0.5 : index / (count - 1))
|
|
153
|
+
interpolateValue(count === 1 ? 0.5 : index / (count - 1)),
|
|
88
154
|
);
|
|
89
155
|
}
|