Files
physics/tests/photometry.sakura
T
2026-10-30 15:05:57 -05:00

72 lines
3.9 KiB
Plaintext

#! Contains the tests of `photometry`: lumens and candela of lights every way and
#! within cones, lux and nits of surfaces, exposure values and what they
#! multiply light by, and the colors of temperatures.
import std::testing;
import std::math;
import malunal::linear;
import malunal::photometry;
# Whether two numbers are within so much of one another, as a fraction of the
# greater: a hundred-thousandth, at first.
def close(a: f32, b: f32, within: f32 = 1e-5): bool {
return math::abs(a - b) <= within * math::max(math::abs(a), math::abs(b));
}
@test def converts_lumens_and_candela(t: &mut tester): void {
val pi := math::pi as f32;
# A sphere's solid angle, a hemisphere's, and a cone of 60 degrees each way.
t.expects(close(solid_angle_of(pi), 4 * pi) && close(solid_angle_of(pi / 2), 2 * pi) && close(solid_angle_of(pi / 3), pi));
# A bare bulb's 800 lumens are about 63.7 candela every way; gathered into
# the cone of 60 degrees, about 255.
t.expects(close(candela_of_lumens(lumens::bulb_60w), 63.66198) && close(candela_of_lumens(800, pi / 3), 254.6479));
t.expects(close(lumens_of_candela(candela::candle), lumens::candle) && close(lumens_of_candela(candela_of_lumens(300, 0.2), 0.2), 300));
# An LED of 8 watts gives what a 60-watt incandescent bulb did.
t.expects(close(lumens_of_watts(8, efficacy::led), lumens::bulb_60w) && close(watts_of_lumens(lumens::bulb_60w, efficacy::incandescent), 57.14286));
}
@test def converts_lux_and_nits(t: &mut tester): void {
# A light of 100 candela gives a surface 2 meters away 25 lux, and gives 4
# lux 5 meters away.
t.expects(close(lux_of_candela(100, 2), 25) && close(distance_of_lux(100, 4), 5));
# A white surface lit by π lux gives back a nit; a grey one half as much.
t.expects(close(nits_of_lux(math::pi as f32), 1) && close(nits_of_lux(100, 0.5), 15.915494));
t.expects(close(lux_of_nits(nits_of_lux(lux::office, 0.18), 0.18), lux::office));
# A clear sky of 7,000 nits lights the ground with about 22,000 lux.
t.expects(close(lux_of_nits(nits::clear_sky), 21991.15));
}
@test def exposes_by_exposure_values(t: &mut tester): void {
# f/16 at a hundredth of a second, as the sunny 16 rule has it, at ISO 100,
# and a step less at ISO 200.
t.expects(close(ev100_of_camera(16, 0.01), 14.643856) && close(ev100_of_camera(16, 0.01, 200), 13.643856));
t.expects(close(ev100_of_lux(lux::direct_sunlight), 15.287712) && close(ev100_of_nits(12.5), 6.643856));
t.expects(close(exposure_of_ev100(0), 1.0 / 1.2) && close(exposure_of_ev100(1), 1.0 / 2.4));
t.expects(close(ev100_of_exposure(exposure_of_ev100(9.7)), 9.7) && close(ev100_of_exposure(1), -0.2630344));
# Exposed as a meter where it's lit reads, a white surface records as two
# thirds of the most, and an 18% grey card about an eighth: middle grey.
for light in [lux::full_moon, lux::office, lux::direct_sunlight] {
val exposure := exposure_of_ev100(ev100_of_lux(light));
t.expects(close(nits_of_lux(light) * exposure, 0.6631456, 1e-4) && close(nits_of_lux(light, 0.18) * exposure, 0.1193662, 1e-4));
}
}
@test def colors_temperatures(t: &mut tester): void {
# A warm bulb's is orange, daylight's about white, and a blue sky's blue.
val warm := color_of_temperature(2700);
t.expects(warm.x == 1 && close(warm.y, 0.4159, 1e-3) && close(warm.z, 0.0984, 1e-2));
val day := color_of_temperature(6500);
t.expects(day.x == 1 && day.y > 0.93 && day.z > 0.98);
val sky := color_of_temperature(10000);
t.expects(sky.z == 1 && sky.y > sky.x && close(sky.x, 0.6095, 1e-3));
# Hotter is bluer all the way.
var last := color_of_temperature(1000);
for k in 1..40u {
val next := color_of_temperature(1000 * (k + 1) as f32);
t.expects(next.z / next.x >= last.z / last.x);
last = next;
}
# Past either end, as either.
t.expects(color_of_temperature(500) == color_of_temperature(1000) && color_of_temperature(90000) == color_of_temperature(40000));
}