xref: /webtrees/app/ColorGenerator.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
1e3d81ebaSDavid Drury<?php
2*3976b470SGreg Roach
3e3d81ebaSDavid Drury/**
4e3d81ebaSDavid Drury * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
6e3d81ebaSDavid Drury * This program is free software: you can redistribute it and/or modify
7e3d81ebaSDavid Drury * it under the terms of the GNU General Public License as published by
8e3d81ebaSDavid Drury * the Free Software Foundation, either version 3 of the License, or
9e3d81ebaSDavid Drury * (at your option) any later version.
10e3d81ebaSDavid Drury * This program is distributed in the hope that it will be useful,
11e3d81ebaSDavid Drury * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e3d81ebaSDavid Drury * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e3d81ebaSDavid Drury * GNU General Public License for more details.
14e3d81ebaSDavid Drury * You should have received a copy of the GNU General Public License
15e3d81ebaSDavid Drury * along with this program. If not, see <http://www.gnu.org/licenses/>.
16e3d81ebaSDavid Drury */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees;
20e3d81ebaSDavid Drury
21e3d81ebaSDavid Drury/**
2276692c8bSGreg Roach * Generate a range of colurs for the lifespan chart.
23e3d81ebaSDavid Drury */
24c1010edaSGreg Roachclass ColorGenerator
25c1010edaSGreg Roach{
2676692c8bSGreg Roach    /** @var int Current hue */
27e3d81ebaSDavid Drury    private $hue;
2828c95b7aSGreg Roach
2976692c8bSGreg Roach    /** @var int Initial hue */
30e3d81ebaSDavid Drury    private $basehue;
3128c95b7aSGreg Roach
3276692c8bSGreg Roach    /** @var int Saturation */
33e3d81ebaSDavid Drury    private $saturation;
3428c95b7aSGreg Roach
3576692c8bSGreg Roach    /** @var int Lightness */
36e3d81ebaSDavid Drury    private $lightness;
3728c95b7aSGreg Roach
3876692c8bSGreg Roach    /** @var int Initial lightness */
39e3d81ebaSDavid Drury    private $baselightness;
4028c95b7aSGreg Roach
417c7e32daSGreg Roach    /** @var float Alpha transparancy */
42e3d81ebaSDavid Drury    private $alpha;
4328c95b7aSGreg Roach
4476692c8bSGreg Roach    /** @var int Clockwise or anticlockwise color wheel */
45e3d81ebaSDavid Drury    private $range;
46e3d81ebaSDavid Drury
47e3d81ebaSDavid Drury    /**
4876692c8bSGreg Roach     * Create a color generator.
4976692c8bSGreg Roach     *
507c7e32daSGreg Roach     * @param int   $hue        0Deg = Red, 120Deg = green, 240Deg = blue)
5128c95b7aSGreg Roach     * @param int   $saturation
5228c95b7aSGreg Roach     * @param int   $lightness
537c7e32daSGreg Roach     * @param float $alpha
547c7e32daSGreg Roach     * @param int   $range      sign determines direction. positive = clockwise, negative = anticlockwise
55e3d81ebaSDavid Drury     */
567c7e32daSGreg Roach    public function __construct(int $hue, int $saturation, int $lightness, float $alpha, int $range)
57c1010edaSGreg Roach    {
58e3d81ebaSDavid Drury        $this->hue           = $hue;
59e3d81ebaSDavid Drury        $this->basehue       = $hue;
60e3d81ebaSDavid Drury        $this->saturation    = $saturation;
61e3d81ebaSDavid Drury        $this->lightness     = $lightness;
62e3d81ebaSDavid Drury        $this->baselightness = $lightness;
63e3d81ebaSDavid Drury        $this->alpha         = $alpha;
64e3d81ebaSDavid Drury        $this->range         = $range;
65e3d81ebaSDavid Drury    }
66e3d81ebaSDavid Drury
67e3d81ebaSDavid Drury    /**
68e3d81ebaSDavid Drury     * Function getNextColor
69e3d81ebaSDavid Drury     *
70e3d81ebaSDavid Drury     * $lightness cycles between $baselightness and 100% in $lightnessStep steps
71e3d81ebaSDavid Drury     * $hue cycles on each complete $lightness cycle
72e3d81ebaSDavid Drury     * between $basehue and $basehue + $range degrees in $hueStep degrees
73e3d81ebaSDavid Drury     *
74e3d81ebaSDavid Drury     * @param int $lightnessStep
75e3d81ebaSDavid Drury     * @param int $hueStep
7628c95b7aSGreg Roach     *
77e3d81ebaSDavid Drury     * @return string
78e3d81ebaSDavid Drury     */
797c7e32daSGreg Roach    public function getNextColor(int $lightnessStep = 10, int $hueStep = 15): string
80c1010edaSGreg Roach    {
81e3d81ebaSDavid Drury        $lightness = $this->lightness + $lightnessStep;
82e3d81ebaSDavid Drury        $hue       = $this->hue;
83e3d81ebaSDavid Drury
84e3d81ebaSDavid Drury        if ($lightness >= 100) {
85e3d81ebaSDavid Drury            $lightness = $this->baselightness;
860300c1d1SGreg Roach            if ($this->range > 0) {
870300c1d1SGreg Roach                $hue += $hueStep;
880300c1d1SGreg Roach            } else {
890300c1d1SGreg Roach                $hue -= $hueStep;
900300c1d1SGreg Roach            }
91e3d81ebaSDavid Drury            if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
92e3d81ebaSDavid Drury                $hue = $this->basehue;
93e3d81ebaSDavid Drury            }
94e3d81ebaSDavid Drury            $this->hue = $hue;
95e3d81ebaSDavid Drury        }
96e3d81ebaSDavid Drury        $this->lightness = $lightness;
97e3d81ebaSDavid Drury
987c7e32daSGreg Roach        return sprintf('hsla(%d, %d%%, %d%%, %0.2f)', $this->hue, $this->saturation, $this->lightness, $this->alpha);
99e3d81ebaSDavid Drury    }
100e3d81ebaSDavid Drury}
101