Completed
Push — master ( eb6f0f...0f89cf )
by Nicholas
05:27
created

regulate::max()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 6
1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
class regulate {
7
	
8
	public static function percent(float &$value) {
9
		$value = static::max(abs($value), 100);
10
	}
11
	
12
	public static function rgb(int &$value) {
13
		$value = static::max(abs($value), 256);
14
	}
15
	
16
	public static function rgb_array(array &$rgb_array) {
17
		static::standardize_array($rgb_array);
18
		$rgb_array += ['r' => 0, 'g' => 0, 'b' => 0];
19
		static::rgb($rgb_array['r']);
20
		static::rgb($rgb_array['g']);
21
		static::rgb($rgb_array['b']);
22
	}
23
	
24
	public static function hsl(float &$value, string $offset) {
25
		if (strtolower($offset) == 'h') {
26
			$value = static::max(abs($value), 359);
27
		}
28
		static::percent($value);
29
	}
30
	
31
	public static function hsl_array(array &$hsl_array) {
32
		static::standardize_array($hsl_array);
33
		$hsl_array += ['h' => 0, 's' => 0, 'l' => 0];
34
		static::hsl($hsl_array['h'], 'h');
35
		static::hsl($hsl_array['s'], 's');
36
		static::hsl($hsl_array['l'], 'l');
37
	}
38
	
39
	public static function cmyk(float &$value) {
40
		static::percent($value);
41
	}
42
	
43
	public static function cmyk_array(array &$cmyk_array) {
44
		static::standardize_array($cmyk_array);
45
		$hsl_array += ['c' => 0, 'm' => 0, 'y' => 0, 'k' => 0];
0 ignored issues
show
Bug introduced by
The variable $hsl_array does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
46
		static::cmyk($cmyk_array['c']);
47
		static::cmyk($cmyk_array['m']);
48
		static::cmyk($cmyk_array['y']);
49
		static::cmyk($cmyk_array['k']);
50
	}
51
	
52
	public static function hex(string &$color) {
53
		static::_validate_hex_str($color);
54
		static::_strip_hash($color);
55
		static::_expand_shorthand($color);
56
	}
57
	
58
	public static function hex_int(int &$value) {
59
		$value = static::max(abs($value), 0x1FFFFFF);
60
	}
61
	
62
	public static function _strip_hash(&$hex) {
63
		if (is_string($hex) && substr($hex, 0 ,1) == '#') {
64
			$hex = substr($hex, 1);
65
		}
66
	}
67
	
68
	public static function _expand_shorthand(string &$hex_str) {
69
		if (strlen($hex_str) === 3) {
70
			$r = $hex_str[0];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
			$g = $hex_str[1];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
			$b = $hex_str[2];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
			$hex_str = $r.$r.$g.$g.$b.$b;
74
		}
75
	}
76
	
77
	public static function _validate_hex_str(&$hex_str) {
78
		if (is_string($hex_str) && preg_match('/\A#?(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})\Z/i', $hex_str)) {
79
			return;
80
		}
81
		// Error - Force color and trigger notice
82
		$hex_str = '000000';
83
		// [todo] Trigger Error
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
84
	}
85
	
86
	protected static function standardize_array(array &$array) {
87
		$array = array_change_key_case($array);
88
	}
89
	
90
	protected static function max(float $number, float $max) :float {
91
		if ($number > $max) {
92
			$number -= floor($number / $max) * $max;
93
		}
94
		return $number;
95
	}
96
	
97
}
98