xref: /webtrees/app/ColorGenerator.php (revision c1010eda29c0909ed4d5d463f32d32bfefdd4dfe)
1e3d81ebaSDavid Drury<?php
2e3d81ebaSDavid Drury/**
3e3d81ebaSDavid Drury * webtrees: online genealogy
41062a142SGreg 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 */
21*c1010edaSGreg Roachclass ColorGenerator
22*c1010edaSGreg Roach{
2376692c8bSGreg Roach    /** @var int Current hue */
24e3d81ebaSDavid Drury    private $hue;
2528c95b7aSGreg Roach
2676692c8bSGreg Roach    /** @var int Initial hue */
27e3d81ebaSDavid Drury    private $basehue;
2828c95b7aSGreg Roach
2976692c8bSGreg Roach    /** @var int Saturation */
30e3d81ebaSDavid Drury    private $saturation;
3128c95b7aSGreg Roach
3276692c8bSGreg Roach    /** @var int Lightness */
33e3d81ebaSDavid Drury    private $lightness;
3428c95b7aSGreg Roach
3576692c8bSGreg Roach    /** @var int Initial lightness */
36e3d81ebaSDavid Drury    private $baselightness;
3728c95b7aSGreg Roach
3876692c8bSGreg Roach    /** @var int Alpha transparancy */
39e3d81ebaSDavid Drury    private $alpha;
4028c95b7aSGreg Roach
4176692c8bSGreg Roach    /** @var int Clockwise or anticlockwise color wheel */
42e3d81ebaSDavid Drury    private $range;
43e3d81ebaSDavid Drury
44e3d81ebaSDavid Drury    /**
4576692c8bSGreg Roach     * Create a color generator.
4676692c8bSGreg Roach     *
4728c95b7aSGreg Roach     * @param int $hue   (0Deg = Red, 120Deg = green, 240Deg = blue)
4828c95b7aSGreg Roach     * @param int $saturation
4928c95b7aSGreg Roach     * @param int $lightness
5028c95b7aSGreg Roach     * @param int $alpha
5128c95b7aSGreg Roach     * @param int $range (sign determines direction. positive = clockwise, negative = anticlockwise)
52e3d81ebaSDavid Drury     */
53*c1010edaSGreg Roach    public function __construct($hue, $saturation, $lightness, $alpha, $range)
54*c1010edaSGreg Roach    {
55e3d81ebaSDavid Drury        $this->hue           = $hue;
56e3d81ebaSDavid Drury        $this->basehue       = $hue;
57e3d81ebaSDavid Drury        $this->saturation    = $saturation;
58e3d81ebaSDavid Drury        $this->lightness     = $lightness;
59e3d81ebaSDavid Drury        $this->baselightness = $lightness;
60e3d81ebaSDavid Drury        $this->alpha         = $alpha;
61e3d81ebaSDavid Drury        $this->range         = $range;
62e3d81ebaSDavid Drury    }
63e3d81ebaSDavid Drury
64e3d81ebaSDavid Drury    /**
65e3d81ebaSDavid Drury     * Function getNextColor
66e3d81ebaSDavid Drury     *
67e3d81ebaSDavid Drury     * $lightness cycles between $baselightness and 100% in $lightnessStep steps
68e3d81ebaSDavid Drury     * $hue cycles on each complete $lightness cycle
69e3d81ebaSDavid Drury     * between $basehue and $basehue + $range degrees in $hueStep degrees
70e3d81ebaSDavid Drury     *
71e3d81ebaSDavid Drury     * @param int $lightnessStep
72e3d81ebaSDavid Drury     * @param int $hueStep
7328c95b7aSGreg Roach     *
74e3d81ebaSDavid Drury     * @return string
75e3d81ebaSDavid Drury     */
76*c1010edaSGreg Roach    public function getNextColor($lightnessStep = 10, $hueStep = 15)
77*c1010edaSGreg Roach    {
78e3d81ebaSDavid Drury        $lightness = $this->lightness + $lightnessStep;
79e3d81ebaSDavid Drury        $hue       = $this->hue;
80e3d81ebaSDavid Drury
81e3d81ebaSDavid Drury        if ($lightness >= 100) {
82e3d81ebaSDavid Drury            $lightness = $this->baselightness;
830300c1d1SGreg Roach            if ($this->range > 0) {
840300c1d1SGreg Roach                $hue += $hueStep;
850300c1d1SGreg Roach            } else {
860300c1d1SGreg Roach                $hue -= $hueStep;
870300c1d1SGreg Roach            }
88e3d81ebaSDavid Drury            if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
89e3d81ebaSDavid Drury                $hue = $this->basehue;
90e3d81ebaSDavid Drury            }
91e3d81ebaSDavid Drury            $this->hue = $hue;
92e3d81ebaSDavid Drury        }
93e3d81ebaSDavid Drury        $this->lightness = $lightness;
94e3d81ebaSDavid Drury
957a6ee1acSGreg Roach        return sprintf('hsla(%s, %s%%, %s%%, %s)',
96e3d81ebaSDavid Drury            $this->hue,
97e3d81ebaSDavid Drury            $this->saturation,
98e3d81ebaSDavid Drury            $this->lightness,
99e3d81ebaSDavid Drury            $this->alpha);
100e3d81ebaSDavid Drury    }
101e3d81ebaSDavid Drury}
102