yiq_scheme   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 184
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A analogous_set() 0 3 1
A complementary_set() 0 3 1
A compound_set() 0 3 1
A monochromatic_set() 0 3 1
A shades_set() 0 3 1
A tetrad_set() 0 3 1
A weighted_tetrad_set() 0 3 1
A triad_set() 0 3 1
A weighted_triad_set() 0 3 1
A rectangular_set() 0 3 1
A yiq_adjust() 0 9 2
A set_adjust() 0 11 3
1
<?php
2
/**
3
 * Scheme Class
4
 * 
5
 * This class has all the predefined HSL scheme algorithms.
6
 */
7
8
namespace projectcleverweb\color;
9
10
/**
11
 * Scheme Class
12
 * 
13
 * This class has all the predefined HSL scheme algorithms.
14
 */
15
class yiq_scheme extends scheme {
16
	
17
	/**
18
	 * A static reference to this class. (important for late static binding)
19
	 * @var string
20
	 */
21
	protected static $this_class = __CLASS__;
22
	
23
	/**
24
	 * These colors are all close to each other on a color wheel.
25
	 * 
26
	 * @param  float     $h       The base color hue degree (0 - 359)
27
	 * @param  float     $s       The base color saturation percentage (0 - 100)
28
	 * @param  float     $l       The base color lighting percentage (0 - 100)
29
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
30
	 * @return array              An array of 5 analogous colors where the first offset is the original input.
31
	 */
32 1
	public static function analogous_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
33 1
		return static::set_adjust(parent::analogous_set($h, $s, $l, $is_dark));
34
	}
35
	
36
	/**
37
	 * 2 of these colors are a different shade of the base color. The other 2 are
38
	 * a weighted opposite of the base color.
39
	 * 
40
	 * @param  float     $h       The base color hue degree (0 - 359)
41
	 * @param  float     $s       The base color saturation percentage (0 - 100)
42
	 * @param  float     $l       The base color lighting percentage (0 - 100)
43
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
44
	 * @return array              An array of 5 complementary colors where the first offset is the original input.
45
	 */
46 1
	public static function complementary_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
47 1
		return static::set_adjust(parent::complementary_set($h, $s, $l, $is_dark));
48
	}
49
	
50
	/**
51
	 * These colors use mathematical offsets that usually complement each other
52
	 * well, and can highlight the base color.
53
	 * 
54
	 * @param  float     $h       The base color hue degree (0 - 359)
55
	 * @param  float     $s       The base color saturation percentage (0 - 100)
56
	 * @param  float     $l       The base color lighting percentage (0 - 100)
57
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
58
	 * @return array              An array of 5 compounding colors where the first offset is the original input.
59
	 */
60 1
	public static function compound_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
61 1
		return static::set_adjust(parent::compound_set($h, $s, $l, $is_dark));
62
	}
63
	
64
	/**
65
	 * 5 complementary shades of one color.
66
	 * 
67
	 * @param  float     $h       The base color hue degree (0 - 359)
68
	 * @param  float     $s       The base color saturation percentage (0 - 100)
69
	 * @param  float     $l       The base color lighting percentage (0 - 100)
70
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
71
	 * @return array              An array of 5 complementary shades of colors where the first offset is the original input.
72
	 */
73 1
	public static function monochromatic_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
74 1
		return static::set_adjust(parent::monochromatic_set($h, $s, $l, $is_dark));
75
	}
76
	
77
	/**
78
	 * 5 different shades of one color.
79
	 * 
80
	 * @param  float     $h       The base color hue degree (0 - 359)
81
	 * @param  float     $s       The base color saturation percentage (0 - 100)
82
	 * @param  float     $l       The base color lighting percentage (0 - 100)
83
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
84
	 * @return array              An array of 5 shades of a color where the first offset is the original input.
85
	 */
86 6
	public static function shades_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
87 6
		return static::set_adjust(parent::shades_set($h, $s, $l, $is_dark));
88
	}
89
	
90
	/**
91
	 * 3 of these colors are all equally distanced from each other on a color
92
	 * wheel, plus 1 alternated shade for the base color and the 1 color that is
93
	 * opposite of the base color.
94
	 * 
95
	 * @param  float     $h       The base color hue degree (0 - 359)
96
	 * @param  float     $s       The base color saturation percentage (0 - 100)
97
	 * @param  float     $l       The base color lighting percentage (0 - 100)
98
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
99
	 * @return array              An array of 5 triangular colors where the first offset is the original input.
100
	 */
101 1
	public static function tetrad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
102 1
		return static::set_adjust(parent::tetrad_set($h, $s, $l, $is_dark));
103
	}
104
	
105
	/**
106
	 * 3 of these colors are all similarly distanced from each other on a color
107
	 * wheel, the base color has an alternate shade, and there is a weighted
108
	 * opposite color. These colors are all slightly closer to the base color
109
	 * than in a normal tetrad.
110
	 * 
111
	 * @param  float     $h       The base color hue degree (0 - 359)
112
	 * @param  float     $s       The base color saturation percentage (0 - 100)
113
	 * @param  float     $l       The base color lighting percentage (0 - 100)
114
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
115
	 * @return array              An array of 5 triangular colors where the first offset is the original input.
116
	 */
117 1
	public static function weighted_tetrad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
118 1
		return static::set_adjust(parent::weighted_tetrad_set($h, $s, $l, $is_dark));
119
	}
120
	
121
	/**
122
	 * These colors are all equally distanced from each other on a color wheel,
123
	 * 2 of which have an alternate shade.
124
	 * 
125
	 * @param  float     $h       The base color hue degree (0 - 359)
126
	 * @param  float     $s       The base color saturation percentage (0 - 100)
127
	 * @param  float     $l       The base color lighting percentage (0 - 100)
128
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
129
	 * @return array              An array of 5 triangular colors where the first offset is the original input.
130
	 */
131 1
	public static function triad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
132 1
		return static::set_adjust(parent::triad_set($h, $s, $l, $is_dark));
133
	}
134
	
135
	/**
136
	 * These colors are all similarly distanced from each other on a color wheel,
137
	 * 2 of which have an alternate shade. These colors are all slightly closer to
138
	 * the base color than in a normal triad.
139
	 * 
140
	 * @param  float     $h       The base color hue degree (0 - 359)
141
	 * @param  float     $s       The base color saturation percentage (0 - 100)
142
	 * @param  float     $l       The base color lighting percentage (0 - 100)
143
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
144
	 * @return array              An array of 5 weighted triangular colors where the first offset is the original input.
145
	 */
146 1
	public static function weighted_triad_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
147 1
		return static::set_adjust(parent::weighted_triad_set($h, $s, $l, $is_dark));
148
	}
149
	
150
	/**
151
	 * 4 of these colors form a rectangle on a color wheel, and 1 color is an
152
	 * alternate shade for the base color.
153
	 * 
154
	 * @param  float     $h       The base color hue degree (0 - 359)
155
	 * @param  float     $s       The base color saturation percentage (0 - 100)
156
	 * @param  float     $l       The base color lighting percentage (0 - 100)
157
	 * @param  bool|null $is_dark Whether or not to treat the base color as a dark color. Leave as null to dynamically generate this.
158
	 * @return array              An array of 5 rectangular colors where the first offset is the original input.
159
	 */
160 1
	public static function rectangular_set (float $h = 0.0, float $s = 0.0, float $l = 0.0, $is_dark = NULL) :array {
161 1
		return static::set_adjust(parent::rectangular_set($h, $s, $l, $is_dark));
162
	}
163
	
164
	/**
165
	 * Adjusts a hue in the YIQ spectrum
166
	 * 
167
	 * @param  float  $current The current hue
168
	 * @param  float  $mod     The desired amount of change (0 - 359; is converted to a percentage)
169
	 * @return int             The resulting hue
170
	 */
171 15
	protected static function yiq_adjust(float $current, float $mod) :int {
172 15
		if ($mod == 0) {
173 13
			return $current;
174
		}
175 8
		$change      = regulate::div($mod, 360) * 1000;
176 8
		$current_yiq = generate::hue_to_yiq($current);
177 8
		$new_loc     = regulate::max(abs($current_yiq + $change), 1000);
178 8
		return round(generate::yiq_to_hue($new_loc));
179
	}
180
	
181
	/**
182
	 * Adjust a scheme array to use YIQ adjusted hues
183
	 * 
184
	 * @param  array $scheme The scheme to adjust
185
	 * @return array         The resulting $scheme
186
	 */
187 15
	protected static function set_adjust(array $scheme) :array {
188 15
		foreach($scheme as $i => &$hsl) {
189 15
			if ($i == 0) {
190 15
				$org = $hsl['h'];
191 15
				continue;
192
			}
193 15
			$hsl['h'] = static::yiq_adjust($org, $hsl['h'] - $org);
0 ignored issues
show
Bug introduced by
The variable $org does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
194
		}
195
		
196 15
		return $scheme;
197
	}
198
}
199