1e3d81ebaSDavid Drury<?php 2e3d81ebaSDavid Drury/** 3e3d81ebaSDavid Drury * webtrees: online genealogy 4*8fcd0d32SGreg Roach * Copyright (C) 2019 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 */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees; 19e3d81ebaSDavid Drury 20e3d81ebaSDavid Drury/** 2176692c8bSGreg Roach * Generate a range of colurs for the lifespan chart. 22e3d81ebaSDavid Drury */ 23c1010edaSGreg Roachclass ColorGenerator 24c1010edaSGreg Roach{ 2576692c8bSGreg Roach /** @var int Current hue */ 26e3d81ebaSDavid Drury private $hue; 2728c95b7aSGreg Roach 2876692c8bSGreg Roach /** @var int Initial hue */ 29e3d81ebaSDavid Drury private $basehue; 3028c95b7aSGreg Roach 3176692c8bSGreg Roach /** @var int Saturation */ 32e3d81ebaSDavid Drury private $saturation; 3328c95b7aSGreg Roach 3476692c8bSGreg Roach /** @var int Lightness */ 35e3d81ebaSDavid Drury private $lightness; 3628c95b7aSGreg Roach 3776692c8bSGreg Roach /** @var int Initial lightness */ 38e3d81ebaSDavid Drury private $baselightness; 3928c95b7aSGreg Roach 407c7e32daSGreg Roach /** @var float Alpha transparancy */ 41e3d81ebaSDavid Drury private $alpha; 4228c95b7aSGreg Roach 4376692c8bSGreg Roach /** @var int Clockwise or anticlockwise color wheel */ 44e3d81ebaSDavid Drury private $range; 45e3d81ebaSDavid Drury 46e3d81ebaSDavid Drury /** 4776692c8bSGreg Roach * Create a color generator. 4876692c8bSGreg Roach * 497c7e32daSGreg Roach * @param int $hue 0Deg = Red, 120Deg = green, 240Deg = blue) 5028c95b7aSGreg Roach * @param int $saturation 5128c95b7aSGreg Roach * @param int $lightness 527c7e32daSGreg Roach * @param float $alpha 537c7e32daSGreg Roach * @param int $range sign determines direction. positive = clockwise, negative = anticlockwise 54e3d81ebaSDavid Drury */ 557c7e32daSGreg Roach public function __construct(int $hue, int $saturation, int $lightness, float $alpha, int $range) 56c1010edaSGreg Roach { 57e3d81ebaSDavid Drury $this->hue = $hue; 58e3d81ebaSDavid Drury $this->basehue = $hue; 59e3d81ebaSDavid Drury $this->saturation = $saturation; 60e3d81ebaSDavid Drury $this->lightness = $lightness; 61e3d81ebaSDavid Drury $this->baselightness = $lightness; 62e3d81ebaSDavid Drury $this->alpha = $alpha; 63e3d81ebaSDavid Drury $this->range = $range; 64e3d81ebaSDavid Drury } 65e3d81ebaSDavid Drury 66e3d81ebaSDavid Drury /** 67e3d81ebaSDavid Drury * Function getNextColor 68e3d81ebaSDavid Drury * 69e3d81ebaSDavid Drury * $lightness cycles between $baselightness and 100% in $lightnessStep steps 70e3d81ebaSDavid Drury * $hue cycles on each complete $lightness cycle 71e3d81ebaSDavid Drury * between $basehue and $basehue + $range degrees in $hueStep degrees 72e3d81ebaSDavid Drury * 73e3d81ebaSDavid Drury * @param int $lightnessStep 74e3d81ebaSDavid Drury * @param int $hueStep 7528c95b7aSGreg Roach * 76e3d81ebaSDavid Drury * @return string 77e3d81ebaSDavid Drury */ 787c7e32daSGreg Roach public function getNextColor(int $lightnessStep = 10, int $hueStep = 15): string 79c1010edaSGreg Roach { 80e3d81ebaSDavid Drury $lightness = $this->lightness + $lightnessStep; 81e3d81ebaSDavid Drury $hue = $this->hue; 82e3d81ebaSDavid Drury 83e3d81ebaSDavid Drury if ($lightness >= 100) { 84e3d81ebaSDavid Drury $lightness = $this->baselightness; 850300c1d1SGreg Roach if ($this->range > 0) { 860300c1d1SGreg Roach $hue += $hueStep; 870300c1d1SGreg Roach } else { 880300c1d1SGreg Roach $hue -= $hueStep; 890300c1d1SGreg Roach } 90e3d81ebaSDavid Drury if (($hue - $this->basehue) * ($hue - ($this->basehue + $this->range)) >= 0) { 91e3d81ebaSDavid Drury $hue = $this->basehue; 92e3d81ebaSDavid Drury } 93e3d81ebaSDavid Drury $this->hue = $hue; 94e3d81ebaSDavid Drury } 95e3d81ebaSDavid Drury $this->lightness = $lightness; 96e3d81ebaSDavid Drury 977c7e32daSGreg Roach return sprintf('hsla(%d, %d%%, %d%%, %0.2f)', $this->hue, $this->saturation, $this->lightness, $this->alpha); 98e3d81ebaSDavid Drury } 99e3d81ebaSDavid Drury} 100