Completed
Push — master ( fe23d9...b94c89 )
by Nicholas
02:52
created

regulate::percent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
class regulate {
7
	
8
	/**
9
	 * Forces a float to be within the range of 0 and 100.
10
	 * 
11
	 * @param  float &$value The value to modify
12
	 * @return void
13
	 */
14 2
	public static function percent(float &$value) {
15 2
		$value = static::max(abs($value), 100);
16 2
	}
17
	
18
	/**
19
	 * Forces a float to be within the range of 0 and 255.
20
	 * 
21
	 * @param  float &$value The value to modify
22
	 * @return void
23
	 */
24 2
	public static function rgb(int &$value) {
25 2
		$value = static::max(abs($value), 255);
26 2
	}
27
	
28
	/**
29
	 * Forces and RGB array to have specific offsets, and max values.
30
	 * 
31
	 * @param  array &$rgb_array The RGB array to modify
32
	 * @return void
33
	 */
34 1
	public static function rgb_array(array &$rgb_array) {
35 1
		static::standardize_array($rgb_array, ['r', 'g', 'b']);
36 1
		static::rgb($rgb_array['r']);
37 1
		static::rgb($rgb_array['g']);
38 1
		static::rgb($rgb_array['b']);
39 1
	}
40
	
41
	/**
42
	 * Forces a float to be within the range of 0 and 100 or if the offset is
43
	 * 'h' 0 and 359.
44
	 * 
45
	 * @param  float &$value  The value to modify
46
	 * @param  string $offset The offset that is being modified
47
	 * @return void
48
	 */
49 1
	public static function hsl(float &$value, string $offset) {
50 1
		if (strtolower($offset) == 'h') {
51 1
			$value = static::max(abs($value), 359);
52
		} else {
53 1
			static::percent($value);
54
		}
55 1
	}
56
	
57
	/**
58
	 * Forces and HSL array to have specific offsets, and max values.
59
	 * 
60
	 * @param  array &$hsl_array The HSL array to modify
61
	 * @return void
62
	 */
63
	public static function hsl_array(array &$hsl_array) {
64
		static::standardize_array($hsl_array, ['h', 's', 'l']);
65
		static::hsl($hsl_array['h'], 'h');
66
		static::hsl($hsl_array['s'], 's');
67
		static::hsl($hsl_array['l'], 'l');
68
	}
69
	
70
	/**
71
	 * Forces a float to be within the range of 0 and 100.
72
	 * 
73
	 * @param  float &$value The value to modify
74
	 * @return void
75
	 */
76
	public static function cmyk(float &$value) {
77
		static::percent($value);
78
	}
79
	
80
	
81
	/**
82
	 * Forces a float to be within the range of 0 and 100.
83
	 * 
84
	 * @param  float &$value The value to modify
85
	 * @return void
86
	 */
87
	public static function alpha(float &$value) {
88
		static::percent($value);
89
	}
90
	
91
	/**
92
	 * Forces and CMYK array to have specific offsets, and max values.
93
	 * 
94
	 * @param  array &$cmyk_array The CMYK array to modify
95
	 * @return void
96
	 */
97
	public static function cmyk_array(array &$cmyk_array) {
98
		static::standardize_array($cmyk_array, ['c', 'm', 'y', 'k']);
99
		static::cmyk($cmyk_array['c']);
100
		static::cmyk($cmyk_array['m']);
101
		static::cmyk($cmyk_array['y']);
102
		static::cmyk($cmyk_array['k']);
103
	}
104
	
105
	/**
106
	 * Forces hexadecimal strings to be valid, without a "#", and not to be
107
	 * shorthand.
108
	 * 
109
	 * @param  string &$color The color string to modify
110
	 * @return void
111
	 */
112
	public static function hex(string &$color) {
113
		static::_validate_hex_str($color);
114
		static::_strip_hash($color);
115
		static::_expand_shorthand($color);
116
	}
117
	
118
	/**
119
	 * Forces hexadecimal integers to be within the range of 0x0 and 0xFFFFFF.
120
	 * 
121
	 * @param  int  &$value The value to modify
122
	 * @return void
123
	 */
124
	public static function hex_int(int &$value) {
125
		$value = static::max(abs($value), 0xFFFFFF);
126
	}
127
	
128
	/**
129
	 * Strips the hash mark (#) off the beginning of a sting if it has one.
130
	 * 
131
	 * @param  string &$hex The hex string
132
	 * @return void
133
	 */
134
	public static function _strip_hash(string &$hex) {
135
		if (is_string($hex) && substr($hex, 0 ,1) == '#') {
136
			$hex = substr($hex, 1);
137
		}
138
	}
139
	
140
	/**
141
	 * Expand a hex strings in short notation (e.g. 000 => 000000)
142
	 * 
143
	 * @param  string &$hex_str The hex string to modify
144
	 * @return void
145
	 */
146
	public static function _expand_shorthand(string &$hex_str) {
147
		if (strlen($hex_str) === 3) {
148
			$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...
149
			$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...
150
			$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...
151
			$hex_str = $r.$r.$g.$g.$b.$b;
152
		}
153
	}
154
	
155
	/**
156
	 * Verify that the hex string is actually a hex string. If not force it to
157
	 * '000000'
158
	 * 
159
	 * @param  string &$hex_str The hex string to modify
160
	 * @return void
161
	 */
162
	public static function _validate_hex_str(string &$hex_str) {
163
		if (is_string($hex_str) && preg_match('/\A#?(?:[0-9a-f]{3}|[0-9a-f]{6})\Z/i', $hex_str)) {
164
			return;
165
		}
166
		error::call(sprintf(
167
			'The input of %s::%s() was not a valid hex string, forcing value to 000000',
168
			__CLASS__,
169
			__FUNCTION__
170
		));
171
		$hex_str = '000000';
172
	}
173
	
174
	/**
175
	 * Force an array to have specific lower-case keys. If the array is an indexed
176
	 * array and the keys are provided, convert it to an associative array.
177
	 * 
178
	 * @param  array &$array The array to be modified
179
	 * @return void
180
	 */
181 1
	protected static function standardize_array(array &$array, array $keys = []) {
182 1
		if (!empty($keys) && array_keys($array) === range(0, count($array) - 1) && count($array) == count($keys)) {
183
			$array = array_combine($keys, $array);
184
		} else {
185 1
			$array = array_change_key_case($array) + array_fill_keys($keys, 0);
186
		}
187 1
	}
188
	
189
	/**
190
	 * Absolute and divide any number so it fits between 0 and $max
191
	 * 
192
	 * @param  float  $number The original number
193
	 * @param  float  $max    The maximum value $number should be
194
	 * @return float          The resulting number as a float
195
	 */
196 4
	protected static function max(float $number, float $max) :float {
197 4
		$max = abs($max);
198 4
		if ($number > $max) {
199 4
			$number -= floor($number / ($max + 1)) * ($max + 1);
200
		}
201 4
		return $number;
202
	}
203
	
204
	/**
205
	 * Simple dividing method to handle division by 0
206
	 * 
207
	 * @param  float  $number  The number to divide
208
	 * @param  float  $divisor The number to divide by
209
	 * @return float           The result
210
	 */
211 1
	public static function div(float $number, float $divisor) :float {
212 1
		if ($divisor == 0) {
213 1
			return 0;
214
		}
215 1
		return $number / $divisor;
216
	}
217
}
218