Completed
Push — master ( 619401...8cfcd7 )
by Nicholas
03:08
created

main::hsb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 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 rgb_scheme(string $scheme_name) :array {
114
		return static::_scheme($scheme_name, 'rgb', $this->hsl(3));
115
	}
116
	
117
	public function hsl_scheme(string $scheme_name) :array {
118
		return static::_scheme($scheme_name, 'hsl', $this->hsl(3));
119
	}
120
	
121
	public function hex_scheme(string $scheme_name) :array {
122
		return static::_scheme($scheme_name, 'hex', $this->hsl(3));
123
	}
124
	
125
	public function cmyk_scheme(string $scheme_name) :array {
126
		return static::_scheme($scheme_name, 'cmyk', $this->hsl(3));
127
	}
128
	
129
	protected static function _scheme(string $scheme_name, string $callback, array $hsl) {
130
		if (is_callable($callable = [new scheme, $callback])) {
131
			return call_user_func($callable, $hsl['h'], $hsl['s'], $hsl['l'], $scheme_name);
132
		}
133
		error::call(sprintf(
134
			'The $callback "%s" is not a valid callback',
135
			$scheme_name,
136
			__CLASS__,
137
			__FUNCTION__
138
		));
139
		return [];
140
	}
141
	
142
	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 {
143
		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...
144
	}
145
	
146
	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 {
147
		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...
148
	}
149
	
150
}
151