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