Completed
Push — master ( 0ee2e7...b858c3 )
by Nicholas
06:23
created

main   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 8
dl 0
loc 116
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0

20 Methods

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