speice.io/blog/2024-11-15-playing-with-fire/3-log-density/paintLinear.ts

26 lines
488 B
TypeScript
Raw Permalink Normal View History

export function paintLinear(
width: number,
height: number,
hist: number[]
) {
const img =
new ImageData(width, height);
2024-12-01 21:57:10 -05:00
let hMax = 0;
for (let value of hist) {
hMax = Math.max(hMax, value);
}
2024-12-01 21:57:10 -05:00
for (let i = 0; i < hist.length; i++) {
const pixelIndex = i * 4;
2024-12-01 21:57:10 -05:00
img.data[pixelIndex] = 0;
img.data[pixelIndex + 1] = 0;
img.data[pixelIndex + 2] = 0;
const alpha = hist[i] / hMax * 0xff;
img.data[pixelIndex + 3] = alpha;
}
return img;
2024-12-01 21:57:10 -05:00
}