xref: /webtrees/app/ColorGenerator.php (revision e3d81ebafd91ad4b59c34a3495fe9aca687c7a6e)
1<?php
2namespace Fisharebest\Webtrees;
3
4	/**
5	 * webtrees: online genealogy
6	 * Copyright (C) 2015 webtrees development team
7	 * This program is free software: you can redistribute it and/or modify
8	 * it under the terms of the GNU General Public License as published by
9	 * the Free Software Foundation, either version 3 of the License, or
10	 * (at your option) any later version.
11	 * This program is distributed in the hope that it will be useful,
12	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14	 * GNU General Public License for more details.
15	 * You should have received a copy of the GNU General Public License
16	 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17	 */
18
19/**
20 * Class colorGenerator
21 */
22class ColorGenerator {
23
24	private $hue;
25	private $basehue;
26	private $saturation;
27	private $lightness;
28	private $baselightness;
29	private $alpha;
30	private $range;
31
32	/**
33	 * @param integer $hue (0Deg = Red, 120Deg = green, 240Deg = blue)
34	 * @param integer $saturation
35	 * @param integer $lightness
36	 * @param integer $alpha
37	 * @param integer $range (sign determines direction. positive = clockwise, negative = anticlockwise)
38	 */
39	public function __construct($hue, $saturation, $lightness, $alpha, $range) {
40		$this->hue           = $hue;
41		$this->basehue       = $hue;
42		$this->saturation    = $saturation;
43		$this->lightness     = $lightness;
44		$this->baselightness = $lightness;
45		$this->alpha         = $alpha;
46		$this->range         = $range;
47	}
48
49	/**
50	 * Function getNextColor
51	 *
52	 * $lightness cycles between $baselightness and 100% in $lightnessStep steps
53	 * $hue cycles on each complete $lightness cycle
54	 * between $basehue and $basehue + $range degrees in $hueStep degrees
55	 *
56	 * @param int $lightnessStep
57	 * @param int $hueStep
58	 * @return string
59	 */
60	public function getNextColor($lightnessStep = 10, $hueStep = 15) {
61		$lightness = $this->lightness + $lightnessStep;
62		$hue       = $this->hue;
63
64		if ($lightness >= 100) {
65			$lightness = $this->baselightness;
66			$hue += $hueStep * (abs($this->range) / $this->range);
67			if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
68				$hue = $this->basehue;
69			}
70			$this->hue = $hue;
71		}
72		$this->lightness = $lightness;
73
74		return sprintf("hsla(%s, %s%%, %s%%, %s)",
75			$this->hue,
76			$this->saturation,
77			$this->lightness,
78			$this->alpha);
79	}
80
81}
82