diff options
Diffstat (limited to 'app/ColorGenerator.php')
| -rw-r--r-- | app/ColorGenerator.php | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/app/ColorGenerator.php b/app/ColorGenerator.php new file mode 100644 index 0000000000..89c4ef6425 --- /dev/null +++ b/app/ColorGenerator.php @@ -0,0 +1,81 @@ +<?php +namespace Fisharebest\Webtrees; + + /** + * webtrees: online genealogy + * Copyright (C) 2015 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * Class colorGenerator + */ +class ColorGenerator { + + private $hue; + private $basehue; + private $saturation; + private $lightness; + private $baselightness; + private $alpha; + private $range; + + /** + * @param integer $hue (0Deg = Red, 120Deg = green, 240Deg = blue) + * @param integer $saturation + * @param integer $lightness + * @param integer $alpha + * @param integer $range (sign determines direction. positive = clockwise, negative = anticlockwise) + */ + public function __construct($hue, $saturation, $lightness, $alpha, $range) { + $this->hue = $hue; + $this->basehue = $hue; + $this->saturation = $saturation; + $this->lightness = $lightness; + $this->baselightness = $lightness; + $this->alpha = $alpha; + $this->range = $range; + } + + /** + * Function getNextColor + * + * $lightness cycles between $baselightness and 100% in $lightnessStep steps + * $hue cycles on each complete $lightness cycle + * between $basehue and $basehue + $range degrees in $hueStep degrees + * + * @param int $lightnessStep + * @param int $hueStep + * @return string + */ + public function getNextColor($lightnessStep = 10, $hueStep = 15) { + $lightness = $this->lightness + $lightnessStep; + $hue = $this->hue; + + if ($lightness >= 100) { + $lightness = $this->baselightness; + $hue += $hueStep * (abs($this->range) / $this->range); + if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) { + $hue = $this->basehue; + } + $this->hue = $hue; + } + $this->lightness = $lightness; + + return sprintf("hsla(%s, %s%%, %s%%, %s)", + $this->hue, + $this->saturation, + $this->lightness, + $this->alpha); + } + +} |
