Completed
Push — master ( cf58ce...c14a81 )
by Nicholas
05:08
created

main::red()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 2
1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
7
class main implements \Serializable {
8
	
9
	public $color;
10
	public $hsl_result_accuracy = 0;
11
	
12
	public function __construct($color) {
13
		$this->set($color);
14
	}
15
	
16
	public function __clone() {
17
		$rgb = $this->color->rgb;
18
		unset($this->color);
19
		$this->set($rgb, 'rgb');
20
	}
21
	
22
	public function serialize() :string {
23
		return $this->color->serialize();
24
	}
25
	
26
	public function unserialize($serialized) {
27
		$unserialized = (array) json_decode((string) $serialized);
28
		regulate::rgb_array($unserialized);
29
		$this->set($unserialized, 'rgb');
30
	}
31
	
32
	public function set($color, string $type = '') {
33
		if ($color instanceof color) {
34
			$this->color = $color;
35
		} else {
36
			$this->color = new color($color, $type);
37
		}
38
	}
39
	
40
	public function rgb() :array {
41
		return (array) $this->color->rgb;
42
	}
43
	
44
	public function hsl() :array {
45
		$color = [];
46
		foreach((array) $this->color->hsl as $key => $value) {
47
			$color[$key] = round($value, abs($this->hsl_result_accuracy));
48
		}
49
		return $color;
50
	}
51
	
52
	public function cmyk() :array {
53
		$rgb = $this->color->rgb;
54
		return generate::rgb_to_cmyk($rgb['r'], $rgb['g'], $rgb['b']);
55
	}
56
	
57
	public function hex() :string {
58
		return $this->color->hex;
59
	}
60
	
61
	public function css() :string {
62
		return css::best($this->color);
63
	}
64
	
65
	public function is_dark(int $check_score = 128) :bool {
66
		$rgb = $this->color->rgb;
67
		return check::is_dark($rgb['r'], $rgb['g'], $rgb['b'], $check_score);
68
	}
69
	
70
	public function red(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
71
		return modify::rgb($this->color, 'red', $adjustment, $as_percentage, $set_absolute);
72
	}
73
	
74
	public function green(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
75
		return modify::rgb($this->color, 'green', $adjustment, $as_percentage, $set_absolute);
76
	}
77
	
78
	public function blue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
79
		return modify::rgb($this->color, 'blue', $adjustment, $as_percentage, $set_absolute);
80
	}
81
	
82
	public function hue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
83
		return modify::hsl($this->color, 'hue', $adjustment, $as_percentage, $set_absolute);
84
	}
85
	
86
	public function saturation(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
87
		return modify::hsl($this->color, 'saturation', $adjustment, $as_percentage, $set_absolute);
88
	}
89
	
90
	public function light(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
91
		return modify::hsl($this->color, 'light', $adjustment, $as_percentage, $set_absolute);
92
	}
93
	
94
	public function rgb_scheme(string $scheme_name = '') :array {
95
		return static::_convert_scheme(
96
			static::hsl_scheme($scheme_name),
97
			[new generate, 'hsl_to_rgb']
98
		);
99
	}
100
	
101
	public function hsl_scheme(string $scheme_name = '') :array {
102
		if (is_callable($callable = [new scheme, $scheme_name])) {
103
			$hsl = $this->color->hsl;
104
			return call_user_func($callable, $hsl['h'], $hsl['s'], $hsl['l']);
105
		}
106
		error::call(sprintf(
107
			'The $scheme_name "%s" is not a valid scheme name',
108
			$scheme_name,
109
			__CLASS__,
110
			__FUNCTION__
111
		));
112
		return [];
113
	}
114
	
115
	public function hex_scheme(string $scheme_name = '') :array {
116
		return static::_convert_scheme(
117
			static::rgb_scheme($scheme_name),
118
			[new generate, 'rgb_to_hex']
119
		);
120
	}
121
	
122
	public function cmyk_scheme(string $scheme_name = '') :array {
123
		return static::_convert_scheme(
124
			static::rgb_scheme($scheme_name),
125
			[new generate, 'rgb_to_cmyk']
126
		);
127
	}
128
	
129
	protected static function _convert_scheme(array $scheme, callable $callback) {
130
		$scheme = array_values($scheme);
131
		foreach ($scheme as &$color) {
132
			$color = call_user_func_array($callback, $color);
133
		}
134
		return $scheme;
135
	}
136
}
137