modify   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 0
cts 37
cp 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A rgb() 0 8 1
A hsl() 0 12 2
A _convert_to_exact() 0 6 2
A _convert_to_abs() 0 6 2
A regenerate_rgb() 0 10 2
A regenerate_hsl() 0 10 2
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) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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 = '') {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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 = '') {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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