Public
Edited
Dec 11, 2024
1 fork
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
renderImage(image, grayscale, grain(noise))
Insert cell
grain = (intensity) => (pixels) => {
const d = pixels.data;
for (let i = 0; i < d.length; i += 4) {
let v = d[i];
const n = math.randomGaussian(
0,
Math.sin(math.map(v, 0, 255, 0, Math.PI, true)) * intensity
);
v = math.constrain(onlyNoise ? n : v + n, 0, 255);
d[i] = d[i + 1] = d[i + 2] = v;
}
return pixels;
}
Insert cell
grayscale = pixels => {
const d = pixels.data;
for (let i = 0; i < d.length; i += 4) {
const r = d[i];
const g = d[i + 1];
const b = d[i + 2];
// CIE luminance for the RGB
// The human eye is bad at seeing red and blue, so we de-emphasize them.
let v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
d[i] = d[i + 1] = d[i + 2] = v;
}
return pixels;
}
Insert cell
import { math, renderImage } from "@kewitz/utils"
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more