| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2020 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 |
* Class for modifying a colour |
| 26 |
*/ |
| 27 |
class ColourFilter { |
| 28 |
private $colour; |
| 29 |
private $filters; |
| 30 |
|
| 31 |
public function __construct($colour, $filters) |
| 32 |
{ |
| 33 |
// these are special cases, can't be processed |
| 34 |
if($colour === 'transparent' || $colour === 'none') |
| 35 |
throw new \InvalidArgumentException('Unable to filter colour [' . $colour . ']'); |
| 36 |
$this->colour = new RGBColour($colour); |
| 37 |
|
| 38 |
$filters = explode('/', $filters); |
| 39 |
foreach($filters as $f) { |
| 40 |
$filter = $f; |
| 41 |
$args = []; |
| 42 |
$fpos = strpos($f, '('); |
| 43 |
$epos = strpos($f, ')'); |
| 44 |
if($fpos > 0) { |
| 45 |
$filter = substr($f, 0, $fpos); |
| 46 |
$a = ''; |
| 47 |
if($epos > $fpos) |
| 48 |
$a = substr($f, $fpos + 1, $epos - $fpos - 1); |
| 49 |
if($a !== '') |
| 50 |
$args = preg_split('/[\s,]+/', $a); |
| 51 |
} |
| 52 |
|
| 53 |
if(method_exists($this, $filter)) |
| 54 |
call_user_func_array([$this, $filter], $args); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Increase or decrease brightness |
| 60 |
*/ |
| 61 |
public function brightness($amount = '1.2') |
| 62 |
{ |
| 63 |
list ($operator, $value) = $this->expression($amount); |
| 64 |
if($value === null) |
| 65 |
throw new \InvalidArgumentException('Invalid brightness [' . $amount . ']'); |
| 66 |
list($h, $s, $l) = $this->colour->getHSL(); |
| 67 |
|
| 68 |
$l = min(1.0, max(0.0, $operator === '+' ? $l + $value : $l * $value)); |
| 69 |
$this->colour->setHSL($h, $s, $l); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Increase or decrease saturation |
| 74 |
*/ |
| 75 |
public function saturation($amount = '0.0') |
| 76 |
{ |
| 77 |
list ($operator, $value) = $this->expression($amount); |
| 78 |
if($value === null) |
| 79 |
throw new \InvalidArgumentException('Invalid saturation [' . $amount . ']'); |
| 80 |
list($h, $s, $l) = $this->colour->getHSL(); |
| 81 |
|
| 82 |
$s = min(1.0, max(0.0, $operator === '+' ? $s + $value : $s * $value)); |
| 83 |
$this->colour->setHSL($h, $s, $l); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Modify the hue |
| 88 |
*/ |
| 89 |
public function hue($amount = '60') |
| 90 |
{ |
| 91 |
if(!is_numeric($amount)) |
| 92 |
throw new \InvalidArgumentException('Invalid hue [' . $amount . ']'); |
| 93 |
list($h, $s, $l) = $this->colour->getHSL(); |
| 94 |
$h += $amount; |
| 95 |
$this->colour->setHSL($h, $s, $l); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the expression to be applied |
| 100 |
*/ |
| 101 |
public static function expression($a) |
| 102 |
{ |
| 103 |
$operator = '*'; |
| 104 |
$value = null; |
| 105 |
if($a[0] === '+' || $a[0] === '-') |
| 106 |
$operator = '+'; |
| 107 |
|
| 108 |
$p = strpos($a, '%'); |
| 109 |
if($p > 0) |
| 110 |
$a = substr($a, 0, $p); |
| 111 |
|
| 112 |
if(is_numeric($a)) |
| 113 |
$value = $p > 0 ? $a / 100 : $a; |
| 114 |
return [$operator, $value]; |
| 115 |
} |
| 116 |
|
| 117 |
public function __toString() |
| 118 |
{ |
| 119 |
return $this->colour->getHex(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|