Skip to content

Instantly share code, notes, and snippets.

@andrevenancio
Created July 5, 2016 01:39
Show Gist options
  • Save andrevenancio/499018845edc85808f03a65a1cb98b9d to your computer and use it in GitHub Desktop.
Save andrevenancio/499018845edc85808f03a65a1cb98b9d to your computer and use it in GitHub Desktop.
apply a threshold filter with a fixed constant. (ES6)
threshold(pixels, threshold) {
const d = pixels.data;
for (let i = 0; i < d.length; i += 4) {
const r = d[i + 0];
const g = d[i + 1];
const b = d[i + 2];
let v = 0;
if ((r + g + b) > threshold) {
v = 255;
}
d[i + 0] = d[i + 1] = d[i + 2] = v;
}
return pixels;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment