const WHOLE: i128 = 1 << 30;
const LIGHT_LEVELS: i128 = 1_023;
pub(super) const PIECE_STEPS: usize = 255;
const HALF_STEP_LIGHT: [u32; 511] = [
0,
1_186,
5_451,
13_302,
25_048,
40_924,
61_119,
85_795,
115_092,
149_135,
188_038,
231_905,
280_830,
334_904,
394_210,
458_824,
528_822,
604_273,
685_244,
771_798,
863_995,
961_896,
1_065_554,
1_175_024,
1_290_358,
1_411_606,
1_538_817,
1_672_037,
1_811_312,
1_956_686,
2_108_202,
2_265_902,
2_429_827,
2_600_017,
2_776_509,
2_959_342,
3_148_553,
3_344_177,
3_546_250,
3_754_807,
3_969_881,
4_191_505,
4_419_712,
4_654_533,
4_896_000,
5_144_143,
5_398_992,
5_660_578,
5_928_928,
6_204_072,
6_486_038,
6_774_853,
7_070_544,
7_373_139,
7_682_662,
7_999_142,
8_322_602,
8_653_069,
8_990_567,
9_335_120,
9_686_753,
10_045_490,
10_411_354,
10_784_368,
11_164_555,
11_551_938,
11_946_540,
12_348_382,
12_757_486,
13_173_873,
13_597_566,
14_028_584,
14_466_950,
14_912_683,
15_365_803,
15_826_332,
16_294_288,
16_769_692,
17_252_562,
17_742_920,
18_240_782,
18_746_169,
19_259_099,
19_779_590,
20_307_661,
20_843_331,
21_386_616,
21_937_536,
22_496_107,
23_062_347,
23_636_273,
24_217_904,
24_807_255,
25_404_343,
26_009_186,
26_621_800,
27_242_201,
27_870_406,
28_506_431,
29_150_292,
29_802_005,
30_461_586,
31_129_050,
31_804_412,
32_487_690,
33_178_897,
33_878_049,
34_585_160,
35_300_247,
36_023_323,
36_754_404,
37_493_505,
38_240_638,
38_995_820,
39_759_064,
40_530_385,
41_309_797,
42_097_313,
42_892_948,
43_696_716,
44_508_630,
45_328_703,
46_156_951,
46_993_385,
47_838_019,
48_690_867,
49_551_942,
50_421_257,
51_298_825,
52_184_658,
53_078_771,
53_981_175,
54_891_883,
55_810_909,
56_738_263,
57_673_960,
58_618_011,
59_570_429,
60_531_226,
61_500_415,
62_478_006,
63_464_013,
64_458_447,
65_461_321,
66_472_646,
67_492_434,
68_520_696,
69_557_445,
70_602_691,
71_656_447,
72_718_724,
73_789_534,
74_868_887,
75_956_795,
77_053_269,
78_158_320,
79_271_960,
80_394_200,
81_525_050,
82_664_521,
83_812_625,
84_969_372,
86_134_773,
87_308_839,
88_491_580,
89_683_007,
90_883_130,
92_091_961,
93_309_509,
94_535_785,
95_770_799,
97_014_562,
98_267_083,
99_528_374,
100_798_444,
102_077_304,
103_364_963,
104_661_431,
105_966_719,
107_280_836,
108_603_793,
109_935_599,
111_276_264,
112_625_798,
113_984_210,
115_351_510,
116_727_708,
118_112_814,
119_506_837,
120_909_786,
122_321_671,
123_742_501,
125_172_287,
126_611_036,
128_058_759,
129_515_465,
130_981_162,
132_455_861,
133_939_570,
135_432_299,
136_934_056,
138_444_851,
139_964_693,
141_493_590,
143_031_552,
144_578_587,
146_134_705,
147_699_914,
149_274_223,
150_857_641,
152_450_176,
154_051_838,
155_662_635,
157_282_575,
158_911_667,
160_549_920,
162_197_343,
163_853_943,
165_519_730,
167_194_711,
168_878_896,
170_572_292,
172_274_908,
173_986_752,
175_707_833,
177_438_159,
179_177_738,
180_926_578,
182_684_687,
184_452_074,
186_228_747,
188_014_714,
189_809_983,
191_614_561,
193_428_458,
195_251_680,
197_084_236,
198_926_135,
200_777_382,
202_637_988,
204_507_959,
206_387_303,
208_276_028,
210_174_143,
212_081_653,
213_998_568,
215_924_896,
217_860_642,
219_805_816,
221_760_425,
223_724_477,
225_697_979,
227_680_938,
229_673_363,
231_675_260,
233_686_637,
235_707_502,
237_737_863,
239_777_725,
241_827_098,
243_885_987,
245_954_401,
248_032_347,
250_119_833,
252_216_864,
254_323_449,
256_439_595,
258_565_310,
260_700_599,
262_845_471,
264_999_933,
267_163_991,
269_337_653,
271_520_926,
273_713_817,
275_916_333,
278_128_480,
280_350_267,
282_581_700,
284_822_786,
287_073_531,
289_333_943,
291_604_029,
293_883_796,
296_173_250,
298_472_398,
300_781_247,
303_099_804,
305_428_075,
307_766_068,
310_113_789,
312_471_245,
314_838_443,
317_215_388,
319_602_089,
321_998_551,
324_404_782,
326_820_787,
329_246_573,
331_682_148,
334_127_517,
336_582_687,
339_047_664,
341_522_456,
344_007_069,
346_501_508,
349_005_781,
351_519_894,
354_043_853,
356_577_665,
359_121_337,
361_674_874,
364_238_282,
366_811_569,
369_394_741,
371_987_804,
374_590_763,
377_203_626,
379_826_399,
382_459_088,
385_101_699,
387_754_239,
390_416_713,
393_089_127,
395_771_489,
398_463_804,
401_166_078,
403_878_317,
406_600_528,
409_332_717,
412_074_889,
414_827_051,
417_589_208,
420_361_368,
423_143_535,
425_935_715,
428_737_916,
431_550_143,
434_372_401,
437_204_697,
440_047_036,
442_899_425,
445_761_869,
448_634_375,
451_516_948,
454_409_594,
457_312_318,
460_225_128,
463_148_028,
466_081_024,
469_024_123,
471_977_330,
474_940_650,
477_914_090,
480_897_655,
483_891_351,
486_895_183,
489_909_158,
492_933_281,
495_967_558,
499_011_994,
502_066_595,
505_131_367,
508_206_315,
511_291_445,
514_386_763,
517_492_274,
520_607_984,
523_733_898,
526_870_021,
530_016_360,
533_172_920,
536_339_707,
539_516_725,
542_703_981,
545_901_480,
549_109_227,
552_327_228,
555_555_488,
558_794_013,
562_042_808,
565_301_879,
568_571_231,
571_850_869,
575_140_799,
578_441_026,
581_751_555,
585_072_392,
588_403_543,
591_745_011,
595_096_804,
598_458_926,
601_831_382,
605_214_178,
608_607_319,
612_010_810,
615_424_656,
618_848_863,
622_283_436,
625_728_380,
629_183_701,
632_649_403,
636_125_491,
639_611_972,
643_108_850,
646_616_129,
650_133_817,
653_661_916,
657_200_434,
660_749_374,
664_308_742,
667_878_542,
671_458_781,
675_049_463,
678_650_593,
682_262_176,
685_884_218,
689_516_723,
693_159_696,
696_813_142,
700_477_067,
704_151_475,
707_836_371,
711_531_761,
715_237_649,
718_954_040,
722_680_940,
726_418_352,
730_166_283,
733_924_736,
737_693_717,
741_473_232,
745_263_283,
749_063_878,
752_875_020,
756_696_714,
760_528_965,
764_371_778,
768_225_158,
772_089_110,
775_963_638,
779_848_748,
783_744_444,
787_650_731,
791_567_613,
795_495_096,
799_433_184,
803_381_882,
807_341_195,
811_311_128,
815_291_685,
819_282_870,
823_284_690,
827_297_148,
831_320_249,
835_353_997,
839_398_399,
843_453_457,
847_519_177,
851_595_564,
855_682_622,
859_780_356,
863_888_770,
868_007_870,
872_137_659,
876_278_142,
880_429_325,
884_591_210,
888_763_804,
892_947_111,
897_141_135,
901_345_881,
905_561_353,
909_787_556,
914_024_494,
918_272_173,
922_530_596,
926_799_768,
931_079_694,
935_370_378,
939_671_824,
943_984_037,
948_307_022,
952_640_784,
956_985_325,
961_340_652,
965_706_768,
970_083_677,
974_471_385,
978_869_896,
983_279_214,
987_699_344,
992_130_289,
996_572_055,
1_001_024_646,
1_005_488_065,
1_009_962_319,
1_014_447_410,
1_018_943_344,
1_023_450_124,
1_027_967_755,
1_032_496_242,
1_037_035_588,
1_041_585_798,
1_046_146_876,
1_050_718_828,
1_055_301_656,
1_059_895_365,
1_064_499_960,
1_069_115_445,
1_073_741_824,
];
const SHARE: [u32; 256] = [
0,
967_781,
1_966_160,
2_995_848,
4_057_569,
5_152_047,
6_280_017,
7_442_218,
8_639_393,
9_872_292,
11_141_669,
12_448_280,
13_792_888,
15_176_257,
16_599_154,
18_062_348,
19_566_611,
21_112_715,
22_701_432,
24_333_537,
26_009_800,
27_730_993,
29_497_885,
31_311_245,
33_171_836,
35_080_417,
37_037_747,
39_044_575,
41_101_648,
43_209_704,
45_369_477,
47_581_689,
49_847_057,
52_166_287,
54_540_076,
56_969_110,
59_454_063,
61_995_596,
64_594_359,
67_250_986,
69_966_097,
72_740_298,
75_574_178,
78_468_306,
81_423_239,
84_439_510,
87_517_635,
90_658_110,
93_861_410,
97_127_987,
100_458_273,
103_852_673,
107_311_573,
110_835_329,
114_424_274,
118_078_717,
121_798_935,
125_585_182,
129_437_681,
133_356_628,
137_342_187,
141_394_494,
145_513_652,
149_699_734,
153_952_782,
158_272_804,
162_659_773,
167_113_633,
171_634_290,
176_221_617,
180_875_452,
185_595_599,
190_381_823,
195_233_856,
200_151_394,
205_134_093,
210_181_575,
215_293_425,
220_469_190,
225_708_379,
231_010_465,
236_374_884,
241_801_032,
247_288_270,
252_835_919,
258_443_266,
264_109_559,
269_834_008,
275_615_787,
281_454_034,
287_347_851,
293_296_301,
299_298_415,
305_353_186,
311_459_574,
317_616_505,
323_822_868,
330_077_521,
336_379_291,
342_726_968,
349_119_316,
355_555_064,
362_032_913,
368_551_534,
375_109_570,
381_705_636,
388_338_321,
395_006_187,
401_707_772,
408_441_588,
415_206_126,
421_999_855,
428_821_222,
435_668_654,
442_540_560,
449_435_331,
456_351_340,
463_286_946,
470_240_495,
477_210_316,
484_194_730,
491_192_045,
498_200_560,
505_218_566,
512_244_347,
519_276_181,
526_312_342,
533_351_100,
540_390_724,
547_429_482,
554_465_643,
561_497_477,
568_523_258,
575_541_264,
582_549_779,
589_547_094,
596_531_508,
603_501_329,
610_454_878,
617_390_484,
624_306_493,
631_201_264,
638_073_170,
644_920_602,
651_741_969,
658_535_698,
665_300_236,
672_034_052,
678_735_637,
685_403_503,
692_036_188,
698_632_254,
705_190_290,
711_708_911,
718_186_760,
724_622_508,
731_014_856,
737_362_533,
743_664_303,
749_918_956,
756_125_319,
762_282_250,
768_388_638,
774_443_409,
780_445_523,
786_393_973,
792_287_790,
798_126_037,
803_907_816,
809_632_265,
815_298_558,
820_905_905,
826_453_554,
831_940_792,
837_366_940,
842_731_359,
848_033_445,
853_272_634,
858_448_399,
863_560_249,
868_607_731,
873_590_430,
878_507_968,
883_360_001,
888_146_225,
892_866_372,
897_520_207,
902_107_534,
906_628_191,
911_082_051,
915_469_020,
919_789_042,
924_042_090,
928_228_172,
932_347_330,
936_399_637,
940_385_196,
944_304_143,
948_156_642,
951_942_889,
955_663_107,
959_317_550,
962_906_495,
966_430_251,
969_889_151,
973_283_551,
976_613_837,
979_880_414,
983_083_714,
986_224_189,
989_302_314,
992_318_585,
995_273_518,
998_167_646,
1_001_001_526,
1_003_775_727,
1_006_490_838,
1_009_147_465,
1_011_746_228,
1_014_287_761,
1_016_772_714,
1_019_201_748,
1_021_575_537,
1_023_894_767,
1_026_160_135,
1_028_372_347,
1_030_532_120,
1_032_640_176,
1_034_697_249,
1_036_704_077,
1_038_661_407,
1_040_569_988,
1_042_430_579,
1_044_243_939,
1_046_010_831,
1_047_732_024,
1_049_408_287,
1_051_040_392,
1_052_629_109,
1_054_175_213,
1_055_679_476,
1_057_142_670,
1_058_565_567,
1_059_948_936,
1_061_293_544,
1_062_600_155,
1_063_869_532,
1_065_102_431,
1_066_299_606,
1_067_461_807,
1_068_589_777,
1_069_684_255,
1_070_745_976,
1_071_775_664,
1_072_774_043,
1_073_741_824,
];
fn light(value: u8) -> i128 {
HALF_STEP_LIGHT
.get(2 * usize::from(value))
.map_or(WHOLE, |&light| i128::from(light))
}
pub(super) fn channel(from: u8, to: u8, step: usize) -> u8 {
let share = SHARE.get(step).map_or(WHOLE, |&share| i128::from(share));
let (low, high) = (light(from), light(to));
let mixed = low * WHOLE + share * (high - low);
let level = (2 * mixed * LIGHT_LEVELS + WHOLE * WHOLE) / (2 * WHOLE * WHOLE);
let nearest = (1..=u8::MAX)
.take_while(|&value| {
let below = HALF_STEP_LIGHT
.get(2 * usize::from(value) - 1)
.map_or(WHOLE, |&light| i128::from(light));
below * LIGHT_LEVELS <= level * WHOLE
})
.count();
u8::try_from(nearest).unwrap_or(u8::MAX)
}
#[cfg(test)]
#[allow(clippy::float_arithmetic)]
mod tests {
use super::{HALF_STEP_LIGHT, PIECE_STEPS, SHARE, WHOLE, channel};
fn erf(x: f64) -> f64 {
let (mut term, mut sum) = (x, x);
for n in 1..60 {
let n = f64::from(n);
term *= -x * x / n;
sum += term / (2.0 * n + 1.0);
}
sum * 2.0 / std::f64::consts::PI.sqrt()
}
fn cumulative(x: f64) -> f64 {
1.0_f64.midpoint(erf(x / std::f64::consts::SQRT_2))
}
#[test]
fn holds_the_light_of_each_half_step_and_the_share_of_each_place() {
let whole = 2f64.powi(30);
for (j, &light) in HALF_STEP_LIGHT.iter().enumerate() {
let want = (f64::from(u32::try_from(j).unwrap()) / 510.0).powf(2.2) * whole;
assert!((f64::from(light) - want).abs() <= 1.0, "half step {j}");
}
let low = cumulative(-2.0);
for (k, &share) in SHARE.iter().enumerate() {
let place = f64::from(u32::try_from(k).unwrap()) / 255.0;
let want = (cumulative((place - 0.5) * 4.0) - low) / (1.0 - 2.0 * low) * whole;
assert!((f64::from(share) - want).abs() <= 1.0, "place {k}");
}
assert_eq!(SHARE.len(), PIECE_STEPS + 1);
assert_eq!(SHARE.last().map(|&s| i128::from(s)), Some(WHOLE));
}
#[test]
fn mixes_the_channels_as_word_does() {
let cases = [
(255, 0, 15, 0xFD),
(0, 255, 15, 0x28),
(255, 0, 128, 0xBA),
(0, 255, 128, 0xBB),
(0, 255, 240, 0xFD),
(0x31, 0x13, 255, 0x12),
(0x31, 0x13, 0, 0x31),
(255, 0, 254, 0x0B),
(255, 0, 255, 0),
(0, 255, 128, 0xBB),
];
for (from, to, step, want) in cases {
assert_eq!(channel(from, to, step), want, "{from} to {to} at {step}");
}
}
}