-
Notifications
You must be signed in to change notification settings - Fork 31
/
Colours.php
201 lines (179 loc) · 5.19 KB
/
Colours.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Copyright (C) 2014-2022 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <[email protected]>
*/
namespace Goat1000\SVGGraph;
class Colours implements \Countable {
private $colours = [];
private $dataset_count = 0;
private $fallback = false;
private $max_index = 1;
private $reverse = false;
/**
* Constructor sets up fallback colour array in case per-dataset
* functions are not used
*/
public function __construct($colours = null)
{
if(is_array($colours))
$this->fallback = $colours;
else
$this->fallback = [
'#11c', '#c11', '#cc1', '#1c1', '#c81',
'#116', '#611', '#661', '#161', '#631'
];
}
/**
* Setup based on graph requirements
*/
public function setup($count, $datasets = null, $reverse = false)
{
if($this->fallback !== false) {
if($datasets !== null) {
foreach($this->fallback as $colour) {
// in fallback, each dataset gets one colour
$this->colours[] = new ColourArray([$colour]);
}
} else {
$this->colours[] = new ColourArray($this->fallback);
}
$this->dataset_count = count($this->colours);
}
foreach($this->colours as $clist)
$clist->setup($count);
$this->max_index = $count - 1;
$this->reverse = $reverse;
}
/**
* Returns the colour for an index and dataset
*/
public function getColour($index, $dataset = null)
{
// default is for a colour per dataset
if($dataset === null)
$dataset = 0;
if($this->reverse)
$index = $this->max_index - $index;
// see if specific dataset exists
if(array_key_exists($dataset, $this->colours))
return $this->colours[$dataset][$index];
// try mod
if(is_numeric($dataset))
$dataset = $dataset % $this->dataset_count;
if(array_key_exists($dataset, $this->colours))
return $this->colours[$dataset][$index];
// just use first dataset
reset($this->colours);
$clist = current($this->colours);
return $clist[$index];
}
/**
* Implement Countable to make it non-countable
*/
#[\ReturnTypeWillChange]
public function count()
{
throw new \Exception('Cannot count Colours class');
return 0;
}
/**
* Set an entry in the colours array
*/
private function setDataset($dataset, $colours)
{
if($this->fallback) {
// use fallback for dataset 0 if not already set
if($dataset != 0)
$this->colours[0] = new ColourArray($this->fallback);
$this->fallback = false;
}
$this->colours[$dataset] = $colours;
$this->dataset_count = count($this->colours);
}
/**
* Assign a colour array for a dataset
*/
public function set($dataset, $colours)
{
if($colours === null) {
if(array_key_exists($dataset, $this->colours))
unset($this->colours[$dataset]);
return;
}
$this->setDataset($dataset, new ColourArray($colours));
}
/**
* Set up RGB colour range
*/
public function rangeRGB($dataset, $r1, $g1, $b1, $r2, $g2, $b2)
{
$this->setDataset($dataset,
new ColourRangeRGB($r1, $g1, $b1, $r2, $g2, $b2));
}
/**
* HSL colour range, with option to go the long way
*/
public function rangeHSL($dataset, $h1, $s1, $l1, $h2, $s2, $l2,
$reverse = false)
{
$rng = new ColourRangeHSL($h1, $s1, $l1, $h2, $s2, $l2);
if($reverse)
$rng->reverse();
$this->setDataset($dataset, $rng);
}
/**
* HSL colour range from RGB values, with option to go the long way
*/
public function rangeRGBtoHSL($dataset, $r1, $g1, $b1, $r2, $g2, $b2,
$reverse = false)
{
$rng = ColourRangeHSL::fromRGB($r1, $g1, $b1, $r2, $g2, $b2);
if($reverse)
$rng->reverse();
$this->setDataset($dataset, $rng);
}
/**
* RGB colour range from two RGB hex codes
*/
public function rangeHexRGB($dataset, $c1, $c2)
{
list($r1, $g1, $b1) = $this->hexRGB($c1);
list($r2, $g2, $b2) = $this->hexRGB($c2);
$this->rangeRGB($dataset, $r1, $g1, $b1, $r2, $g2, $b2);
}
/**
* HSL colour range from RGB hex codes
*/
public function rangeHexHSL($dataset, $c1, $c2, $reverse = false)
{
list($r1, $g1, $b1) = $this->hexRGB($c1);
list($r2, $g2, $b2) = $this->hexRGB($c2);
$this->rangeRGBtoHSL($dataset, $r1, $g1, $b1, $r2, $g2, $b2, $reverse);
}
/**
* Convert a colour code to RGB array
*/
public static function hexRGB($c)
{
// support filters, other colour formats by using Colour class
$graph = null;
$cc = new Colour($graph, $c, false, false, false);
return $cc->rgb();
}
}