xref: /webtrees/app/ColorGenerator.php (revision 1062a1429914c995339f502856821457aa975a5a)
1e3d81ebaSDavid Drury<?php
2e3d81ebaSDavid Drury/**
3e3d81ebaSDavid Drury * webtrees: online genealogy
4*1062a142SGreg Roach * Copyright (C) 2018 webtrees development team
5e3d81ebaSDavid Drury * This program is free software: you can redistribute it and/or modify
6e3d81ebaSDavid Drury * it under the terms of the GNU General Public License as published by
7e3d81ebaSDavid Drury * the Free Software Foundation, either version 3 of the License, or
8e3d81ebaSDavid Drury * (at your option) any later version.
9e3d81ebaSDavid Drury * This program is distributed in the hope that it will be useful,
10e3d81ebaSDavid Drury * but WITHOUT ANY WARRANTY; without even the implied warranty of
11e3d81ebaSDavid Drury * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12e3d81ebaSDavid Drury * GNU General Public License for more details.
13e3d81ebaSDavid Drury * You should have received a copy of the GNU General Public License
14e3d81ebaSDavid Drury * along with this program. If not, see <http://www.gnu.org/licenses/>.
15e3d81ebaSDavid Drury */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17e3d81ebaSDavid Drury
18e3d81ebaSDavid Drury/**
1976692c8bSGreg Roach * Generate a range of colurs for the lifespan chart.
20e3d81ebaSDavid Drury */
21e3d81ebaSDavid Druryclass ColorGenerator {
2276692c8bSGreg Roach	/** @var int Current hue */
23e3d81ebaSDavid Drury	private $hue;
2428c95b7aSGreg Roach
2576692c8bSGreg Roach	/** @var int Initial hue*/
26e3d81ebaSDavid Drury	private $basehue;
2728c95b7aSGreg Roach
2876692c8bSGreg Roach	/** @var int Saturation */
29e3d81ebaSDavid Drury	private $saturation;
3028c95b7aSGreg Roach
3176692c8bSGreg Roach	/** @var int Lightness */
32e3d81ebaSDavid Drury	private $lightness;
3328c95b7aSGreg Roach
3476692c8bSGreg Roach	/** @var int Initial lightness*/
35e3d81ebaSDavid Drury	private $baselightness;
3628c95b7aSGreg Roach
3776692c8bSGreg Roach	/** @var int Alpha transparancy */
38e3d81ebaSDavid Drury	private $alpha;
3928c95b7aSGreg Roach
4076692c8bSGreg Roach	/** @var int Clockwise or anticlockwise color wheel */
41e3d81ebaSDavid Drury	private $range;
42e3d81ebaSDavid Drury
43e3d81ebaSDavid Drury	/**
4476692c8bSGreg Roach	 * Create a color generator.
4576692c8bSGreg Roach	 *
4628c95b7aSGreg Roach	 * @param int $hue (0Deg = Red, 120Deg = green, 240Deg = blue)
4728c95b7aSGreg Roach	 * @param int $saturation
4828c95b7aSGreg Roach	 * @param int $lightness
4928c95b7aSGreg Roach	 * @param int $alpha
5028c95b7aSGreg Roach	 * @param int $range (sign determines direction. positive = clockwise, negative = anticlockwise)
51e3d81ebaSDavid Drury	 */
52e3d81ebaSDavid Drury	public function __construct($hue, $saturation, $lightness, $alpha, $range) {
53e3d81ebaSDavid Drury		$this->hue           = $hue;
54e3d81ebaSDavid Drury		$this->basehue       = $hue;
55e3d81ebaSDavid Drury		$this->saturation    = $saturation;
56e3d81ebaSDavid Drury		$this->lightness     = $lightness;
57e3d81ebaSDavid Drury		$this->baselightness = $lightness;
58e3d81ebaSDavid Drury		$this->alpha         = $alpha;
59e3d81ebaSDavid Drury		$this->range         = $range;
60e3d81ebaSDavid Drury	}
61e3d81ebaSDavid Drury
62e3d81ebaSDavid Drury	/**
63e3d81ebaSDavid Drury	 * Function getNextColor
64e3d81ebaSDavid Drury	 *
65e3d81ebaSDavid Drury	 * $lightness cycles between $baselightness and 100% in $lightnessStep steps
66e3d81ebaSDavid Drury	 * $hue cycles on each complete $lightness cycle
67e3d81ebaSDavid Drury	 * between $basehue and $basehue + $range degrees in $hueStep degrees
68e3d81ebaSDavid Drury	 *
69e3d81ebaSDavid Drury	 * @param int $lightnessStep
70e3d81ebaSDavid Drury	 * @param int $hueStep
7128c95b7aSGreg Roach	 *
72e3d81ebaSDavid Drury	 * @return string
73e3d81ebaSDavid Drury	 */
74e3d81ebaSDavid Drury	public function getNextColor($lightnessStep = 10, $hueStep = 15) {
75e3d81ebaSDavid Drury		$lightness = $this->lightness + $lightnessStep;
76e3d81ebaSDavid Drury		$hue       = $this->hue;
77e3d81ebaSDavid Drury
78e3d81ebaSDavid Drury		if ($lightness >= 100) {
79e3d81ebaSDavid Drury			$lightness = $this->baselightness;
800300c1d1SGreg Roach			if ($this->range > 0) {
810300c1d1SGreg Roach				$hue += $hueStep;
820300c1d1SGreg Roach			} else {
830300c1d1SGreg Roach				$hue -= $hueStep;
840300c1d1SGreg Roach			}
85e3d81ebaSDavid Drury			if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
86e3d81ebaSDavid Drury				$hue = $this->basehue;
87e3d81ebaSDavid Drury			}
88e3d81ebaSDavid Drury			$this->hue = $hue;
89e3d81ebaSDavid Drury		}
90e3d81ebaSDavid Drury		$this->lightness = $lightness;
91e3d81ebaSDavid Drury
927a6ee1acSGreg Roach		return sprintf('hsla(%s, %s%%, %s%%, %s)',
93e3d81ebaSDavid Drury			$this->hue,
94e3d81ebaSDavid Drury			$this->saturation,
95e3d81ebaSDavid Drury			$this->lightness,
96e3d81ebaSDavid Drury			$this->alpha);
97e3d81ebaSDavid Drury	}
98e3d81ebaSDavid Drury}
99