const width = 1280;
const height = 720;
const targetR = 128;
const targetG = 128;
const targetB = 128;
const targetSize = 2;
const data = new Uint8ClampedArray(width * height * 4);
for (let i = 0; i < data.length; i++) {
data[i] = ~~(Math.random() * 256);
}
let count = 0;
for (let i = 0; i < data.length; i += 4) {
if (
targetR - targetSize <= data[i] && data[i] <= targetR + targetSize &&
targetG - targetSize <= data[i + 1] && data[i + 1] <= targetG + targetSize &&
targetB - targetSize <= data[i + 2] && data[i + 2] <= targetB + targetSize
) {
count++;
}
}
const hitArray = new Uint8Array(1 << 24);
const rMin = Math.max(0, targetR - targetSize);
const rMax = Math.min(255, targetR + targetSize);
const gMin = Math.max(0, targetG - targetSize);
const gMax = Math.min(255, targetG + targetSize);
const bMin = Math.max(0, targetB - targetSize);
const bMax = Math.min(255, targetB + targetSize);
for (r = rMin; r <= rMax; r++) {
for (g = gMin; g <= gMax; g++) {
for (b = bMin; b <= bMax; b++) {
hitArray[r | (g << 8) | (b << 16)] = 1;
}
}
}
let count = 0;
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
for (let i = 0; i < data.length; i += 4) {
count += hitArray[dataView.getUint32(i, true) & 0xffffff];
}