Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Color
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 validHexColor
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validColorName
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validRgbColor
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Karla ImageMagick wrapper library
5 *
6 * PHP Version 8.0<
7 *
8 * @category Utility
9 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
10 * @license  http://www.opensource.org/licenses/mit-license.php MIT
11 * @link     https://github.com/localgod/karla Karla
12 * @since    2013-05-26
13 */
14
15declare(strict_types=1);
16
17namespace Karla;
18
19/**
20 * Helper class for color operations
21 *
22 * @category Utility
23 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
24 * @license  http://www.opensource.org/licenses/mit-license.php MIT
25 * @link     https://github.com/localgod/karla Karla
26 */
27class Color
28{
29    /**
30     * Check if supplied color is a valid hex color
31     *
32     * @param string $color
33     *            Color to check
34     *
35     * @return boolean
36     */
37    public static function validHexColor(string $color): bool
38    {
39        $expr = '#?(([a-fA-F0-9]){3}){1,2}';
40
41        return boolval(preg_match('/^' . $expr . '$/', $color));
42    }
43
44    /**
45     * Check if this is a valid color name
46     *
47     * @param string $color
48     *            Color to check
49     *
50     * @return boolean
51     */
52    public static function validColorName(string $color): bool
53    {
54        $expr = '(aqua)|(black)|(blue)|(fuchsia)|(gray)|(green)|(lime)|(maroon)|(navy)|
55                (olive)|(orange)|(purple)|(red)|(silver)|(teal)|(white)|(yellow)';
56
57        return boolval(preg_match('/^' . $expr . '$/', $color));
58    }
59
60    /**
61     * Check if this is a valid rgb color definition
62     *
63     * @param string $color
64     *            Color to check
65     *
66     * @return boolean
67     */
68    public static function validRgbColor(string $color): bool
69    {
70        $expr = '(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,
71                \s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,
72                \s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|
73                (rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))';
74
75        return boolval(preg_match('/^' . $expr . '$/x', $color));
76    }
77}