1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Color Modifier Class |
4
|
|
|
* ==================== |
5
|
|
|
* Allows you to modify the various aspects of a 'color' instance |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace projectcleverweb\color; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Color Modifier Class |
12
|
|
|
* ==================== |
13
|
|
|
* Allows you to modify the various aspects of a 'color' instance |
14
|
|
|
*/ |
15
|
|
|
class modify { |
16
|
|
|
|
17
|
|
|
public static function rgb(color $color, string $scope, float $adjustment, bool $as_percentage, bool $set_absolute) { |
|
|
|
|
18
|
|
|
$current = array_combine(['red', 'green', 'blue'], $color->rgb); |
19
|
|
|
$scope = strtolower($scope); |
20
|
|
|
$adjustment = max(min($adjustment, 255), 0 - 255); // Force valid range |
21
|
|
|
$adjustment = static::_convert_to_exact($adjustment, 255, $as_percentage); |
22
|
|
|
$current[$scope] = static::_convert_to_abs($current[$scope], $adjustment, $set_absolute, 0, 255); |
23
|
|
|
return static::regenerate_rgb($color, $current, $scope); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function hsl(color $color, string $scope, float $adjustment, bool $as_percentage, bool $set_absolute) { |
|
|
|
|
27
|
|
|
$current = array_combine(['hue', 'saturation', 'light'], $color->hsl()); |
28
|
|
|
$scope = strtolower($scope); |
29
|
|
|
$max = 100; |
30
|
|
|
if ($scope == 'hue') { |
31
|
|
|
$max = 359; |
32
|
|
|
} |
33
|
|
|
$adjustment = max(min($adjustment, $max), 0 - $max); // Force valid range |
34
|
|
|
$adjustment = static::_convert_to_exact($adjustment, $max, $as_percentage); |
35
|
|
|
$current[$scope] = static::_convert_to_abs($current[$scope], $adjustment, $set_absolute, 0, $max); |
36
|
|
|
return static::regenerate_hsl($color, $current, $scope); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected static function _convert_to_exact(float $adjustment, float $max, bool $as_percentage) { |
40
|
|
|
if ($as_percentage) { |
41
|
|
|
return ($adjustment / 100) * abs($max); |
42
|
|
|
} |
43
|
|
|
return $adjustment; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected static function _convert_to_abs(float $current, float $adjustment, bool $set_absolute, float $min = 0, float $max = PHP_INT_MAX) { |
47
|
|
|
if ($set_absolute) { |
48
|
|
|
return abs($adjustment); |
49
|
|
|
} |
50
|
|
|
return max(min($current + $adjustment, $max), $min); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected static function regenerate_rgb(color $color, array $update, string $return_offset = '') { |
|
|
|
|
54
|
|
|
$color->import_rgb([ |
55
|
|
|
'r' => $update['red'], |
56
|
|
|
'g' => $update['green'], |
57
|
|
|
'b' => $update['blue'] |
58
|
|
|
]); |
59
|
|
|
if (isset($update[$return_offset])) { |
60
|
|
|
return $update[$return_offset]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected static function regenerate_hsl(color $color, array $update, string $return_offset = '') { |
|
|
|
|
65
|
|
|
$color->import_hsl([ |
66
|
|
|
'h' => $update['hue'], |
67
|
|
|
's' => $update['saturation'], |
68
|
|
|
'l' => $update['light'] |
69
|
|
|
]); |
70
|
|
|
if (isset($update[$return_offset])) { |
71
|
|
|
return $update[$return_offset]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
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.