-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathColorWheel.php
More file actions
executable file
·75 lines (66 loc) · 2.74 KB
/
ColorWheel.php
File metadata and controls
executable file
·75 lines (66 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
class ColorWheel {
var $hues;
var $values;
function __construct($totalColors, $saturation=100, $brightness=100) {
$distance = 360 / max(5,$totalColors);
for ($i=0;$i<$totalColors;$i++) {
$this->hues[$i] = $distance * $i;
$color = $this->hsbToRgb($this->hues[$i], $saturation, $brightness);
$this->values[$i] = sprintf('%02X%02X%02X', $color['red'], $color['green'], $color['blue']);
}
}
function Fetch($index, $saturation=100, $brightness=100) {
$color = $this->hsbToRgb($hues[$index], $saturation, $brightness);
return '#'.dechex($color['red']).dechex($color['green']).dechex($color['blue']);
}
private function hsbToRgb($hue, $saturation, $brightness) {
$hue = $this->significantRound($hue, 3);
if ($hue < 0 || $hue > 360) {
throw new LengthException('Argument $hue is not a number between 0 and 360');
}
$hue = $hue == 360 ? 0 : $hue;
$saturation = $this->significantRound($saturation, 3);
if ($saturation < 0 || $saturation > 100) {
throw new LengthException('Argument $saturation is not a number between 0 and 100');
}
$brightness = $this->significantRound($brightness, 3);
if ($brightness < 0 || $brightness > 100) {
throw new LengthException('Argument $brightness is not a number between 0 and 100.');
}
$hexBrightness = (int) round($brightness * 2.55);
if ($saturation == 0) {
return array('red' => $hexBrightness, 'green' => $hexBrightness, 'blue' => $hexBrightness);
}
$Hi = floor($hue / 60);
$f = $hue / 60 - $Hi;
$p = (int) round($brightness * (100 - $saturation) * .0255);
$q = (int) round($brightness * (100 - $f * $saturation) * .0255);
$t = (int) round($brightness * (100 - (1 - $f) * $saturation) * .0255);
switch ($Hi) {
case 0:
return array('red' => $hexBrightness, 'green' => $t, 'blue' => $p);
case 1:
return array('red' => $q, 'green' => $hexBrightness, 'blue' => $p);
case 2:
return array('red' => $p, 'green' => $hexBrightness, 'blue' => $t);
case 3:
return array('red' => $p, 'green' => $q, 'blue' => $hexBrightness);
case 4:
return array('red' => $t, 'green' => $p, 'blue' => $hexBrightness);
case 5:
return array('red' => $hexBrightness, 'green' => $p, 'blue' => $q);
}
return false;
}
function significantRound($number, $precision) {
if (!is_numeric($number)) {
throw new InvalidArgumentException('Argument $number must be an number.');
}
if (!is_int($precision)) {
throw new InvalidArgumentException('Argument $precision must be an integer.');
}
return round($number, $precision - strlen(floor($number)));
}
}
?>