xref: /webtrees/app/ColorGenerator.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
1e3d81ebaSDavid Drury<?php
2e3d81ebaSDavid Drury/**
3e3d81ebaSDavid Drury * webtrees: online genealogy
4e3d81ebaSDavid Drury * Copyright (C) 2015 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 */
16*76692c8bSGreg Roachnamespace Fisharebest\Webtrees;
17e3d81ebaSDavid Drury
18e3d81ebaSDavid Drury/**
19*76692c8bSGreg Roach * Generate a range of colurs for the lifespan chart.
20e3d81ebaSDavid Drury */
21e3d81ebaSDavid Druryclass ColorGenerator {
22*76692c8bSGreg Roach	/** @var int Current hue */
23e3d81ebaSDavid Drury	private $hue;
2428c95b7aSGreg Roach
25*76692c8bSGreg Roach	/** @var int Initial hue*/
26e3d81ebaSDavid Drury	private $basehue;
2728c95b7aSGreg Roach
28*76692c8bSGreg Roach	/** @var int Saturation */
29e3d81ebaSDavid Drury	private $saturation;
3028c95b7aSGreg Roach
31*76692c8bSGreg Roach	/** @var int Lightness */
32e3d81ebaSDavid Drury	private $lightness;
3328c95b7aSGreg Roach
34*76692c8bSGreg Roach	/** @var int Initial lightness*/
35e3d81ebaSDavid Drury	private $baselightness;
3628c95b7aSGreg Roach
37*76692c8bSGreg Roach	/** @var int Alpha transparancy */
38e3d81ebaSDavid Drury	private $alpha;
3928c95b7aSGreg Roach
40*76692c8bSGreg Roach	/** @var int Clockwise or anticlockwise color wheel */
41e3d81ebaSDavid Drury	private $range;
42e3d81ebaSDavid Drury
43e3d81ebaSDavid Drury	/**
44*76692c8bSGreg Roach	 * Create a color generator.
45*76692c8bSGreg 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;
80e3d81ebaSDavid Drury			$hue += $hueStep * (abs($this->range) / $this->range);
81e3d81ebaSDavid Drury			if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
82e3d81ebaSDavid Drury				$hue = $this->basehue;
83e3d81ebaSDavid Drury			}
84e3d81ebaSDavid Drury			$this->hue = $hue;
85e3d81ebaSDavid Drury		}
86e3d81ebaSDavid Drury		$this->lightness = $lightness;
87e3d81ebaSDavid Drury
88e3d81ebaSDavid Drury		return sprintf("hsla(%s, %s%%, %s%%, %s)",
89e3d81ebaSDavid Drury			$this->hue,
90e3d81ebaSDavid Drury			$this->saturation,
91e3d81ebaSDavid Drury			$this->lightness,
92e3d81ebaSDavid Drury			$this->alpha);
93e3d81ebaSDavid Drury	}
94e3d81ebaSDavid Drury
95e3d81ebaSDavid Drury}
96