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

18 lines
546 B
TypeScript
Raw Normal View History

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