Completed
Push — master ( 488975...9ee5a3 )
by Nicholas
03:10
created

main::gradient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Main Interface For Library
4
 * ==========================
5
 * Provides a simplified interface so programming with colors can be relatively
6
 * quick & easy
7
 */
8
9
namespace projectcleverweb\color;
10
11
/**
12
 * Main Interface For Library
13
 * ==========================
14
 * Provides a simplified interface so programming with colors can be relatively
15
 * quick & easy
16
 */
17
class main extends main_peripheral {
18
	
19
	public $color;
20
	
21
	protected $cache;
22
	
23
	public function __construct($color, string $type = '') {
24
		$this->set($color, $type);
25
		$this->cache = new cache;
26
	}
27
	
28
	protected function set($color, string $type = '') {
29
		if (is_a($color, __CLASS__)) {
30
			$this->color = clone $color->color;
31
		} else {
32
			$this->color = new color($color, $type);
33
		}
34
	}
35
	
36
	public function rgb() :array {
37
		return (array) $this->color->rgb + ['a' => $this->color->alpha()];
38
	}
39
	
40
	public function hsl(int $accuracy = 0) :array {
41
		$color = [];
42
		foreach($this->color->hsl() as $key => $value) {
43
			$color[$key] = round($value, abs($accuracy));
44
		}
45
		return $color + ['a' => $this->color->alpha()];
46
	}
47
	
48 View Code Duplication
	public function cmyk() :array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
		if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) {
50
			return $cached;
51
		}
52
		$rgb    = $this->color->rgb;
53
		$result = convert::rgb_to_cmyk($rgb['r'], $rgb['g'], $rgb['b']) + ['a' => $this->color->alpha()];
54
		$this->cache->set(__FUNCTION__, $this->hex(), $result);
55
		return $result;
56
	}
57
	
58 View Code Duplication
	public function hsb(int $accuracy = 0) :array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
		if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) {
60
			return $cached;
61
		}
62
		$rgb    = $this->color->rgb;
63
		$result = convert::rgb_to_hsb($rgb['r'], $rgb['g'], $rgb['b'], $accuracy) + ['a' => $this->color->alpha()];
64
		$this->cache->set(__FUNCTION__, $this->hex(), $result);
65
		return $result;
66
	}
67
	
68
	public function hex() :string {
69
		return $this->color->hex;
70
	}
71
	
72 View Code Duplication
	public function web_safe() :string {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
		if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) {
74
			return $cached;
75
		}
76
		$rgb    = $this->color->rgb;
77
		$result = generate::web_safe($rgb['r'], $rgb['g'], $rgb['b']);
78
		$this->cache->set(__FUNCTION__, $this->hex(), $result);
79
		return $result;
80
	}
81
	
82
	public function css() :string {
83
		return css::best($this->color);
84
	}
85
	
86
	/**
87
	 * Get (and set) the alpha channel
88
	 * 
89
	 * @param  mixed $new_alpha If numeric, the alpha channel is set to this value
90
	 * @return float            The current alpha value
91
	 */
92
	public function alpha($new_alpha) {
93
		return $this->color->alpha($new_alpha);
94
	}
95
	
96 View Code Duplication
	public function is_dark(int $check_score = 128) :bool {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
		if (!is_null($cached = $this->cache->get(__FUNCTION__, $this->hex()))) {
98
			return $cached;
99
		}
100
		$rgb    = $this->color->rgb;
101
		$result = check::is_dark($rgb['r'], $rgb['g'], $rgb['b'], $check_score);
102
		$this->cache->set(__FUNCTION__, $this->hex(), $result);
103
		return $result;
104
	}
105
	
106
	public function red(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
107
		return modify::rgb($this->color, 'red', $adjustment, $as_percentage, $set_absolute);
108
	}
109
	
110
	public function green(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
111
		return modify::rgb($this->color, 'green', $adjustment, $as_percentage, $set_absolute);
112
	}
113
	
114
	public function blue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
115
		return modify::rgb($this->color, 'blue', $adjustment, $as_percentage, $set_absolute);
116
	}
117
	
118
	public function hue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
119
		return modify::hsl($this->color, 'hue', $adjustment, $as_percentage, $set_absolute);
120
	}
121
	
122
	public function saturation(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
123
		return modify::hsl($this->color, 'saturation', $adjustment, $as_percentage, $set_absolute);
124
	}
125
	
126
	public function light(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
127
		return modify::hsl($this->color, 'light', $adjustment, $as_percentage, $set_absolute);
128
	}
129
	
130
	public function blend(main $color2, float $amount = 50.0) {
131
		$c1 = $this->rgb();
132
		$c2 = $color2->rgb();
133
		return new $this(generate::blend(
134
			$c1['r'], $c1['g'], $c1['b'], $c1['a'],
135
			$c2['r'], $c2['g'], $c2['b'], $c2['a'],
136
			$amount
137
		));
138
	}
139
	
140
	public function gradient(main $color2, int $steps = 0) {
141
		$c1 = $this->rgb();
142
		$c2 = $color2->rgb();
143
		return generate::gradient_range(
144
			$c1['r'], $c1['g'], $c1['b'],
145
			$c2['r'], $c2['g'], $c2['b'],
146
			$steps
147
		);
148
	}
149
	
150
	public function scheme(string $scheme_name, string $return_type = 'hex') :array {
151
		if (!is_null($cached = $this->cache->get(__FUNCTION__.'_'.$scheme_name.'_'.$return_type, $this->hex()))) {
152
			return $cached;
153
		}
154
		$result = static::_scheme($scheme_name, strtolower($return_type), $this->hsl(3));
155
		$this->cache->set(__FUNCTION__.'_'.$scheme_name.'_'.$return_type, $this->hex(), $result);
156
		return $result;
157
	}
158
	
159
	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) :main {
160
		return new main(generate::rgb_rand($min_r, $max_r, $min_g, $max_g, $min_b, $max_b));
161
	}
162
	
163
	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) :main {
164
		return new main(generate::hsl_rand($min_h, $max_h, $min_s, $max_s, $min_l, $max_l));
165
	}
166
}
167