ES
apps/winzik / src / Gui / Palette.php · commit 6576d6f
<?phpnamespace App\Gui;use App\Form\SkinEditorForm;use JumPHP\Gui\Pointer;use JumPHP\Gui\Theme;use JumPHP\Gui\Widget;use JumPHP\Renderer\Renderer;/** The colours at hand: the 16 of Windows 98, the transparent one, and the colour of the brush (big, on the left). */final class Palette extends Widget{    public const DEFAULTS = ['width' => 'fill', 'height' => 30];    public const COLORS = ['#000000', '#808080', '#800000', '#808000', '#008000', '#008080', '#000080', '#800080',        '#ffffff', '#c0c0c0', '#ff0000', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#ff00ff', ''];    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        return [$maxWidth, $t->px(30)];    }    private function cell(): float    {        return min($this->h, ($this->w - $this->h * 2) / count(self::COLORS));    }    public function render(Renderer $r, Theme $t): void    {        $editor = $this->form instanceof SkinEditorForm ? $this->form : null;        $this->swatch($r, $this->x, $this->y, $this->h * 1.6, $this->h, $editor === null ? '#000000' : $editor->color);        $c = $this->cell();        foreach (self::COLORS as $i => $color) {            $this->swatch($r, $this->x + $this->h * 2 + $i * $c, $this->y + ($this->h - $c) / 2, $c - 2, $c - 2, $color);        }    }    private function swatch(Renderer $r, float $x, float $y, float $w, float $h, string $color): void    {        if ($color === '') {            $r->fillRect($x, $y, $w, $h, '#ffffff');            $r->fillRect($x, $y, $w / 2, $h / 2, '#a0a0a0');            $r->fillRect($x + $w / 2, $y + $h / 2, $w / 2, $h / 2, '#a0a0a0');        } else {            $r->fillRect($x, $y, $w, $h, $color);        }        $r->drawRect($x, $y, $w, $h, '#000000');    }    protected function click(Pointer $p): void    {        $i = (int)floor(($p->x - $this->x - $this->h * 2) / max(1, $this->cell()));        if ($i >= 0 && $i < count(self::COLORS) && $this->form instanceof SkinEditorForm) {            $this->form->setColor(self::COLORS[$i]);        }    }}