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