Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Color | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| validHexColor | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| validColorName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| validRgbColor | |
100.00% |
3 / 3 |
|
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 | |
| 15 | declare(strict_types=1); |
| 16 | |
| 17 | namespace 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 | */ |
| 27 | class Color |
| 28 | { |
| 29 | /** |
| 30 | * Check if supplied color is a valid hex color |
| 31 | * |
| 32 | * @param string $color Color to check |
| 33 | */ |
| 34 | public static function validHexColor(string $color): bool |
| 35 | { |
| 36 | $expr = '#?(([a-fA-F0-9]){3}){1,2}'; |
| 37 | |
| 38 | return boolval(preg_match('/^' . $expr . '$/', $color)); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check if this is a valid color name |
| 43 | * |
| 44 | * @param string $color Color to check |
| 45 | */ |
| 46 | public static function validColorName(string $color): bool |
| 47 | { |
| 48 | $expr = '(?:aqua|black|blue|fuchsia|gray|green|lime|maroon|navy' . |
| 49 | '|olive|orange|purple|red|silver|teal|white|yellow)'; |
| 50 | |
| 51 | return boolval(preg_match('/^' . $expr . '$/', $color)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Check if this is a valid rgb color definition |
| 56 | * |
| 57 | * @param string $color Color to check |
| 58 | */ |
| 59 | public static function validRgbColor(string $color): bool |
| 60 | { |
| 61 | $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*, |
| 62 | \s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*, |
| 63 | \s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))| |
| 64 | (rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\))'; |
| 65 | |
| 66 | return boolval(preg_match('/^' . $expr . '$/x', $color)); |
| 67 | } |
| 68 | } |