1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Conversion Class |
4
|
|
|
* ================ |
5
|
|
|
* Responsible for all the conversion method between Hex, RGB, HSL, HSB, and CMYK |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace projectcleverweb\color\traits; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Conversion Class |
12
|
|
|
* ================ |
13
|
|
|
* Responsible for all the conversion method between Hex, RGB, HSL, HSB, and CMYK |
14
|
|
|
*/ |
15
|
|
|
trait converter { |
16
|
|
|
|
17
|
|
|
protected static function error($message) { |
18
|
|
|
return \projectcleverweb\color\error::trigger(0, $message); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected static function _validate_array_input($input) { |
|
|
|
|
22
|
|
|
if (!is_array($input)) { |
23
|
|
|
static::error(sprintf( |
24
|
|
|
'%s input must be of type "array", input was of type "%s"', |
25
|
|
|
strtoupper(implode('', static::$valid_keys)), |
26
|
|
|
gettype($input) |
27
|
|
|
)); |
28
|
|
|
$input = static::$default_value; |
29
|
|
|
} |
30
|
|
|
$input = array_change_key_case($input); |
31
|
|
|
$array = static::$default_value; |
32
|
|
|
foreach (static::$valid_keys as $key) { |
33
|
|
|
if (!isset($input[$key])) { |
34
|
|
|
static::error(sprintf( |
35
|
|
|
'Key "%s" missing from %s array', |
36
|
|
|
$key, |
37
|
|
|
strtoupper(implode('', static::$valid_keys)) |
38
|
|
|
)); |
39
|
|
|
$input[$key] = $array[$key]; |
40
|
|
|
} |
41
|
|
|
$array[$key] = (float) filter_var($input[$key], FILTER_VALIDATE_FLOAT); |
42
|
|
|
} |
43
|
|
|
return $array; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function _validate_hex_input($input) { |
|
|
|
|
47
|
|
|
if (is_int($input)) { |
48
|
|
|
return str_pad(dechex($input % 16777216), 6, '0', STR_PAD_LEFT); |
49
|
|
|
} elseif (is_string($input)) { |
50
|
|
|
return static::_validate_hex_input_str($input); |
51
|
|
|
} |
52
|
|
|
static::error(sprintf( |
53
|
|
|
'hex input must be of type "string" or "integer", input was of type "%s"', |
54
|
|
|
gettype($input) |
55
|
|
|
)); |
56
|
|
|
return static::$default_value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected static function _validate_hex_input_str(string $input) :string { |
60
|
|
|
$input = strtolower($input); |
61
|
|
|
if ($input[0] == '#') { |
62
|
|
|
$input = substr($input, 1); |
63
|
|
|
} |
64
|
|
|
if (!preg_match('([0-9a-f]{6}|[0-9a-f]{3})', $input)) { |
65
|
|
|
static::error('invalid hex string input'); |
66
|
|
|
return static::$default_value; |
67
|
|
|
} |
68
|
|
|
return static::_expand_hex_str($input); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected static function _expand_hex_str(string $input) : string { |
72
|
|
|
if (strlen($input) == 3) { |
73
|
|
|
$input = $input[0].$input[0].$input[1].$input[1].$input[2].$input[2]; |
74
|
|
|
} |
75
|
|
|
return $input; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Color delta algorithm |
80
|
|
|
* |
81
|
|
|
* @param float $rgb The R, G, or B value |
82
|
|
|
* @param float $max The max RGB value |
83
|
|
|
* @param float $delta The delta value ($max - $min) |
84
|
|
|
* @return float The color delta |
85
|
|
|
*/ |
86
|
|
|
protected static function _rgbhsl_delta_rgb(float $rgb, float $max, float $delta) { |
87
|
|
|
return ((($max - $rgb) / 6) + ($delta / 2)) / $delta; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Calculate the hue as a percentage from RGB |
92
|
|
|
* |
93
|
|
|
* @param float &$h The variable to modify as hue |
94
|
|
|
* @param float $r The red value as a percentage |
95
|
|
|
* @param float $g The green value as a percentage |
96
|
|
|
* @param float $b The blue value as a percentage |
97
|
|
|
* @param float $max The max RGB value |
98
|
|
|
* @param float $delta The delta value ($max - $min) |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
protected static function _rgbhsl_hue(float &$h, float $r, float $g, float $b, float $max, float $delta) { |
102
|
|
|
$delta_r = static::_rgbhsl_delta_rgb($r, $max, $delta); |
103
|
|
|
$delta_g = static::_rgbhsl_delta_rgb($g, $max, $delta); |
104
|
|
|
$delta_b = static::_rgbhsl_delta_rgb($b, $max, $delta); |
105
|
|
|
|
106
|
|
|
$h = (2 / 3) + $delta_g - $delta_r; |
107
|
|
|
if ($r == $max) { |
108
|
|
|
$h = $delta_b - $delta_g; |
109
|
|
|
} elseif ($g == $max) { |
110
|
|
|
$h = (1 / 3) + $delta_r - $delta_b; |
111
|
|
|
} |
112
|
|
|
if ($h < 0) { |
113
|
|
|
$h++; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Handle low hue values |
119
|
|
|
* |
120
|
|
|
* @param float &$r The red value to modify |
121
|
|
|
* @param float &$g The green value to modify |
122
|
|
|
* @param float &$b The blue value to modify |
123
|
|
|
* @param float $c Potential R, G, or B value |
124
|
|
|
* @param float $x Potential R, G, or B value |
125
|
|
|
* @param float $h The hue |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
protected static function _hslrgb_low(float &$r, float &$g, float &$b, float $c, float $x, float $h) { |
129
|
|
|
if ($h < 60) { |
130
|
|
|
$r = $c; |
131
|
|
|
$g = $x; |
132
|
|
|
$b = 0; |
133
|
|
|
} elseif ($h < 120) { |
134
|
|
|
$r = $x; |
135
|
|
|
$g = $c; |
136
|
|
|
$b = 0; |
137
|
|
|
} else { |
138
|
|
|
$r = 0; |
139
|
|
|
$g = $c; |
140
|
|
|
$b = $x; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Handle high hue values |
146
|
|
|
* |
147
|
|
|
* @param float &$r The red value to modify |
148
|
|
|
* @param float &$g The green value to modify |
149
|
|
|
* @param float &$b The blue value to modify |
150
|
|
|
* @param float $c Potential R, G, or B value |
151
|
|
|
* @param float $x Potential R, G, or B value |
152
|
|
|
* @param float $h The hue |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
protected static function _hslrgb_high(float &$r, float &$g, float &$b, float $c, float $x, float $h) { |
156
|
|
|
if ($h < 240) { |
157
|
|
|
$r = 0; |
158
|
|
|
$g = $x; |
159
|
|
|
$b = $c; |
160
|
|
|
} else { |
161
|
|
|
$r = $x; |
162
|
|
|
$g = 0; |
163
|
|
|
$b = $c; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.