Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Color
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 colorize
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getFgColorCode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 getBgColorCode
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Console;
16
17/**
18 * Console color class
19 *
20 * @category   Pop
21 * @package    Pop\Console
22 * @author     Nick Sagona, III <dev@noladev.com>
23 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27class Color
28{
29
30    /**
31     * Color indices
32     */
33    const NORMAL              = 0;
34    const BLACK               = 1;
35    const RED                 = 2;
36    const GREEN               = 3;
37    const YELLOW              = 4;
38    const BLUE                = 5;
39    const MAGENTA             = 6;
40    const CYAN                = 7;
41    const WHITE               = 8;
42    const BRIGHT_BLACK        = 9;
43    const BRIGHT_RED          = 10;
44    const BRIGHT_GREEN        = 11;
45    const BRIGHT_YELLOW       = 12;
46    const BRIGHT_BLUE         = 13;
47    const BRIGHT_MAGENTA      = 14;
48    const BRIGHT_CYAN         = 15;
49    const BRIGHT_WHITE        = 16;
50    CONST BOLD_BLACK          = 17;
51    CONST BOLD_RED            = 18;
52    CONST BOLD_GREEN          = 19;
53    CONST BOLD_YELLOW         = 20;
54    CONST BOLD_BLUE           = 21;
55    CONST BOLD_MAGENTA        = 22;
56    CONST BOLD_CYAN           = 23;
57    CONST BOLD_WHITE          = 24;
58    CONST BRIGHT_BOLD_BLACK   = 25;
59    CONST BRIGHT_BOLD_RED     = 26;
60    CONST BRIGHT_BOLD_GREEN   = 27;
61    CONST BRIGHT_BOLD_YELLOW  = 28;
62    CONST BRIGHT_BOLD_BLUE    = 29;
63    CONST BRIGHT_BOLD_MAGENTA = 30;
64    CONST BRIGHT_BOLD_CYAN    = 31;
65    CONST BRIGHT_BOLD_WHITE   = 32;
66
67    /**
68     * Color map of foreground ansi values
69     *
70     * @var array
71     */
72    protected static array $fgColorMap = [
73        self::NORMAL              => '22;39',
74        self::BLACK               => '0;30',
75        self::RED                 => '0;31',
76        self::GREEN               => '0;32',
77        self::YELLOW              => '0;33',
78        self::BLUE                => '0;34',
79        self::MAGENTA             => '0;35',
80        self::CYAN                => '0;36',
81        self::WHITE               => '0;37',
82        self::BRIGHT_BLACK        => '0;90',
83        self::BRIGHT_RED          => '0;91',
84        self::BRIGHT_GREEN        => '0;92',
85        self::BRIGHT_YELLOW       => '0;93',
86        self::BRIGHT_BLUE         => '0;94',
87        self::BRIGHT_MAGENTA      => '0;95',
88        self::BRIGHT_CYAN         => '0;96',
89        self::BRIGHT_WHITE        => '0;97',
90        self::BOLD_BLACK          => '1;30',
91        self::BOLD_RED            => '1;31',
92        self::BOLD_GREEN          => '1;32',
93        self::BOLD_YELLOW         => '1;33',
94        self::BOLD_BLUE           => '1;34',
95        self::BOLD_MAGENTA        => '1;35',
96        self::BOLD_CYAN           => '1;36',
97        self::BOLD_WHITE          => '1;37',
98        self::BRIGHT_BOLD_BLACK   => '1;90',
99        self::BRIGHT_BOLD_RED     => '1;91',
100        self::BRIGHT_BOLD_GREEN   => '1;92',
101        self::BRIGHT_BOLD_YELLOW  => '1;93',
102        self::BRIGHT_BOLD_BLUE    => '1;94',
103        self::BRIGHT_BOLD_MAGENTA => '1;95',
104        self::BRIGHT_BOLD_CYAN    => '1;96',
105        self::BRIGHT_BOLD_WHITE   => '1;97',
106    ];
107
108    /**
109     * Color map of background color ansi values
110     *
111     * @var array
112     */
113    protected static array $bgColorMap = [
114        self::NORMAL         => '0;49',
115        self::BLACK          => '40',
116        self::RED            => '41',
117        self::GREEN          => '42',
118        self::YELLOW         => '43',
119        self::BLUE           => '44',
120        self::MAGENTA        => '45',
121        self::CYAN           => '46',
122        self::WHITE          => '47',
123        self::BRIGHT_BLACK   => '100',
124        self::BRIGHT_RED     => '101',
125        self::BRIGHT_GREEN   => '102',
126        self::BRIGHT_YELLOW  => '103',
127        self::BRIGHT_BLUE    => '104',
128        self::BRIGHT_MAGENTA => '105',
129        self::BRIGHT_CYAN    => '106',
130        self::BRIGHT_WHITE   => '107'
131    ];
132
133    /**
134     * Colorize a string for output
135     *
136     * @param  string $string
137     * @param  ?int   $fg
138     * @param  ?int   $bg
139     * @param  bool   $raw
140     * @return string
141     */
142    public static function colorize(string $string, ?int $fg = null, ?int $bg = null, bool $raw = false): string
143    {
144        if ((stripos(PHP_OS, 'win') === false) && (!$raw)) {
145            return static::getFgColorCode($fg) . static::getBgColorCode($bg) . $string . "\x1b[0m";
146        } else {
147            return $string;
148        }
149    }
150
151    /**
152     * Get the foreground color code from the color map
153     *
154     * @param  ?int $color
155     * @return mixed
156     */
157    public static function getFgColorCode(?int $color = null): mixed
158    {
159        if (($color !== null) && isset(static::$fgColorMap[$color])) {
160            return "\x1b[" . static::$fgColorMap[$color] . "m";
161        }
162        return null;
163    }
164
165    /**
166     * Get the background color code from the color map
167     *
168     * @param  ?int $color
169     * @return mixed
170     */
171    public static function getBgColorCode(?int $color = null): mixed
172    {
173        if (($color !== null) && isset(static::$bgColorMap[$color])) {
174            return "\x1b[" . static::$bgColorMap[$color] . "m";
175        }
176        return null;
177    }
178
179}