IT
apps/winzik / src / Model / Skin.php · commit 6576d6f
<?phpnamespace App\Model;/** * A skin of WinZik: a folder of pictures (main, mainpl, mainp, the buttons...) and an INFO.txt: * *   [INF] *   Skin Chaine-Hifi            the name shown in the skin viewer (Form9) *   &H0000FFFF&                 the scrolling title (Text4)            then the volume, balance, kbps labels, *   ...                         the hours / minutes / seconds, the six bars of the bargraph (green, orange, red; *                               left / right), the colour of the line playing, then the 23 lines of the play list * * Colours are VB's &H00BBGGRR& (or a decimal number): turned into '#rrggbb'. */final class Skin{    /** the colours of INFO.txt, in their order */    public const COLORS = ['title', 'volume', 'balance', 'kbps', 'hours', 'minutes', 'seconds',        'barGreenLeft', 'barGreenRight', 'barOrangeLeft', 'barOrangeRight', 'barRedLeft', 'barRedRight', 'active'];    /** @var array<string, string> */    public array $colors = [        'title' => '#ffff00', 'volume' => '#ffff00', 'balance' => '#ffff00', 'kbps' => '#ffff00',        'hours' => '#00ff00', 'minutes' => '#00ff00', 'seconds' => '#00ff00',        'barGreenLeft' => '#00ff00', 'barGreenRight' => '#00ff00', 'barOrangeLeft' => '#ff8000',        'barOrangeRight' => '#ff8000', 'barRedLeft' => '#ff0000', 'barRedRight' => '#ff0000', 'active' => '#ffffff',    ];    /** @var list<string> the colours of the 23 lines of the play list */    public array $lineColors = [];    /** @var array<string, string> picture name (lower case, no extension) => file */    private array $files = [];    public function __construct(public string $dir, public string $name = '')    {        $this->lineColors = array_fill(0, 23, '#8080ff');        foreach (is_dir($dir) ? (scandir($dir) ?: []) : [] as $file) {            $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));            if (in_array($ext, ['png', 'gif', 'bmp', 'jpg'], true)) {                $key = strtolower(pathinfo($file, PATHINFO_FILENAME));                // a PNG wins over the GIF / BMP of the same name (the converted pictures keep the transparency)                if (!isset($this->files[$key]) || $ext === 'png') {                    $this->files[$key] = rtrim($dir, '/') . '/' . $file;                }            }        }        $info = self::findInfo($dir);        if ($info !== null) {            $this->readInfo((string)file_get_contents($info));        }        if ($this->name === '') {            $this->name = basename($dir);        }        // VIS.txt (WinZik 2026): "x y w h" of the visualizer, in the opened part of the player's picture        $vis = rtrim($dir, '/') . '/VIS.txt';        if (is_file($vis) && preg_match('/^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/', (string)file_get_contents($vis), $m)) {            $this->vis = [(int)$m[1], (int)$m[2], (int)$m[3], (int)$m[4]];        }    }    /** @var array{0: int, 1: int, 2: int, 3: int}|null where the visualizer goes (x, y, w, h), null: the skin has none */    public ?array $vis = null;    /** @var array<string, string>|null */    private ?array $palette = null;    /**     * The colours of the skin's own pictures, for the windows 2001 did not have (the equalizer): its case (the play     * list's head), its list's background, its buttons' face, a text that reads on that face and one on the case.     * @return array{case: string, list: string, face: string, ink: string, caseInk: string}     */    public function palette(): array    {        if ($this->palette !== null) {            return $this->palette;        }        $pick = function (string $name, int $x, int $y, string $default): string {            $file = $this->image($name);            if ($file === null || !function_exists('imagecreatefromstring')) {                return $default;            }            $im = @imagecreatefromstring((string)@file_get_contents($file));            if ($im === false || $x >= imagesx($im) || $y >= imagesy($im)) {                return $default;            }            $c = imagecolorsforindex($im, imagecolorat($im, $x, $y));            return sprintf('#%02x%02x%02x', $c['red'], $c['green'], $c['blue']);        };        $luma = fn (string $hex): float => 0.299 * hexdec(substr($hex, 1, 2)) + 0.587 * hexdec(substr($hex, 3, 2)) + 0.114 * hexdec(substr($hex, 5, 2));        $case = $pick('mainpl', 40, 24, '#2a2a40');        $list = $pick('mainpl', 60, 200, '#000000');        $face = $pick('plp', 4, 8, '#c0c0c0');        return $this->palette = ['case' => $case, 'list' => $list, 'face' => $face,            'ink' => $luma($face) > 128 ? '#101018' : '#f0f0f8', 'caseInk' => $luma($case) > 128 ? '#202030' : '#e0e0f0'];    }    /** A folder is a skin when it has an INFO.txt (any case) and a main picture. */    public static function isSkin(string $dir): bool    {        if (self::findInfo($dir) === null) {            return false;        }        foreach (['main.png', 'main.bmp', 'MAIN.BMP', 'Main.bmp'] as $main) {            if (is_file($dir . '/' . $main)) {                return true;            }        }        return false;    }    private static function findInfo(string $dir): ?string    {        foreach (['INFO.txt', 'info.txt', 'INFO.TXT', 'Info.txt'] as $name) {            if (is_file($dir . '/' . $name)) {                return $dir . '/' . $name;            }        }        return null;    }    public function readInfo(string $content): void    {        $lines = array_map(fn (string $l) => Text::unquote($l), Text::lines($content));        if (($lines[0] ?? '') !== '[INF]') {            return;    // "votre skin a peut-etre un probleme": the pictures are still used, with the default colours        }        $this->name = trim($lines[1] ?? '') ?: $this->name;        foreach (self::COLORS as $i => $key) {            $color = self::vbColor($lines[2 + $i] ?? '');            if ($color !== null) {                $this->colors[$key] = $color;            }        }        for ($i = 0; $i < 23; $i++) {            $color = self::vbColor($lines[2 + count(self::COLORS) + $i] ?? '');            if ($color !== null) {                $this->lineColors[$i] = $color;            }        }    }    /** &H00BBGGRR& (or 16744576) -> '#rrggbb'; null when it is not a colour. */    public static function vbColor(string $value): ?string    {        $value = trim($value);        if (preg_match('/^&H([0-9A-Fa-f]{1,8})&?$/', $value, $m)) {            $n = hexdec($m[1]);        } elseif (preg_match('/^[0-9]+$/', $value)) {            $n = (int)$value;        } else {            return null;        }        $n = (int)fmod((float)$n, 16777216.0);        return sprintf('#%02x%02x%02x', $n & 0xFF, ($n >> 8) & 0xFF, ($n >> 16) & 0xFF);    }    /** The file of a picture of the skin ('main', 'bplay'...), null when the skin has none. */    public function image(string $name): ?string    {        return $this->files[strtolower($name)] ?? null;    }    /** The colour of a line of the play list (1..23 on screen, like tablo(i)). */    public function lineColor(int $row): string    {        return $this->lineColors[max(0, min(22, $row))] ?? '#8080ff';    }    /**     * The skins found in these folders (each sub-folder with an INFO.txt), sorted by name.     *     * @param list<string> $roots     * @return list<Skin>     */    public static function findAll(array $roots): array    {        $skins = [];        foreach ($roots as $root) {            foreach (is_dir($root) ? (scandir($root) ?: []) : [] as $entry) {                $dir = rtrim($root, '/') . '/' . $entry;                if ($entry[0] !== '.' && is_dir($dir) && self::isSkin($dir)) {                    $skins[] = new self($dir);                }            }        }        usort($skins, fn (Skin $a, Skin $b) => strcasecmp($a->name, $b->name));        return $skins;    }}