Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractLayer
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 getColor
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 getHSBColor
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3/**
4 * Abstract cover layer
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2018.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Cover_Generator
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
28 */
29
30namespace VuFind\Cover\Layer;
31
32use function strlen;
33
34/**
35 * Abstract cover layer
36 *
37 * @category VuFind
38 * @package  Cover_Generator
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development:plugins:hierarchy_components Wiki
42 */
43abstract class AbstractLayer implements LayerInterface
44{
45    /**
46     * Mapping of color names to RGB values.
47     *
48     * @var array
49     */
50    protected $colorMap = [
51        'black' => [0, 0, 0],
52        'silver' => [192, 192, 192],
53        'gray' => [128, 128, 128],
54        'white' => [255, 255, 255],
55        'maroon' => [128, 0, 0],
56        'red' => [255, 0, 0],
57        'purple' => [128, 0, 128],
58        'fuchsia' => [255, 0, 255],
59        'green' => [0, 128, 0],
60        'lime' => [0, 255, 0],
61        'olive' => [128, 128, 0],
62        'yellow' => [255, 255, 0],
63        'navy' => [0, 0, 128],
64        'blue' => [0, 0, 255],
65        'teal' => [0, 128, 128],
66        'aqua' => [0, 255, 255],
67    ];
68
69    /**
70     * Check and allocates color
71     *
72     * @param resource $im    Image resource being updated
73     * @param string   $color Legal color name from HTML4
74     *
75     * @return int|false allocated color
76     */
77    protected function getColor($im, $color)
78    {
79        // Case one: named color found in map
80        $key = strtolower($color);
81        if (isset($this->colorMap[$key])) {
82            return imagecolorallocate($im, ...$this->colorMap[$key]);
83        }
84        // Case two: hex color
85        if (str_starts_with($color, '#') && strlen($color) == 7) {
86            $r = hexdec(substr($color, 1, 2));
87            $g = hexdec(substr($color, 3, 2));
88            $b = hexdec(substr($color, 5, 2));
89            return imagecolorallocate($im, $r, $g, $b);
90        }
91        // Default case: unsupported color
92        return false;
93    }
94
95    /**
96     * Using HSB allows us to control the contrast while allowing randomness
97     *
98     * @param resource $im Active image resource
99     * @param int      $h  Hue (0-255)
100     * @param int      $s  Saturation (0-255)
101     * @param int      $v  Lightness (0-255)
102     *
103     * @return int
104     */
105    protected function getHSBColor($im, $h, $s, $v)
106    {
107        $s /= 256.0;
108        if ($s == 0.0) {
109            return imagecolorallocate($im, $v, $v, $v);
110        }
111        $h /= (256.0 / 6.0);
112        $i = floor($h);
113        $f = $h - $i;
114        $p = (int)($v * (1.0 - $s));
115        $q = (int)($v * (1.0 - $s * $f));
116        $t = (int)($v * (1.0 - $s * (1.0 - $f)));
117        switch ($i) {
118            case 0:
119                return imagecolorallocate($im, $v, $t, $p);
120            case 1:
121                return imagecolorallocate($im, $q, $v, $p);
122            case 2:
123                return imagecolorallocate($im, $p, $v, $t);
124            case 3:
125                return imagecolorallocate($im, $p, $q, $v);
126            case 4:
127                return imagecolorallocate($im, $t, $p, $v);
128            default:
129                return imagecolorallocate($im, $v, $p, $q);
130        }
131    }
132}