xref: /webtrees/app/ColorGenerator.php (revision d11be7027e34e3121be11cc025421873364403f9)
1e3d81ebaSDavid Drury<?php
23976b470SGreg Roach
3e3d81ebaSDavid Drury/**
4e3d81ebaSDavid Drury * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg 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{
2743f2f523SGreg Roach    private int $hue;
2828c95b7aSGreg Roach
2943f2f523SGreg Roach    private int $basehue;
3028c95b7aSGreg Roach
3143f2f523SGreg Roach    private int $saturation;
3228c95b7aSGreg Roach
3343f2f523SGreg Roach    private int $lightness;
3428c95b7aSGreg Roach
3543f2f523SGreg Roach    private int $baselightness;
3628c95b7aSGreg Roach
3743f2f523SGreg Roach    private float $alpha;
3828c95b7aSGreg Roach
3943f2f523SGreg Roach    private int $range;
40e3d81ebaSDavid Drury
41e3d81ebaSDavid Drury    /**
4276692c8bSGreg Roach     * Create a color generator.
4376692c8bSGreg Roach     *
447c7e32daSGreg Roach     * @param int   $hue        0Deg = Red, 120Deg = green, 240Deg = blue)
4528c95b7aSGreg Roach     * @param int   $saturation
4628c95b7aSGreg Roach     * @param int   $lightness
477c7e32daSGreg Roach     * @param float $alpha
487c7e32daSGreg Roach     * @param int   $range      sign determines direction. positive = clockwise, negative = anticlockwise
49e3d81ebaSDavid Drury     */
507c7e32daSGreg Roach    public function __construct(int $hue, int $saturation, int $lightness, float $alpha, int $range)
51c1010edaSGreg Roach    {
52e3d81ebaSDavid Drury        $this->hue           = $hue;
53e3d81ebaSDavid Drury        $this->basehue       = $hue;
54e3d81ebaSDavid Drury        $this->saturation    = $saturation;
55e3d81ebaSDavid Drury        $this->lightness     = $lightness;
56e3d81ebaSDavid Drury        $this->baselightness = $lightness;
57e3d81ebaSDavid Drury        $this->alpha         = $alpha;
58e3d81ebaSDavid Drury        $this->range         = $range;
59e3d81ebaSDavid Drury    }
60e3d81ebaSDavid Drury
61e3d81ebaSDavid Drury    /**
62e3d81ebaSDavid Drury     * Function getNextColor
63e3d81ebaSDavid Drury     *
64e3d81ebaSDavid Drury     * $lightness cycles between $baselightness and 100% in $lightnessStep steps
65e3d81ebaSDavid Drury     * $hue cycles on each complete $lightness cycle
66e3d81ebaSDavid Drury     * between $basehue and $basehue + $range degrees in $hueStep degrees
67e3d81ebaSDavid Drury     *
68e3d81ebaSDavid Drury     * @param int $lightnessStep
69e3d81ebaSDavid Drury     * @param int $hueStep
7028c95b7aSGreg Roach     *
71e3d81ebaSDavid Drury     * @return string
72e3d81ebaSDavid Drury     */
737c7e32daSGreg Roach    public function getNextColor(int $lightnessStep = 10, int $hueStep = 15): string
74c1010edaSGreg Roach    {
75e3d81ebaSDavid Drury        $lightness = $this->lightness + $lightnessStep;
76e3d81ebaSDavid Drury        $hue       = $this->hue;
77e3d81ebaSDavid Drury
78e3d81ebaSDavid Drury        if ($lightness >= 100) {
79e3d81ebaSDavid Drury            $lightness = $this->baselightness;
800300c1d1SGreg Roach            if ($this->range > 0) {
810300c1d1SGreg Roach                $hue += $hueStep;
820300c1d1SGreg Roach            } else {
830300c1d1SGreg Roach                $hue -= $hueStep;
840300c1d1SGreg Roach            }
85e3d81ebaSDavid Drury            if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) {
86e3d81ebaSDavid Drury                $hue = $this->basehue;
87e3d81ebaSDavid Drury            }
88e3d81ebaSDavid Drury            $this->hue = $hue;
89e3d81ebaSDavid Drury        }
90e3d81ebaSDavid Drury        $this->lightness = $lightness;
91e3d81ebaSDavid Drury
927c7e32daSGreg Roach        return sprintf('hsla(%d, %d%%, %d%%, %0.2f)', $this->hue, $this->saturation, $this->lightness, $this->alpha);
93e3d81ebaSDavid Drury    }
94e3d81ebaSDavid Drury}
95