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

main::css()   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 0
crap 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