heatmaps: part 22

This commit is contained in:
nym21
2026-06-01 17:54:42 +02:00
parent 88c38e74f9
commit 2bbc535b58
12 changed files with 448 additions and 178 deletions
+106 -1
View File
@@ -12,6 +12,25 @@ const INFERNO_STOPS = [
export const INFERNO_LUT = createColorLut(INFERNO_STOPS);
const DIVERGING_NEGATIVE_STOPS = [
[0, 0, 0, 0],
[0.25, 60, 0, 0],
[0.5, 140, 10, 0],
[0.75, 200, 30, 10],
[1, 240, 60, 20],
];
const DIVERGING_POSITIVE_STOPS = [
[0, 0, 0, 0],
[0.25, 0, 40, 0],
[0.5, 0, 110, 10],
[0.75, 10, 180, 20],
[1, 30, 230, 50],
];
export const DIVERGING_NEGATIVE_LUT = createColorLut(DIVERGING_NEGATIVE_STOPS);
export const DIVERGING_POSITIVE_LUT = createColorLut(DIVERGING_POSITIVE_STOPS);
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
@@ -31,7 +50,7 @@ export function intensityColor(lut) {
export function logIntensityColor(lut) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const max = context.grid.getMaxValue();
const max = context.grid.getMaxValue(context.col);
if (max <= 0) return 0x00000000;
const t = Math.log2(value + 1) / Math.log2(max + 1);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
@@ -39,6 +58,92 @@ export function logIntensityColor(lut) {
};
}
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
*/
export function linearIntensityColor(lut) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const t = Math.min(1, value / cap);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} lut
* @param {number} [exponent]
* @returns {HeatmapColorFn}
*/
export function powerIntensityColor(lut, exponent = 0.4) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const t = Math.pow(Math.min(1, value / cap), exponent);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} negativeLut
* @param {ArrayLike<number>} positiveLut
* @param {number} [exponent]
* @returns {HeatmapColorFn}
*/
export function divergingPowerIntensityColor(
negativeLut,
positiveLut,
exponent = 0.4,
) {
return (value, context) => {
if (!Number.isFinite(value) || value === 0) return 0x00000000;
const cap = context.grid.getMagnitudeMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const t = Math.pow(Math.min(1, Math.abs(value) / cap), exponent);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
const lut = value < 0 ? negativeLut : positiveLut;
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} lut
* @param {{ knee?: number, max?: number }} [options]
* @returns {HeatmapColorFn}
*/
export function softIntensityColor(lut, { knee = 0.15, max = 1 } = {}) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const ratio = Math.min(1, value / cap);
const t = (ratio / (ratio + knee)) * max;
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {ArrayLike<number>} lut
* @returns {HeatmapColorFn}
*/
export function smoothLogIntensityColor(lut) {
return (value, context) => {
if (!Number.isFinite(value) || value <= 0) return 0x00000000;
const cap = context.grid.getMaxValue(context.col);
if (cap <= 0) return 0x00000000;
const u = Math.log1p(value) / Math.log1p(cap);
const t = u * u * (3 - 2 * u);
const index = Math.min(255, Math.max(0, Math.round(t * 255)));
return lut[index] ?? 0x00000000;
};
}
/**
* @param {number[][]} stops - Tuples of [position, red, green, blue].
*/