xref: /webtrees/app/ColorGenerator.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1e3d81ebaSDavid Drury<?php
23976b470SGreg Roach
3e3d81ebaSDavid Drury/**
4e3d81ebaSDavid Drury * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16e3d81ebaSDavid Drury */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
21e3d81ebaSDavid Drury
22e3d81ebaSDavid Drury/**
2376692c8bSGreg Roach * Generate a range of colurs for the lifespan chart.
24e3d81ebaSDavid Drury */
25c1010edaSGreg Roachclass ColorGenerator
26c1010edaSGreg Roach{
2776692c8bSGreg Roach    /** @var int Current hue */
28e3d81ebaSDavid Drury    private $hue;
2928c95b7aSGreg Roach
3076692c8bSGreg Roach    /** @var int Initial hue */
31e3d81ebaSDavid Drury    private $basehue;
3228c95b7aSGreg Roach
3376692c8bSGreg Roach    /** @var int Saturation */
34e3d81ebaSDavid Drury    private $saturation;
3528c95b7aSGreg Roach
3676692c8bSGreg Roach    /** @var int Lightness */
37e3d81ebaSDavid Drury    private $lightness;
3828c95b7aSGreg Roach
3976692c8bSGreg Roach    /** @var int Initial lightness */
40e3d81ebaSDavid Drury    private $baselightness;
4128c95b7aSGreg Roach
427c7e32daSGreg Roach    /** @var float Alpha transparancy */
43e3d81ebaSDavid Drury    private $alpha;
4428c95b7aSGreg Roach
4576692c8bSGreg Roach    /** @var int Clockwise or anticlockwise color wheel */
46e3d81ebaSDavid Drury    private $range;
47e3d81ebaSDavid Drury
48e3d81ebaSDavid Drury    /**
4976692c8bSGreg Roach     * Create a color generator.
5076692c8bSGreg Roach     *
517c7e32daSGreg Roach     * @param int   $hue        0Deg = Red, 120Deg = green, 240Deg = blue)
5228c95b7aSGreg Roach     * @param int   $saturation
5328c95b7aSGreg Roach     * @param int   $lightness
547c7e32daSGreg Roach     * @param float $alpha
557c7e32daSGreg Roach     * @param int   $range      sign determines direction. positive = clockwise, negative = anticlockwise
56e3d81ebaSDavid Drury     */
577c7e32daSGreg Roach    public function __construct(int $hue, int $saturation, int $lightness, float $alpha, int $range)
58c1010edaSGreg Roach    {
59e3d81ebaSDavid Drury        $this->hue           = $hue;
60e3d81ebaSDavid Drury        $this->basehue       = $hue;
61e3d81ebaSDavid Drury        $this->saturation    = $saturation;
62e3d81ebaSDavid Drury        $this->lightness     = $lightness;
63e3d81ebaSDavid Drury        $this->baselightness = $lightness;
64e3d81ebaSDavid Drury        $this->alpha         = $alpha;
65e3d81ebaSDavid Drury        $this->range         = $range;
66e3d81ebaSDavid Drury    }
67e3d81ebaSDavid Drury
68e3d81ebaSDavid Drury    /**
69e3d81ebaSDavid Drury     * Function getNextColor
70e3d81ebaSDavid Drury     *
71e3d81ebaSDavid Drury     * $lightness cycles between $baselightness and 100% in $lightnessStep steps
72e3d81ebaSDavid Drury     * $hue cycles on each complete $lightness cycle
73e3d81ebaSDavid Drury     * between $basehue and $basehue + $range degrees in $hueStep degrees
74e3d81ebaSDavid Drury     *
75e3d81ebaSDavid Drury     * @param int $lightnessStep
76e3d81ebaSDavid Drury     * @param int $hueStep
7728c95b7aSGreg Roach     *
78e3d81ebaSDavid Drury     * @return string
79e3d81ebaSDavid Drury     */
807c7e32daSGreg Roach    public function getNextColor(int $lightnessStep = 10, int $hueStep = 15): string
81c1010edaSGreg Roach    {
82e3d81ebaSDavid Drury        $lightness = $this->lightness + $lightnessStep;
83e3d81ebaSDavid Drury        $hue       = $this->hue;
84e3d81ebaSDavid Drury
85e3d81ebaSDavid Drury        if ($lightness >= 100) {
86e3d81ebaSDavid Drury            $lightness = $this->baselightness;
870300c1d1SGreg Roach            if ($this->range > 0) {
880300c1d1SGreg Roach                $hue += $hueStep;
890300c1d1SGreg Roach            } else {
900300c1d1SGreg Roach                $hue -= $hueStep;
910300c1d1SGreg Roach            }
92e3d81ebaSDavid Drury            if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
93e3d81ebaSDavid Drury                $hue = $this->basehue;
94e3d81ebaSDavid Drury            }
95e3d81ebaSDavid Drury            $this->hue = $hue;
96e3d81ebaSDavid Drury        }
97e3d81ebaSDavid Drury        $this->lightness = $lightness;
98e3d81ebaSDavid Drury
997c7e32daSGreg Roach        return sprintf('hsla(%d, %d%%, %d%%, %0.2f)', $this->hue, $this->saturation, $this->lightness, $this->alpha);
100e3d81ebaSDavid Drury    }
101e3d81ebaSDavid Drury}
102