| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2019-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 |
/** |
| 25 |
* Colour range for HSL values |
| 26 |
*/ |
| 27 |
class ColourRangeHSL extends ColourRange { |
| 28 |
|
| 29 |
private $h1, $s1, $l1; |
| 30 |
private $hdiff, $sdiff, $ldiff; |
| 31 |
|
| 32 |
/** |
| 33 |
* HSL range |
| 34 |
*/ |
| 35 |
public function __construct($h1, $s1, $l1, $h2, $s2, $l2) |
| 36 |
{ |
| 37 |
$this->h1 = $this->clamp($h1, 0, 360); |
| 38 |
$this->s1 = $this->clamp($s1, 0, 1); |
| 39 |
$this->l1 = $this->clamp($l1, 0, 1); |
| 40 |
|
| 41 |
$hdiff = $this->clamp($h2, 0, 360) - $this->h1; |
| 42 |
if(abs($hdiff) > 180) |
| 43 |
$hdiff += $hdiff < 0 ? 360 : -360; |
| 44 |
$this->hdiff = $hdiff; |
| 45 |
$this->sdiff = $this->clamp($s2, 0, 1) - $this->s1; |
| 46 |
$this->ldiff = $this->clamp($l2, 0, 1) - $this->l1; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Reverse direction of colour cycle |
| 51 |
*/ |
| 52 |
public function reverse() |
| 53 |
{ |
| 54 |
$this->hdiff += $this->hdiff < 0 ? 360 : -360; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Return the colour from the range |
| 59 |
*/ |
| 60 |
#[\ReturnTypeWillChange] |
| 61 |
public function offsetGet($offset) |
| 62 |
{ |
| 63 |
$c = max($this->count - 1, 1); |
| 64 |
$offset = $this->clamp($offset, 0, $c); |
| 65 |
$h = fmod(360 + $this->h1 + $offset * $this->hdiff / $c, 360); |
| 66 |
$s = $this->s1 + $offset * $this->sdiff / $c; |
| 67 |
$l = $this->l1 + $offset * $this->ldiff / $c; |
| 68 |
|
| 69 |
list($r, $g, $b) = $this->hslToRgb($h, $s, $l); |
| 70 |
return sprintf('#%02x%02x%02x', $r, $g, $b); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Factory method creates an instance from RGB values |
| 75 |
*/ |
| 76 |
public static function fromRgb($r1, $g1, $b1, $r2, $g2, $b2) |
| 77 |
{ |
| 78 |
list($h1, $s1, $l1) = ColourRangeHSL::rgbToHsl($r1, $g1, $b1); |
| 79 |
list($h2, $s2, $l2) = ColourRangeHSL::rgbToHsl($r2, $g2, $b2); |
| 80 |
return new ColourRangeHSL($h1, $s1, $l1, $h2, $s2, $l2); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Convert RGB to HSL (0-360, 0-1, 0-1) |
| 85 |
*/ |
| 86 |
public static function rgbToHsl($r, $g, $b) |
| 87 |
{ |
| 88 |
$r1 = ColourRangeHSL::clamp($r, 0, 255) / 255; |
| 89 |
$g1 = ColourRangeHSL::clamp($g, 0, 255) / 255; |
| 90 |
$b1 = ColourRangeHSL::clamp($b, 0, 255) / 255; |
| 91 |
$cmax = max($r1, $g1, $b1); |
| 92 |
$cmin = min($r1, $g1, $b1); |
| 93 |
$delta = $cmax - $cmin; |
| 94 |
|
| 95 |
$l = ($cmax + $cmin) / 2; |
| 96 |
if($delta == 0) { |
| 97 |
$h = $s = 0; |
| 98 |
} else { |
| 99 |
if($cmax == $r1) { |
| 100 |
$h = fmod(($g1 - $b1) / $delta, 6); |
| 101 |
} elseif($cmax == $g1) { |
| 102 |
$h = 2 + ($b1 - $r1) / $delta; |
| 103 |
} else { |
| 104 |
$h = 4 + ($r1 - $g1) / $delta; |
| 105 |
} |
| 106 |
$h = fmod(360 + ($h * 60), 360); |
| 107 |
$s = $delta / (1 - abs(2 * $l - 1)); |
| 108 |
} |
| 109 |
return [$h, $s, $l]; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Convert HSL to RGB |
| 114 |
*/ |
| 115 |
public static function hslToRgb($h, $s, $l) |
| 116 |
{ |
| 117 |
$h1 = ColourRangeHSL::clamp($h, 0, 360); |
| 118 |
$s1 = ColourRangeHSL::clamp($s, 0, 1); |
| 119 |
$l1 = ColourRangeHSL::clamp($l, 0, 1); |
| 120 |
|
| 121 |
$c = (1 - abs(2 * $l1 - 1)) * $s1; |
| 122 |
$x = $c * (1 - abs(fmod($h1 / 60, 2) - 1)); |
| 123 |
$m = $l1 - $c / 2; |
| 124 |
|
| 125 |
$c = 255 * ($c + $m); |
| 126 |
$x = 255 * ($x + $m); |
| 127 |
$m *= 255; |
| 128 |
switch(floor($h1 / 60)) { |
| 129 |
case 0 : $rgb = [$c, $x, $m]; break; |
| 130 |
case 1 : $rgb = [$x, $c, $m]; break; |
| 131 |
case 2 : $rgb = [$m, $c, $x]; break; |
| 132 |
case 3 : $rgb = [$m, $x, $c]; break; |
| 133 |
case 4 : $rgb = [$x, $m, $c]; break; |
| 134 |
case 5 : $rgb = [$c, $m, $x]; break; |
| 135 |
} |
| 136 |
|
| 137 |
return $rgb; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|