@vitessce/sets-utils 3.3.2 → 3.3.4

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.
@@ -1,4 +1,5 @@
1
1
  export { dataToCellSetsTree } from "./CellSetsZarrLoader.js";
2
+ export { getCellColors } from "./interpolate-colors.js";
2
3
  export { treeToCellColorsBySetNames, treeToCellSetColorIndicesBySetNames, treeToSetSizesBySetNames, treeToObjectsBySetNames, treeExportLevelZeroNode, treeExportSet, treeToExpectedCheckedLevel, nodeToLevelDescendantNamePaths, treeToIntersection, treeToUnion, treeToComplement, treeFindNodeByNamePath, treesConflict, nodeTransform, nodeAppendChild, nodePrependChild, nodeInsertChild, filterNode, treeInitialize, initializeCellSetColor, nodeToRenderProps, getCellSetPolygons, treeToMembershipMap, nodeToSet } from "./cell-set-utils.js";
3
4
  export { findLongestCommonPath, filterPathsByExpansionAndSelection, findChangedHierarchy } from "./set-path-utils.js";
4
5
  export { isEqualOrPrefix, tryRenamePath, PATH_SEP, setObsSelection, mergeObsSets, getNextNumberedNodeName, colorArrayToString, callbackOnKeyPress, getLevelTooltipText, pathToKey, getObsInfoFromDataWithinRange } from "./utils.js";
package/dist-tsc/index.js CHANGED
@@ -4,3 +4,4 @@ export { isEqualOrPrefix, tryRenamePath, PATH_SEP, setObsSelection, mergeObsSets
4
4
  export { downloadForUser, handleExportJSON, handleExportTabular, tryUpgradeTreeToLatestSchema, handleImportJSON, handleImportTabular, } from './io.js';
5
5
  export { FILE_EXTENSION_JSON, FILE_EXTENSION_TABULAR, SETS_DATATYPE_OBS, MIME_TYPE_JSON, MIME_TYPE_TABULAR, } from './constants.js';
6
6
  export { dataToCellSetsTree, } from './CellSetsZarrLoader.js';
7
+ export { getCellColors, } from './interpolate-colors.js';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Get a mapping of cell IDs to cell colors based on
3
+ * gene / cell set selection coordination state.
4
+ * @param {object} params
5
+ * @param {array[]} params.cellSetSelection Selected cell sets.
6
+ * @param {object[]} params.cellSetColor Array of cell set color
7
+ * objects, each containing a path and color [r, g, b].
8
+ * @param {string[]} params.obsIndex Array of cell IDs,
9
+ * in order to initialize all cells to the default color.
10
+ * @param {string} params.theme The current theme,
11
+ * in order to get the theme-based default color.
12
+ * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
13
+ */
14
+ export function getCellColors(params: {
15
+ cellSetSelection: array[];
16
+ cellSetColor: object[];
17
+ obsIndex: string[];
18
+ theme: string;
19
+ }): Map<any, any>;
20
+ export function interpolateRdBu(t: any): any[];
21
+ export function interpolatePlasma(t: any): any;
22
+ //# sourceMappingURL=interpolate-colors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolate-colors.d.ts","sourceRoot":"","sources":["../src/interpolate-colors.js"],"names":[],"mappings":"AAoFA;;;;;;;;;;;;GAYG;AACH;IAT2B,gBAAgB,EAAhC,OAAO;IACU,YAAY,EAA7B,MAAM,EAAE;IAES,QAAQ,EAAzB,MAAM,EAAE;IAEO,KAAK,EAApB,MAAM;kBAsBhB;AA/EU,+CAAmD;AAkCrD,+CAA2E"}
@@ -0,0 +1,95 @@
1
+ /* eslint-disable no-nested-ternary */
2
+ /* eslint-disable no-param-reassign */
3
+ import { getDefaultColor } from '@vitessce/utils';
4
+ import { treeToCellColorsBySetNames } from './cell-set-utils.js';
5
+ // The functions defined here have been adapted from d3-interpolate,
6
+ // d3-color, and d3-scale-chromatic.
7
+ // Color string "rgb(r,g,b)" representations are replaced by color array [r, g, b]
8
+ // representations, to allow them to work nicely with deck.gl,
9
+ // without the need to converting back and forth between string and array formats.
10
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/431d21da776f97c632f53a855bd822edfbbcd56e/src/diverging/RdBu.js
11
+ // eslint-disable-next-line max-len
12
+ const schemeRdBu = [[103, 0, 31], [178, 24, 43], [214, 96, 77], [244, 165, 130], [253, 219, 199], [247, 247, 247], [209, 229, 240], [146, 197, 222], [67, 147, 195], [33, 102, 172], [5, 48, 97]];
13
+ // eslint-disable-next-line max-len
14
+ const schemePlasma = [[13, 8, 135], [16, 7, 136], [19, 7, 137], [22, 7, 138], [25, 6, 140], [27, 6, 141], [29, 6, 142], [32, 6, 143], [34, 6, 144], [36, 6, 145], [38, 5, 145], [40, 5, 146], [42, 5, 147], [44, 5, 148], [46, 5, 149], [47, 5, 150], [49, 5, 151], [51, 5, 151], [53, 4, 152], [55, 4, 153], [56, 4, 154], [58, 4, 154], [60, 4, 155], [62, 4, 156], [63, 4, 156], [65, 4, 157], [67, 3, 158], [68, 3, 158], [70, 3, 159], [72, 3, 159], [73, 3, 160], [75, 3, 161], [76, 2, 161], [78, 2, 162], [80, 2, 162], [81, 2, 163], [83, 2, 163], [85, 2, 164], [86, 1, 164], [88, 1, 164], [89, 1, 165], [91, 1, 165], [92, 1, 166], [94, 1, 166], [96, 1, 166], [97, 0, 167], [99, 0, 167], [100, 0, 167], [102, 0, 167], [103, 0, 168], [105, 0, 168], [106, 0, 168], [108, 0, 168], [110, 0, 168], [111, 0, 168], [113, 0, 168], [114, 1, 168], [116, 1, 168], [117, 1, 168], [119, 1, 168], [120, 1, 168], [122, 2, 168], [123, 2, 168], [125, 3, 168], [126, 3, 168], [128, 4, 168], [129, 4, 167], [131, 5, 167], [132, 5, 167], [134, 6, 166], [135, 7, 166], [136, 8, 166], [138, 9, 165], [139, 10, 165], [141, 11, 165], [142, 12, 164], [143, 13, 164], [145, 14, 163], [146, 15, 163], [148, 16, 162], [149, 17, 161], [150, 19, 161], [152, 20, 160], [153, 21, 159], [154, 22, 159], [156, 23, 158], [157, 24, 157], [158, 25, 157], [160, 26, 156], [161, 27, 155], [162, 29, 154], [163, 30, 154], [165, 31, 153], [166, 32, 152], [167, 33, 151], [168, 34, 150], [170, 35, 149], [171, 36, 148], [172, 38, 148], [173, 39, 147], [174, 40, 146], [176, 41, 145], [177, 42, 144], [178, 43, 143], [179, 44, 142], [180, 46, 141], [181, 47, 140], [182, 48, 139], [183, 49, 138], [184, 50, 137], [186, 51, 136], [187, 52, 136], [188, 53, 135], [189, 55, 134], [190, 56, 133], [191, 57, 132], [192, 58, 131], [193, 59, 130], [194, 60, 129], [195, 61, 128], [196, 62, 127], [197, 64, 126], [198, 65, 125], [199, 66, 124], [200, 67, 123], [201, 68, 122], [202, 69, 122], [203, 70, 121], [204, 71, 120], [204, 73, 119], [205, 74, 118], [206, 75, 117], [207, 76, 116], [208, 77, 115], [209, 78, 114], [210, 79, 113], [211, 81, 113], [212, 82, 112], [213, 83, 111], [213, 84, 110], [214, 85, 109], [215, 86, 108], [216, 87, 107], [217, 88, 106], [218, 90, 106], [218, 91, 105], [219, 92, 104], [220, 93, 103], [221, 94, 102], [222, 95, 101], [222, 97, 100], [223, 98, 99], [224, 99, 99], [225, 100, 98], [226, 101, 97], [226, 102, 96], [227, 104, 95], [228, 105, 94], [229, 106, 93], [229, 107, 93], [230, 108, 92], [231, 110, 91], [231, 111, 90], [232, 112, 89], [233, 113, 88], [233, 114, 87], [234, 116, 87], [235, 117, 86], [235, 118, 85], [236, 119, 84], [237, 121, 83], [237, 122, 82], [238, 123, 81], [239, 124, 81], [239, 126, 80], [240, 127, 79], [240, 128, 78], [241, 129, 77], [241, 131, 76], [242, 132, 75], [243, 133, 75], [243, 135, 74], [244, 136, 73], [244, 137, 72], [245, 139, 71], [245, 140, 70], [246, 141, 69], [246, 143, 68], [247, 144, 68], [247, 145, 67], [247, 147, 66], [248, 148, 65], [248, 149, 64], [249, 151, 63], [249, 152, 62], [249, 154, 62], [250, 155, 61], [250, 156, 60], [250, 158, 59], [251, 159, 58], [251, 161, 57], [251, 162, 56], [252, 163, 56], [252, 165, 55], [252, 166, 54], [252, 168, 53], [252, 169, 52], [253, 171, 51], [253, 172, 51], [253, 174, 50], [253, 175, 49], [253, 177, 48], [253, 178, 47], [253, 180, 47], [253, 181, 46], [254, 183, 45], [254, 184, 44], [254, 186, 44], [254, 187, 43], [254, 189, 42], [254, 190, 42], [254, 192, 41], [253, 194, 41], [253, 195, 40], [253, 197, 39], [253, 198, 39], [253, 200, 39], [253, 202, 38], [253, 203, 38], [252, 205, 37], [252, 206, 37], [252, 208, 37], [252, 210, 37], [251, 211, 36], [251, 213, 36], [251, 215, 36], [250, 216, 36], [250, 218, 36], [249, 220, 36], [249, 221, 37], [248, 223, 37], [248, 225, 37], [247, 226, 37], [247, 228, 37], [246, 230, 38], [246, 232, 38], [245, 233, 38], [245, 235, 39], [244, 237, 39], [243, 238, 39], [243, 240, 39], [242, 242, 39], [241, 244, 38], [241, 245, 37], [240, 247, 36], [240, 249, 33]];
15
+ // Reference: https://github.com/d3/d3-interpolate/blob/96d54051d1c2fec55f240edd0ec5401715b10390/src/rgb.js
16
+ function rgbSpline(spline /* : (values: number[]) => ((t: number) => number) */) {
17
+ return (colors /* : number[][] */) => {
18
+ const n = colors.length;
19
+ const r /* : number[] */ = new Array(n);
20
+ const g /* : number[] */ = new Array(n);
21
+ const b /* : number[] */ = new Array(n);
22
+ let i;
23
+ let color;
24
+ // eslint-disable-next-line no-plusplus
25
+ for (i = 0; i < n; ++i) {
26
+ color = [colors[i][0], colors[i][1], colors[i][2]];
27
+ r[i] = color[0] || 0;
28
+ g[i] = color[1] || 0;
29
+ b[i] = color[2] || 0;
30
+ }
31
+ const rFunc /* : (t: number) => number */ = spline(r);
32
+ const gFunc /* : (t: number) => number */ = spline(g);
33
+ const bFunc /* : (t: number) => number */ = spline(b);
34
+ return (t /* : number */) => [rFunc(t), gFunc(t), bFunc(t)];
35
+ };
36
+ }
37
+ // Reference: https://github.com/d3/d3-interpolate/blob/594a32af1fe1118812b439012c2cb742e907c0c0/src/basis.js
38
+ function basis(values /* : number[] */) {
39
+ function innerBasis(t1 /* : number */, v0 /* : number */, v1 /* : number */, v2 /* : number */, v3 /* : number */) {
40
+ const t2 = t1 * t1;
41
+ const t3 = t2 * t1;
42
+ return ((1 - 3 * t1 + 3 * t2 - t3) * v0
43
+ + (4 - 6 * t2 + 3 * t3) * v1
44
+ + (1 + 3 * t1 + 3 * t2 - 3 * t3) * v2
45
+ + t3 * v3) / 6;
46
+ }
47
+ const n = values.length - 1;
48
+ return (t /* : number */) => {
49
+ const i = t <= 0 ? (t = 0) : t >= 1 ? (t = 1, n - 1) : Math.floor(t * n);
50
+ const v1 = values[i];
51
+ const v2 = values[i + 1];
52
+ const v0 = i > 0 ? values[i - 1] : 2 * v1 - v2;
53
+ const v3 = i < n - 1 ? values[i + 2] : 2 * v2 - v1;
54
+ return innerBasis((t - i / n) * n, v0, v1, v2, v3);
55
+ };
56
+ }
57
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/ade54c13e8dfdb9807801a794eaec1a37f926b8a/src/ramp.js
58
+ const interpolateRgbBasis = rgbSpline(basis);
59
+ function interpolateSequentialMulti(range /* : number[][] */) {
60
+ const n = range.length;
61
+ return (t /* : number */) => range[Math.max(0, Math.min(n - 1, Math.floor(t * n)))];
62
+ }
63
+ export const interpolateRdBu = interpolateRgbBasis(schemeRdBu);
64
+ export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
65
+ /* type GetCellColorsParams = {
66
+ cellSetSelection: string[][] | null;
67
+ cellSetColor: { path: string[]; color: [number, number, number] }[] | null;
68
+ cellSets: object; // TODO(ts): more strict typing here
69
+ obsIndex: string[] | null;
70
+ theme: 'light' | 'dark' | null;
71
+ } */
72
+ /**
73
+ * Get a mapping of cell IDs to cell colors based on
74
+ * gene / cell set selection coordination state.
75
+ * @param {object} params
76
+ * @param {array[]} params.cellSetSelection Selected cell sets.
77
+ * @param {object[]} params.cellSetColor Array of cell set color
78
+ * objects, each containing a path and color [r, g, b].
79
+ * @param {string[]} params.obsIndex Array of cell IDs,
80
+ * in order to initialize all cells to the default color.
81
+ * @param {string} params.theme The current theme,
82
+ * in order to get the theme-based default color.
83
+ * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
84
+ */
85
+ export function getCellColors(params /* : GetCellColorsParams */) {
86
+ const { cellSets, cellSetSelection, cellSetColor, obsIndex, theme, } = params;
87
+ if (cellSetSelection && cellSets) {
88
+ // TODO(ts): fix typing here once better typing in sets-utils.
89
+ return treeToCellColorsBySetNames(cellSets, cellSetSelection, cellSetColor /* as object[] */, theme /* as string */);
90
+ }
91
+ if (obsIndex && theme) {
92
+ return new Map(obsIndex.map(o => ([o, getDefaultColor(theme)])));
93
+ }
94
+ return new Map();
95
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=interpolate-colors.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interpolate-colors.test.d.ts","sourceRoot":"","sources":["../src/interpolate-colors.test.js"],"names":[],"mappings":""}
@@ -0,0 +1,25 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { interpolateRdBu, interpolatePlasma } from './interpolate-colors.js';
3
+ const expectRgb = ([testR, testG, testB] /* : number[] */, [r, g, b] /* : number[] */) => ([
4
+ expect(testR).toBeCloseTo(r),
5
+ expect(testG).toBeCloseTo(g),
6
+ expect(testB).toBeCloseTo(b),
7
+ ]);
8
+ describe('components/interpolate-colors', () => {
9
+ describe('interpolateRdBu()', () => {
10
+ it('maps 0 to a red color', () => {
11
+ expectRgb(interpolateRdBu(0), [103, 0, 31]);
12
+ });
13
+ it('maps 1 to a blue color', () => {
14
+ expectRgb(interpolateRdBu(1), [5, 48, 97]);
15
+ });
16
+ });
17
+ describe('interpolatePlasma()', () => {
18
+ it('maps 0 to dark cool color', () => {
19
+ expectRgb(interpolatePlasma(0), [13, 8, 135]);
20
+ });
21
+ it('maps 1 to bright warm color', () => {
22
+ expectRgb(interpolatePlasma(1), [240, 249, 33]);
23
+ });
24
+ });
25
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vitessce/sets-utils",
3
- "version": "3.3.2",
3
+ "version": "3.3.4",
4
4
  "author": "Gehlenborg Lab",
5
5
  "homepage": "http://vitessce.io",
6
6
  "repository": {
@@ -25,8 +25,8 @@
25
25
  "lodash-es": "^4.17.21",
26
26
  "tinycolor2": "^1.4.1",
27
27
  "uuid": "^9.0.0",
28
- "@vitessce/utils": "3.3.2",
29
- "@vitessce/schemas": "3.3.2"
28
+ "@vitessce/schemas": "3.3.4",
29
+ "@vitessce/utils": "3.3.4"
30
30
  },
31
31
  "scripts": {
32
32
  "bundle": "pnpm exec vite build -c ../../../scripts/vite.config.js",
package/src/index.js CHANGED
@@ -60,3 +60,6 @@ export {
60
60
  export {
61
61
  dataToCellSetsTree,
62
62
  } from './CellSetsZarrLoader.js';
63
+ export {
64
+ getCellColors,
65
+ } from './interpolate-colors.js';
@@ -0,0 +1,116 @@
1
+ /* eslint-disable no-nested-ternary */
2
+ /* eslint-disable no-param-reassign */
3
+ import { getDefaultColor } from '@vitessce/utils';
4
+ import { treeToCellColorsBySetNames } from './cell-set-utils.js';
5
+
6
+ // The functions defined here have been adapted from d3-interpolate,
7
+ // d3-color, and d3-scale-chromatic.
8
+ // Color string "rgb(r,g,b)" representations are replaced by color array [r, g, b]
9
+ // representations, to allow them to work nicely with deck.gl,
10
+ // without the need to converting back and forth between string and array formats.
11
+
12
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/431d21da776f97c632f53a855bd822edfbbcd56e/src/diverging/RdBu.js
13
+ // eslint-disable-next-line max-len
14
+ const schemeRdBu = [[103, 0, 31], [178, 24, 43], [214, 96, 77], [244, 165, 130], [253, 219, 199], [247, 247, 247], [209, 229, 240], [146, 197, 222], [67, 147, 195], [33, 102, 172], [5, 48, 97]];
15
+ // eslint-disable-next-line max-len
16
+ const schemePlasma = [[13, 8, 135], [16, 7, 136], [19, 7, 137], [22, 7, 138], [25, 6, 140], [27, 6, 141], [29, 6, 142], [32, 6, 143], [34, 6, 144], [36, 6, 145], [38, 5, 145], [40, 5, 146], [42, 5, 147], [44, 5, 148], [46, 5, 149], [47, 5, 150], [49, 5, 151], [51, 5, 151], [53, 4, 152], [55, 4, 153], [56, 4, 154], [58, 4, 154], [60, 4, 155], [62, 4, 156], [63, 4, 156], [65, 4, 157], [67, 3, 158], [68, 3, 158], [70, 3, 159], [72, 3, 159], [73, 3, 160], [75, 3, 161], [76, 2, 161], [78, 2, 162], [80, 2, 162], [81, 2, 163], [83, 2, 163], [85, 2, 164], [86, 1, 164], [88, 1, 164], [89, 1, 165], [91, 1, 165], [92, 1, 166], [94, 1, 166], [96, 1, 166], [97, 0, 167], [99, 0, 167], [100, 0, 167], [102, 0, 167], [103, 0, 168], [105, 0, 168], [106, 0, 168], [108, 0, 168], [110, 0, 168], [111, 0, 168], [113, 0, 168], [114, 1, 168], [116, 1, 168], [117, 1, 168], [119, 1, 168], [120, 1, 168], [122, 2, 168], [123, 2, 168], [125, 3, 168], [126, 3, 168], [128, 4, 168], [129, 4, 167], [131, 5, 167], [132, 5, 167], [134, 6, 166], [135, 7, 166], [136, 8, 166], [138, 9, 165], [139, 10, 165], [141, 11, 165], [142, 12, 164], [143, 13, 164], [145, 14, 163], [146, 15, 163], [148, 16, 162], [149, 17, 161], [150, 19, 161], [152, 20, 160], [153, 21, 159], [154, 22, 159], [156, 23, 158], [157, 24, 157], [158, 25, 157], [160, 26, 156], [161, 27, 155], [162, 29, 154], [163, 30, 154], [165, 31, 153], [166, 32, 152], [167, 33, 151], [168, 34, 150], [170, 35, 149], [171, 36, 148], [172, 38, 148], [173, 39, 147], [174, 40, 146], [176, 41, 145], [177, 42, 144], [178, 43, 143], [179, 44, 142], [180, 46, 141], [181, 47, 140], [182, 48, 139], [183, 49, 138], [184, 50, 137], [186, 51, 136], [187, 52, 136], [188, 53, 135], [189, 55, 134], [190, 56, 133], [191, 57, 132], [192, 58, 131], [193, 59, 130], [194, 60, 129], [195, 61, 128], [196, 62, 127], [197, 64, 126], [198, 65, 125], [199, 66, 124], [200, 67, 123], [201, 68, 122], [202, 69, 122], [203, 70, 121], [204, 71, 120], [204, 73, 119], [205, 74, 118], [206, 75, 117], [207, 76, 116], [208, 77, 115], [209, 78, 114], [210, 79, 113], [211, 81, 113], [212, 82, 112], [213, 83, 111], [213, 84, 110], [214, 85, 109], [215, 86, 108], [216, 87, 107], [217, 88, 106], [218, 90, 106], [218, 91, 105], [219, 92, 104], [220, 93, 103], [221, 94, 102], [222, 95, 101], [222, 97, 100], [223, 98, 99], [224, 99, 99], [225, 100, 98], [226, 101, 97], [226, 102, 96], [227, 104, 95], [228, 105, 94], [229, 106, 93], [229, 107, 93], [230, 108, 92], [231, 110, 91], [231, 111, 90], [232, 112, 89], [233, 113, 88], [233, 114, 87], [234, 116, 87], [235, 117, 86], [235, 118, 85], [236, 119, 84], [237, 121, 83], [237, 122, 82], [238, 123, 81], [239, 124, 81], [239, 126, 80], [240, 127, 79], [240, 128, 78], [241, 129, 77], [241, 131, 76], [242, 132, 75], [243, 133, 75], [243, 135, 74], [244, 136, 73], [244, 137, 72], [245, 139, 71], [245, 140, 70], [246, 141, 69], [246, 143, 68], [247, 144, 68], [247, 145, 67], [247, 147, 66], [248, 148, 65], [248, 149, 64], [249, 151, 63], [249, 152, 62], [249, 154, 62], [250, 155, 61], [250, 156, 60], [250, 158, 59], [251, 159, 58], [251, 161, 57], [251, 162, 56], [252, 163, 56], [252, 165, 55], [252, 166, 54], [252, 168, 53], [252, 169, 52], [253, 171, 51], [253, 172, 51], [253, 174, 50], [253, 175, 49], [253, 177, 48], [253, 178, 47], [253, 180, 47], [253, 181, 46], [254, 183, 45], [254, 184, 44], [254, 186, 44], [254, 187, 43], [254, 189, 42], [254, 190, 42], [254, 192, 41], [253, 194, 41], [253, 195, 40], [253, 197, 39], [253, 198, 39], [253, 200, 39], [253, 202, 38], [253, 203, 38], [252, 205, 37], [252, 206, 37], [252, 208, 37], [252, 210, 37], [251, 211, 36], [251, 213, 36], [251, 215, 36], [250, 216, 36], [250, 218, 36], [249, 220, 36], [249, 221, 37], [248, 223, 37], [248, 225, 37], [247, 226, 37], [247, 228, 37], [246, 230, 38], [246, 232, 38], [245, 233, 38], [245, 235, 39], [244, 237, 39], [243, 238, 39], [243, 240, 39], [242, 242, 39], [241, 244, 38], [241, 245, 37], [240, 247, 36], [240, 249, 33]];
17
+
18
+ // Reference: https://github.com/d3/d3-interpolate/blob/96d54051d1c2fec55f240edd0ec5401715b10390/src/rgb.js
19
+ function rgbSpline(spline/* : (values: number[]) => ((t: number) => number) */) {
20
+ return (colors/* : number[][] */) => {
21
+ const n = colors.length;
22
+ const r/* : number[] */ = new Array(n);
23
+ const g/* : number[] */ = new Array(n);
24
+ const b/* : number[] */ = new Array(n);
25
+ let i; let
26
+ color;
27
+ // eslint-disable-next-line no-plusplus
28
+ for (i = 0; i < n; ++i) {
29
+ color = [colors[i][0], colors[i][1], colors[i][2]];
30
+ r[i] = color[0] || 0;
31
+ g[i] = color[1] || 0;
32
+ b[i] = color[2] || 0;
33
+ }
34
+ const rFunc/* : (t: number) => number */ = spline(r);
35
+ const gFunc/* : (t: number) => number */ = spline(g);
36
+ const bFunc/* : (t: number) => number */ = spline(b);
37
+ return (t/* : number */) => [rFunc(t), gFunc(t), bFunc(t)];
38
+ };
39
+ }
40
+
41
+ // Reference: https://github.com/d3/d3-interpolate/blob/594a32af1fe1118812b439012c2cb742e907c0c0/src/basis.js
42
+ function basis(values/* : number[] */) {
43
+ function innerBasis(
44
+ t1/* : number */, v0/* : number */, v1/* : number */, v2/* : number */, v3/* : number */,
45
+ ) {
46
+ const t2 = t1 * t1; const
47
+ t3 = t2 * t1;
48
+ return ((1 - 3 * t1 + 3 * t2 - t3) * v0
49
+ + (4 - 6 * t2 + 3 * t3) * v1
50
+ + (1 + 3 * t1 + 3 * t2 - 3 * t3) * v2
51
+ + t3 * v3) / 6;
52
+ }
53
+
54
+ const n = values.length - 1;
55
+ return (t/* : number */) => {
56
+ const i = t <= 0 ? (t = 0) : t >= 1 ? (t = 1, n - 1) : Math.floor(t * n);
57
+ const v1 = values[i];
58
+ const v2 = values[i + 1];
59
+ const v0 = i > 0 ? values[i - 1] : 2 * v1 - v2;
60
+ const v3 = i < n - 1 ? values[i + 2] : 2 * v2 - v1;
61
+ return innerBasis((t - i / n) * n, v0, v1, v2, v3);
62
+ };
63
+ }
64
+
65
+
66
+ // Reference: https://github.com/d3/d3-scale-chromatic/blob/ade54c13e8dfdb9807801a794eaec1a37f926b8a/src/ramp.js
67
+ const interpolateRgbBasis = rgbSpline(basis);
68
+
69
+ function interpolateSequentialMulti(range/* : number[][] */) {
70
+ const n = range.length;
71
+ return (t/* : number */) => range[Math.max(0, Math.min(n - 1, Math.floor(t * n)))];
72
+ }
73
+
74
+ export const interpolateRdBu = interpolateRgbBasis(schemeRdBu);
75
+ export const interpolatePlasma = interpolateSequentialMulti(schemePlasma);
76
+
77
+ /* type GetCellColorsParams = {
78
+ cellSetSelection: string[][] | null;
79
+ cellSetColor: { path: string[]; color: [number, number, number] }[] | null;
80
+ cellSets: object; // TODO(ts): more strict typing here
81
+ obsIndex: string[] | null;
82
+ theme: 'light' | 'dark' | null;
83
+ } */
84
+
85
+ /**
86
+ * Get a mapping of cell IDs to cell colors based on
87
+ * gene / cell set selection coordination state.
88
+ * @param {object} params
89
+ * @param {array[]} params.cellSetSelection Selected cell sets.
90
+ * @param {object[]} params.cellSetColor Array of cell set color
91
+ * objects, each containing a path and color [r, g, b].
92
+ * @param {string[]} params.obsIndex Array of cell IDs,
93
+ * in order to initialize all cells to the default color.
94
+ * @param {string} params.theme The current theme,
95
+ * in order to get the theme-based default color.
96
+ * @returns {Map} Mapping from cell IDs to [r, g, b] color arrays.
97
+ */
98
+ export function getCellColors(params/* : GetCellColorsParams */)/* : Map<string, number[]> */ {
99
+ const {
100
+ cellSets,
101
+ cellSetSelection,
102
+ cellSetColor,
103
+ obsIndex,
104
+ theme,
105
+ } = params;
106
+ if (cellSetSelection && cellSets) {
107
+ // TODO(ts): fix typing here once better typing in sets-utils.
108
+ return treeToCellColorsBySetNames(
109
+ cellSets, cellSetSelection, cellSetColor/* as object[] */, theme/* as string */,
110
+ );
111
+ }
112
+ if (obsIndex && theme) {
113
+ return new Map(obsIndex.map(o => ([o, getDefaultColor(theme)])));
114
+ }
115
+ return new Map();
116
+ }
@@ -0,0 +1,30 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { interpolateRdBu, interpolatePlasma } from './interpolate-colors.js';
3
+
4
+ const expectRgb = ([testR, testG, testB]/* : number[] */, [r, g, b]/* : number[] */) => ([
5
+ expect(testR).toBeCloseTo(r),
6
+ expect(testG).toBeCloseTo(g),
7
+ expect(testB).toBeCloseTo(b),
8
+ ]);
9
+
10
+ describe('components/interpolate-colors', () => {
11
+ describe('interpolateRdBu()', () => {
12
+ it('maps 0 to a red color', () => {
13
+ expectRgb(interpolateRdBu(0), [103, 0, 31]);
14
+ });
15
+
16
+ it('maps 1 to a blue color', () => {
17
+ expectRgb(interpolateRdBu(1), [5, 48, 97]);
18
+ });
19
+ });
20
+
21
+ describe('interpolatePlasma()', () => {
22
+ it('maps 0 to dark cool color', () => {
23
+ expectRgb(interpolatePlasma(0), [13, 8, 135]);
24
+ });
25
+
26
+ it('maps 1 to bright warm color', () => {
27
+ expectRgb(interpolatePlasma(1), [240, 249, 33]);
28
+ });
29
+ });
30
+ });