Completed
Push — master ( 337dbc...dc36e9 )
by Nicholas
03:31
created

main   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 8
dl 0
loc 158
ccs 0
cts 84
cp 0
rs 9.6
c 0
b 0
f 0

29 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 jsonSerialize() 0 3 1
A set() 0 7 2
A rgb() 0 3 1
A hsl() 0 7 2
A cmyk() 0 4 1
A hsb() 0 4 1
A hex() 0 3 1
A css() 0 3 1
A alpha() 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 blend() 0 9 1
A rgb_scheme() 0 3 1
A hsl_scheme() 0 3 1
A hsb_scheme() 0 3 1
A hex_scheme() 0 3 1
A cmyk_scheme() 0 3 1
A _scheme() 0 12 2
A rgb_rand() 0 3 1
A hsl_rand() 0 3 1
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 hsb(int $accuracy = 0) :array {
62
		$rgb = $this->color->rgb;
63
		return generate::rgb_to_hsb($rgb['r'], $rgb['g'], $rgb['b'], $accuracy) + ['a' => $this->color->alpha()];
64
	}
65
	
66
	public function hex() :string {
67
		return $this->color->hex;
68
	}
69
	
70
	public function css() :string {
71
		return css::best($this->color);
72
	}
73
	
74
	/**
75
	 * Get (and set) the alpha channel
76
	 * 
77
	 * @param  mixed $new_alpha If numeric, the alpha channel is set to this value
78
	 * @return float            The current alpha value
79
	 */
80
	public function alpha($new_alpha) {
81
		return $this->color->alpha($new_alpha);
82
	}
83
	
84
	public function is_dark(int $check_score = 128) :bool {
85
		$rgb = $this->color->rgb;
86
		return check::is_dark($rgb['r'], $rgb['g'], $rgb['b'], $check_score);
87
	}
88
	
89
	public function red(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
90
		return modify::rgb($this->color, 'red', $adjustment, $as_percentage, $set_absolute);
91
	}
92
	
93
	public function green(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
94
		return modify::rgb($this->color, 'green', $adjustment, $as_percentage, $set_absolute);
95
	}
96
	
97
	public function blue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
98
		return modify::rgb($this->color, 'blue', $adjustment, $as_percentage, $set_absolute);
99
	}
100
	
101
	public function hue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
102
		return modify::hsl($this->color, 'hue', $adjustment, $as_percentage, $set_absolute);
103
	}
104
	
105
	public function saturation(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
106
		return modify::hsl($this->color, 'saturation', $adjustment, $as_percentage, $set_absolute);
107
	}
108
	
109
	public function light(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
110
		return modify::hsl($this->color, 'light', $adjustment, $as_percentage, $set_absolute);
111
	}
112
	
113
	public function blend(main $color2, float $amount = 50.0) {
114
		$c1 = $this->rgb();
115
		$c2 = $color2->rgb();
116
		return new $this(generate::blend(
117
			$c1['r'], $c1['g'], $c1['b'], $c1['a'],
118
			$c2['r'], $c2['g'], $c2['b'], $c2['a'],
119
			$amount)
120
		);
121
	}
122
	
123
	public function rgb_scheme(string $scheme_name) :array {
124
		return static::_scheme($scheme_name, 'rgb', $this->hsl(3));
125
	}
126
	
127
	public function hsl_scheme(string $scheme_name) :array {
128
		return static::_scheme($scheme_name, 'hsl', $this->hsl(3));
129
	}
130
	
131
	public function hsb_scheme(string $scheme_name) :array {
132
		return static::_scheme($scheme_name, 'hsb', $this->hsl(3));
133
	}
134
	
135
	public function hex_scheme(string $scheme_name) :array {
136
		return static::_scheme($scheme_name, 'hex', $this->hsl(3));
137
	}
138
	
139
	public function cmyk_scheme(string $scheme_name) :array {
140
		return static::_scheme($scheme_name, 'cmyk', $this->hsl(3));
141
	}
142
	
143
	protected static function _scheme(string $scheme_name, string $callback, array $hsl) {
144
		if (is_callable($callable = [new scheme, $callback])) {
145
			return call_user_func($callable, $hsl['h'], $hsl['s'], $hsl['l'], $scheme_name);
146
		}
147
		error::call(sprintf(
148
			'The $callback "%s" is not a valid callback',
149
			$scheme_name,
150
			__CLASS__,
151
			__FUNCTION__
152
		));
153
		return [];
154
	}
155
	
156
	public function rgb_rand(int $min_r = 0, int $max_r = 255, int $min_g = 0, int $max_g = 255, int $min_b = 0, int $max_b = 255) :color {
157
		return new color(generate::rgb_rand($min_r, $max_r, $min_g, $max_g, $min_b, $max_b));
0 ignored issues
show
Bug introduced by
The method rgb_rand() does not seem to exist on object<projectcleverweb\color\generate>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
	}
159
	
160
	public function hsl_rand(int $min_h = 0, int $max_h = 255, int $min_s = 0, int $max_s = 255, int $min_l = 0, int $max_l = 255) :color {
161
		return new color(generate::hsl_rand($min_h, $max_h, $min_s, $max_s, $min_l, $max_l));
0 ignored issues
show
Bug introduced by
The method hsl_rand() does not seem to exist on object<projectcleverweb\color\generate>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
	}
163
	
164
}
165