37 lines
2.0 KiB
JavaScript
37 lines
2.0 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'), assert = require('node:assert/strict');
|
|
const light = require('../utils/face-luma');
|
|
function pixels(width, height, value) { return new Int32Array(width * height * 3).fill(value); }
|
|
|
|
test('face brightness reports black, white and neutral levels on the H5 0-255 scale', () => {
|
|
for (const value of [0, 50, 128, 240, 255]) {
|
|
const actual = light.sample(pixels(32, 48, value), 32, 48, { x: 0, y: 0, width: 32, height: 48 });
|
|
assert(Math.abs(actual - value) < 1e-8);
|
|
}
|
|
});
|
|
|
|
test('bright backgrounds cannot hide a dark face and dark backgrounds cannot hide an overexposed face', () => {
|
|
for (const [background, face] of [[255, 40], [0, 245]]) {
|
|
const rgb = pixels(80, 80, background);
|
|
for (let y = 20; y < 60; y++) for (let x = 20; x < 60; x++) for (let channel = 0; channel < 3; channel++) rgb[(y * 80 + x) * 3 + channel] = face;
|
|
assert(Math.abs(light.sample(rgb, 80, 80, { x: 20, y: 20, width: 40, height: 40 }) - face) < 1e-8);
|
|
}
|
|
});
|
|
|
|
test('normalized RGB uses H5 luminance weights and safely clips fractional face boxes', () => {
|
|
for (const [channel, weight] of [[0, .299], [1, .587], [2, .114]]) {
|
|
const rgb = pixels(8, 8, 0); for (let i = channel; i < rgb.length; i += 3) rgb[i] = 255;
|
|
assert(Math.abs(light.sample(rgb, 8, 8, { x: -1.5, y: -.5, width: 12, height: 10 }) - weight * 255) < 1e-8);
|
|
}
|
|
});
|
|
|
|
test('unavailable, malformed and off-frame brightness inputs stay unknown instead of becoming dark readings', () => {
|
|
const rgb = pixels(8, 8, 128), box = { x: 0, y: 0, width: 8, height: 8 };
|
|
assert.equal(light.sample(null, 8, 8, box), null); assert.equal(light.sample(rgb, 8, 8, null), null);
|
|
assert.equal(light.sample(rgb, 8, 8, { ...box, x: 9 }), null);
|
|
assert.equal(light.sample(rgb, 8, 8, { ...box, width: -1 }), null);
|
|
assert.equal(light.sample(rgb, NaN, 8, box), null); assert.equal(light.sample(rgb, 8.2, 8, box), null);
|
|
assert.equal(light.sample(rgb.slice(0, 4), 8, 8, box), null);
|
|
rgb.fill(-1); assert.equal(light.sample(rgb, 8, 8, box), null);
|
|
});
|