Passed
Push — master ( 58da02...5be15d )
by Nicholas
05:25
created

main::jsonSerialize()   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, \JsonSerializable {
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 + ['a' => $this->color->alpha()];
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 jsonSerialize() :array {
32
		return $this->color->jsonSerialize();
33
	}
34
	
35
	
36
	protected function set($color, string $type = '') {
37
		if ($color instanceof color) {
38
			$this->color = $color;
39
		} else {
40
			$this->color = new color($color, $type);
41
		}
42
	}
43
	
44
	public function rgb() :array {
45
		return (array) $this->color->rgb + ['a' => $this->color->alpha()];
46
	}
47
	
48
	public function hsl(int $accuracy = 0) :array {
49
		$color = [];
50
		foreach($this->color->hsl() as $key => $value) {
51
			$color[$key] = round($value, abs($accuracy));
52
		}
53
		return $color + ['a' => $this->color->alpha()];
54
	}
55
	
56
	public function cmyk() :array {
57
		$rgb = $this->color->rgb;
58
		return generate::rgb_to_cmyk($rgb['r'], $rgb['g'], $rgb['b']) + ['a' => $this->color->alpha()];
59
	}
60
	
61
	public function hex() :string {
62
		return $this->color->hex;
63
	}
64
	
65
	public function css() :string {
66
		return css::best($this->color);
67
	}
68
	
69
	/**
70
	 * Get (and set) the alpha channel
71
	 * 
72
	 * @param  mixed $new_alpha If numeric, the alpha channel is set to this value
73
	 * @return float            The current alpha value
74
	 */
75
	public function alpha($new_alpha) {
76
		return $this->color->alpha($new_alpha);
77
	}
78
	
79
	public function is_dark(int $check_score = 128) :bool {
80
		$rgb = $this->color->rgb;
81
		return check::is_dark($rgb['r'], $rgb['g'], $rgb['b'], $check_score);
82
	}
83
	
84
	public function red(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
85
		return modify::rgb($this->color, 'red', $adjustment, $as_percentage, $set_absolute);
86
	}
87
	
88
	public function green(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
89
		return modify::rgb($this->color, 'green', $adjustment, $as_percentage, $set_absolute);
90
	}
91
	
92
	public function blue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
93
		return modify::rgb($this->color, 'blue', $adjustment, $as_percentage, $set_absolute);
94
	}
95
	
96
	public function hue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
97
		return modify::hsl($this->color, 'hue', $adjustment, $as_percentage, $set_absolute);
98
	}
99
	
100
	public function saturation(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
101
		return modify::hsl($this->color, 'saturation', $adjustment, $as_percentage, $set_absolute);
102
	}
103
	
104
	public function light(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
105
		return modify::hsl($this->color, 'light', $adjustment, $as_percentage, $set_absolute);
106
	}
107
	
108
	public function rgb_scheme(string $scheme_name) :array {
109
		return static::_scheme($scheme_name, 'rgb', $this->hsl(3));
110
	}
111
	
112
	public function hsl_scheme(string $scheme_name) :array {
113
		return static::_scheme($scheme_name, 'hsl', $this->hsl(3));
114
	}
115
	
116
	public function hex_scheme(string $scheme_name) :array {
117
		return static::_scheme($scheme_name, 'hex', $this->hsl(3));
118
	}
119
	
120
	public function cmyk_scheme(string $scheme_name) :array {
121
		return static::_scheme($scheme_name, 'cmyk', $this->hsl(3));
122
	}
123
	
124
	protected static function _scheme(string $scheme_name, string $callback, array $hsl) {
125
		if (is_callable($callable = [new scheme, $callback])) {
126
			return call_user_func($callable, $hsl['h'], $hsl['s'], $hsl['l'], $scheme_name);
127
		}
128
		error::call(sprintf(
129
			'The $callback "%s" is not a valid callback',
130
			$scheme_name,
131
			__CLASS__,
132
			__FUNCTION__
133
		));
134
		return [];
135
	}
136
	
137
	
138
}
139