1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace projectcleverweb\color; |
5
|
|
|
|
6
|
|
|
class modify { |
7
|
|
|
|
8
|
|
|
public static function rgb(data $data, string $scope, float $adjustment, bool $as_percentage, bool $set_absolute) { |
9
|
|
|
$current = array_combine(['red', 'green', 'blue'], $data->rgb); |
|
|
|
|
10
|
|
|
$scope = strtolower($scope); // [todo] Validate scope |
|
|
|
|
11
|
|
|
$adjustment = max(min($adjustment, 255), 0 - 255); // Force valid range |
12
|
|
|
$adjustment = static::_convert_to_exact($adjustment, 255, $as_percentage); |
13
|
|
|
$current[$scope] = static::_convert_to_abs($current[$scope], $adjustment, $set_absolute, 0, 255); |
14
|
|
|
return static::regenerate_rgb($data, $current); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function hsl(data $data, string $scope, float $adjustment, bool $as_percentage, bool $set_absolute) { |
|
|
|
|
18
|
|
|
$current = array_combine(['hue', 'saturation', 'light'], $data->hsl->hsl); |
|
|
|
|
19
|
|
|
$scope = strtolower($scope); // [todo] Validate scope |
|
|
|
|
20
|
|
|
$max = 100; |
21
|
|
|
if ($scope == 'hue') { |
22
|
|
|
$max = 359; |
23
|
|
|
} |
24
|
|
|
$adjustment = max(min($adjustment, $max), 0 - $max); // Force valid range |
|
|
|
|
25
|
|
|
$adjustment = static::_convert_to_exact($adjustment, $max, $as_percentage); |
|
|
|
|
26
|
|
|
$current[$scope] = static::_convert_to_abs($current[$scope], $adjustment, $set_absolute, 0, $max); |
27
|
|
|
return static::regenerate_hsl($data, $current); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected static function _convert_to_exact(float $adjustment, float $max, bool $as_percentage) { |
31
|
|
|
if ($as_percentage) { |
32
|
|
|
return ($adjustment / 100) * abs($max); |
33
|
|
|
} |
34
|
|
|
return $adjustment; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected static function _convert_to_abs(float $current, float $adjustment, bool $set_absolute, float $min = 0, float $max = PHP_INT_MAX) { |
38
|
|
|
if ($set_absolute) { |
39
|
|
|
return abs($adjustment); |
40
|
|
|
} |
41
|
|
|
return max(min($current + $adjustment, $max), $min); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected static function regenerate_rgb(data $data, array $update) { |
45
|
|
|
$data->import_rgb([ |
46
|
|
|
'r' => $update['red'], |
47
|
|
|
'g' => $update['green'], |
48
|
|
|
'b' => $update['blue'] |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.