<?phpnamespace Tests;use App\Revisited\Sim\Items;use App\Revisited\Ui\Icons;use PHPUnit\Framework\TestCase;/** * The item pictograms: every item has its own, and no two look alike (a 12 x 12 grey thumbnail of each, compared * cell by cell). States of one thing (the water of a gourd, an arrow's tip, a broken rifle) are drawn on the same * picture on purpose: they only have to differ by their badge or colour. */final class RevisitedIconsTest extends TestCase{ /** Below this mean grey difference (0..1) two icons read as the same picture. */ public const MIN_DISTANCE = 0.03; /** States of one thing: only their badge or colour differs, but it must still show. */ public const MIN_VARIANT_DISTANCE = 0.01; public const VARIANTS = [ ['gourd_empty', 'gourd_dirty', 'gourd_filtered', 'gourd_boiled', 'gourd_purified', 'gourd_clean'], ['arrow', 'arrow_bone', 'arrow_metal'], ['pulse_rifle', 'pulse_rifle_broken'], ['blueprint_crossbow', 'blueprint_forge', 'blueprint_pulse'], ['hide', 'fur'], ['gloves_pilot', 'gloves_hide'], ['trousers_hide', 'leggings_fur'], ['boots', 'pilot_boots_worn'], ['meat_raw', 'meat_cooked'], ['fish_raw', 'fish_cooked'], ['smoked_meat', 'smoked_fish'], ['jacket_hide', 'jacket_scale'], ]; /** @return list<float> 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<float> $a @param list<float> $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); } }}