xref: /webtrees/app/ColorGenerator.php (revision 28c95b7a4ecf09fcf6dd432dea55b49642f19f25)
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	/** @var int */
24	private $hue;
25
26	/** @var int */
27	private $basehue;
28
29	/** @var int */
30	private $saturation;
31
32	/** @var int */
33	private $lightness;
34
35	/** @var int */
36	private $baselightness;
37
38	/** @var int */
39	private $alpha;
40
41	/** @var int */
42	private $range;
43
44	/**
45	 * @param int $hue (0Deg = Red, 120Deg = green, 240Deg = blue)
46	 * @param int $saturation
47	 * @param int $lightness
48	 * @param int $alpha
49	 * @param int $range (sign determines direction. positive = clockwise, negative = anticlockwise)
50	 */
51	public function __construct($hue, $saturation, $lightness, $alpha, $range) {
52		$this->hue           = $hue;
53		$this->basehue       = $hue;
54		$this->saturation    = $saturation;
55		$this->lightness     = $lightness;
56		$this->baselightness = $lightness;
57		$this->alpha         = $alpha;
58		$this->range         = $range;
59	}
60
61	/**
62	 * Function getNextColor
63	 *
64	 * $lightness cycles between $baselightness and 100% in $lightnessStep steps
65	 * $hue cycles on each complete $lightness cycle
66	 * between $basehue and $basehue + $range degrees in $hueStep degrees
67	 *
68	 * @param int $lightnessStep
69	 * @param int $hueStep
70	 *
71	 * @return string
72	 */
73	public function getNextColor($lightnessStep = 10, $hueStep = 15) {
74		$lightness = $this->lightness + $lightnessStep;
75		$hue       = $this->hue;
76
77		if ($lightness >= 100) {
78			$lightness = $this->baselightness;
79			$hue += $hueStep * (abs($this->range) / $this->range);
80			if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
81				$hue = $this->basehue;
82			}
83			$this->hue = $hue;
84		}
85		$this->lightness = $lightness;
86
87		return sprintf("hsla(%s, %s%%, %s%%, %s)",
88			$this->hue,
89			$this->saturation,
90			$this->lightness,
91			$this->alpha);
92	}
93
94}
95