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