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

29 lines
588 B
TypeScript
Raw Permalink Normal View History

export function paintLogarithmic(
width: number,
height: number,
hist: number[]
) {
const img =
new ImageData(width, height);
2024-12-01 21:57:10 -05:00
const histLog = hist.map(Math.log);
2024-12-01 21:57:10 -05:00
let hLogMax = -Infinity;
for (let value of histLog) {
hLogMax = Math.max(hLogMax, 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; // red
img.data[pixelIndex + 1] = 0; // green
img.data[pixelIndex + 2] = 0; // blue
const alpha =
histLog[i] / hLogMax * 0xff;
img.data[pixelIndex + 3] = alpha;
}
return img;
2024-12-01 21:57:10 -05:00
}