|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Console\Output; |
| 13 | + |
| 14 | +use Symfony\Component\Asset\Exception\InvalidArgumentException; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Fabien Potencier <[email protected]> |
| 18 | + * @author Julien Boudry <[email protected]> |
| 19 | + */ |
| 20 | +enum AnsiColorMode |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Classical 4-bit Ansi colors, including 8 classical colors and 8 bright color. Output syntax is "ESC[${foreGroundColorcode};${backGroundColorcode}m" |
| 24 | + * Must be compatible with all terminals and it's the minimal version supported. |
| 25 | + */ |
| 26 | + case Ansi4; |
| 27 | + |
| 28 | + /** |
| 29 | + * 8-bit Ansi colors (240 differents colors + 16 duplicate color codes, ensuring backward compatibility). |
| 30 | + * Output syntax is: "ESC[38;5;${foreGroundColorcode};48;5;${backGroundColorcode}m" |
| 31 | + * Should be compatible with most terminals. |
| 32 | + */ |
| 33 | + case Ansi8; |
| 34 | + |
| 35 | + /** |
| 36 | + * 24-bit Ansi colors (RGB). |
| 37 | + * Output syntax is: "ESC[38;2;${foreGroundColorcodeRed};${foreGroundColorcodeGreen};${foreGroundColorcodeBlue};48;2;${backGroundColorcodeRed};${backGroundColorcodeGreen};${backGroundColorcodeBlue}m" |
| 38 | + * May be compatible with many modern terminals. |
| 39 | + */ |
| 40 | + case Ansi24; |
| 41 | + |
| 42 | + /** |
| 43 | + * Converts an RGB hexadecimal color to the corresponding Ansi code. |
| 44 | + */ |
| 45 | + public function convertFromHexToAnsiColorCode(string $hexColor): string |
| 46 | + { |
| 47 | + $hexColor = str_replace('#', '', $hexColor); |
| 48 | + |
| 49 | + if (3 === \strlen($hexColor)) { |
| 50 | + $hexColor = $hexColor[0].$hexColor[0].$hexColor[1].$hexColor[1].$hexColor[2].$hexColor[2]; |
| 51 | + } |
| 52 | + |
| 53 | + if (6 !== \strlen($hexColor)) { |
| 54 | + throw new InvalidArgumentException(sprintf('Invalid "#%s" color.', $hexColor)); |
| 55 | + } |
| 56 | + |
| 57 | + $color = hexdec($hexColor); |
| 58 | + |
| 59 | + $r = ($color >> 16) & 255; |
| 60 | + $g = ($color >> 8) & 255; |
| 61 | + $b = $color & 255; |
| 62 | + |
| 63 | + return match ($this) { |
| 64 | + self::Ansi4 => (string) $this->convertFromRGB($r, $g, $b), |
| 65 | + self::Ansi8 => '8;5;'.((string) $this->convertFromRGB($r, $g, $b)), |
| 66 | + self::Ansi24 => sprintf('8;2;%d;%d;%d', $r, $g, $b) |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private function convertFromRGB(int $r, int $g, int $b): int |
| 71 | + { |
| 72 | + return match ($this) { |
| 73 | + self::Ansi4 => $this->degradeHexColorToAnsi4($r, $g, $b), |
| 74 | + self::Ansi8 => $this->degradeHexColorToAnsi8($r, $g, $b), |
| 75 | + default => throw new InvalidArgumentException("RGB cannot be converted to {$this->name}.") |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + private function degradeHexColorToAnsi4(int $r, int $g, int $b): int |
| 80 | + { |
| 81 | + if (0 === round($this->getSaturation($r, $g, $b) / 50)) { |
| 82 | + return 0; |
| 83 | + } |
| 84 | + |
| 85 | + return (int) ((round($b / 255) << 2) | (round($g / 255) << 1) | round($r / 255)); |
| 86 | + } |
| 87 | + |
| 88 | + private function getSaturation(int $r, int $g, int $b): int |
| 89 | + { |
| 90 | + $r = $r / 255; |
| 91 | + $g = $g / 255; |
| 92 | + $b = $b / 255; |
| 93 | + $v = max($r, $g, $b); |
| 94 | + |
| 95 | + if (0 === $diff = $v - min($r, $g, $b)) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + |
| 99 | + return (int) ((int) $diff * 100 / $v); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Inspired from https://github.com/ajalt/colormath/blob/e464e0da1b014976736cf97250063248fc77b8e7/colormath/src/commonMain/kotlin/com/github/ajalt/colormath/model/Ansi256.kt code (MIT license). |
| 104 | + */ |
| 105 | + private function degradeHexColorToAnsi8(int $r, int $g, int $b): int |
| 106 | + { |
| 107 | + if ($r === $g && $g === $b) { |
| 108 | + if ($r < 8) { |
| 109 | + return 16; |
| 110 | + } |
| 111 | + |
| 112 | + if ($r > 248) { |
| 113 | + return 231; |
| 114 | + } |
| 115 | + |
| 116 | + return (int) round(($r - 8) / 247 * 24) + 232; |
| 117 | + } else { |
| 118 | + return 16 + |
| 119 | + (36 * (int) round($r / 255 * 5)) + |
| 120 | + (6 * (int) round($g / 255 * 5)) + |
| 121 | + (int) round($b / 255 * 5); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments