| 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 RGB values |
| 26 |
*/ |
| 27 |
class ColourRangeRGB extends ColourRange { |
| 28 |
|
| 29 |
private $r1, $g1, $b1; |
| 30 |
private $rdiff, $gdiff, $bdiff; |
| 31 |
|
| 32 |
/** |
| 33 |
* RGB range |
| 34 |
*/ |
| 35 |
public function __construct($r1, $g1, $b1, $r2, $g2, $b2) |
| 36 |
{ |
| 37 |
$this->r1 = $this->clamp($r1, 0, 255); |
| 38 |
$this->g1 = $this->clamp($g1, 0, 255); |
| 39 |
$this->b1 = $this->clamp($b1, 0, 255); |
| 40 |
$this->rdiff = $this->clamp($r2, 0, 255) - $this->r1; |
| 41 |
$this->gdiff = $this->clamp($g2, 0, 255) - $this->g1; |
| 42 |
$this->bdiff = $this->clamp($b2, 0, 255) - $this->b1; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Return the colour from the range |
| 47 |
*/ |
| 48 |
#[\ReturnTypeWillChange] |
| 49 |
public function offsetGet($offset) |
| 50 |
{ |
| 51 |
$c = max($this->count - 1, 1); |
| 52 |
$offset = $this->clamp($offset, 0, $c); |
| 53 |
$r = $this->r1 + $offset * $this->rdiff / $c; |
| 54 |
$g = $this->g1 + $offset * $this->gdiff / $c; |
| 55 |
$b = $this->b1 + $offset * $this->bdiff / $c; |
| 56 |
return sprintf('#%02x%02x%02x', $r, $g, $b); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|