144 grey levels 0..1 of the icon over a mid grey */ public static function thumb(string $id): array { $icon = Icons::image($id, 48); $bg = imagecreatetruecolor(48, 48); imagefill($bg, 0, 0, imagecolorallocate($bg, 128, 128, 128)); imagealphablending($bg, true); imagecopy($bg, $icon, 0, 0, 0, 0, 48, 48); $small = imagecreatetruecolor(12, 12); imagecopyresampled($small, $bg, 0, 0, 0, 0, 12, 12, 48, 48); $g = []; for ($y = 0; $y < 12; $y++) { for ($x = 0; $x < 12; $x++) { $c = imagecolorat($small, $x, $y); $g[] = (0.299 * (($c >> 16) & 255) + 0.587 * (($c >> 8) & 255) + 0.114 * ($c & 255)) / 255; } } return $g; } /** @param list $a @param list $b */ public static function distance(array $a, array $b): float { $d = 0.0; foreach ($a as $i => $v) { $d += abs($v - $b[$i]); } return $d / count($a); } private static function variants(string $a, string $b): bool { foreach (self::VARIANTS as $group) { if (in_array($a, $group, true) && in_array($b, $group, true)) { return true; } } return false; } public function testEveryItemHasAPictogramOfItsOwn(): void { $missing = array_values(array_filter(Items::all(), static fn (string $id) => !Icons::covers($id))); self::assertSame([], $missing, 'items drawn with the "?" tag'); } public function testTheVitalsHaveTheirPictograms(): void { foreach (['v_health', 'v_blood', 'v_temperature', 'v_hunger', 'v_thirst', 'v_fatigue', 'v_morale', 'v_weight', 'v_fire', 'v_shelter'] as $id) { self::assertTrue(Icons::covers($id), $id); } } public function testNoTwoItemsLookAlike(): void { $ids = Items::all(); $thumbs = []; foreach ($ids as $id) { $thumbs[$id] = self::thumb($id); } $close = []; foreach ($ids as $i => $a) { foreach (array_slice($ids, $i + 1) as $b) { $d = self::distance($thumbs[$a], $thumbs[$b]); $min = self::variants($a, $b) ? self::MIN_VARIANT_DISTANCE : self::MIN_DISTANCE; if ($d < $min) { $close[] = sprintf('%s ~ %s (%.3f)', $a, $b, $d); } } } self::assertSame([], $close, 'icons too much alike'); } public function testTheIconsAreDrawnAtEverySizeTheGameUses(): void { foreach ([18, 26, 36, 52, 96] as $px) { $im = Icons::image('knife_flint', $px); self::assertSame($px, imagesx($im)); // something is drawn, the corners stay transparent self::assertSame(127, (imagecolorat($im, 0, 0) >> 24) & 127); self::assertLessThan(127, (imagecolorat($im, intdiv($px, 2), intdiv($px, 2)) >> 24) & 127); } } }