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