DataTrait.php
2 years ago
FormatTrait.php
6 months ago
Picture.php
6 months ago
PointTrait.php
2 years ago
StyleTrait.php
2 years ago
StyleTrait.php
204 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Davaxi\Sparkline; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | /** |
| 7 | * Trait StyleTrait. |
| 8 | */ |
| 9 | trait StyleTrait |
| 10 | { |
| 11 | /** |
| 12 | * RGB |
| 13 | * Default: #ffffff |
| 14 | * @var int[] |
| 15 | */ |
| 16 | protected $backgroundColor = [255, 255, 255]; |
| 17 | /** |
| 18 | * RGB |
| 19 | * Default: #1388db |
| 20 | * @var int[][] |
| 21 | */ |
| 22 | protected $lineColor = [[19, 136, 219]]; |
| 23 | /** |
| 24 | * RGB |
| 25 | * Default: #e6f2fa |
| 26 | * @var int[][] |
| 27 | */ |
| 28 | protected $fillColor = [[230, 242, 250]]; |
| 29 | /** |
| 30 | * Default: 1.75px |
| 31 | * @var float |
| 32 | */ |
| 33 | protected $lineThickness = 1.75; |
| 34 | /** |
| 35 | * Set background to transparent. |
| 36 | */ |
| 37 | public function deactivateBackgroundColor() |
| 38 | { |
| 39 | $this->backgroundColor = []; |
| 40 | } |
| 41 | /** |
| 42 | * @param string $color (hexadecimal) |
| 43 | */ |
| 44 | public function setBackgroundColorHex(string $color) |
| 45 | { |
| 46 | list($red, $green, $blue) = $this->colorHexToRGB($color); |
| 47 | $this->setBackgroundColorRGB($red, $green, $blue); |
| 48 | } |
| 49 | /** |
| 50 | * @param int $red |
| 51 | * @param int $green |
| 52 | * @param int $blue |
| 53 | */ |
| 54 | public function setBackgroundColorRGB(int $red, int $green, int $blue) |
| 55 | { |
| 56 | $this->backgroundColor = [$red, $green, $blue]; |
| 57 | } |
| 58 | /** |
| 59 | * @param string $color (hexadecimal) |
| 60 | * @param int $seriesIndex |
| 61 | */ |
| 62 | public function setLineColorHex(string $color, int $seriesIndex = 0) |
| 63 | { |
| 64 | list($red, $green, $blue) = $this->colorHexToRGB($color); |
| 65 | $this->setLineColorRGB($red, $green, $blue, $seriesIndex); |
| 66 | } |
| 67 | /** |
| 68 | * @param int $red |
| 69 | * @param int $green |
| 70 | * @param int $blue |
| 71 | * @param int $seriesIndex |
| 72 | */ |
| 73 | public function setLineColorRGB(int $red, int $green, int $blue, int $seriesIndex = 0) |
| 74 | { |
| 75 | $this->lineColor[$seriesIndex] = [$red, $green, $blue]; |
| 76 | } |
| 77 | /** |
| 78 | * @param float $thickness (in px) |
| 79 | */ |
| 80 | public function setLineThickness(float $thickness) |
| 81 | { |
| 82 | $this->lineThickness = $thickness; |
| 83 | } |
| 84 | /** |
| 85 | * @param int $seriesIndex |
| 86 | * @return array |
| 87 | */ |
| 88 | public function getLineColor(int $seriesIndex = 0) : array |
| 89 | { |
| 90 | return $this->lineColor[$seriesIndex] ?? $this->lineColor[0]; |
| 91 | } |
| 92 | /** |
| 93 | * Set fill color to transparent. |
| 94 | * @param int $seriesIndex |
| 95 | */ |
| 96 | public function deactivateFillColor(int $seriesIndex = 0) |
| 97 | { |
| 98 | unset($this->fillColor[$seriesIndex]); |
| 99 | } |
| 100 | /** |
| 101 | * Set all fill color to transparent. |
| 102 | * @return void |
| 103 | */ |
| 104 | public function deactivateAllFillColor() |
| 105 | { |
| 106 | $this->fillColor = []; |
| 107 | } |
| 108 | /** |
| 109 | * @param string $color (hexadecimal) |
| 110 | * @param int $seriesIndex |
| 111 | */ |
| 112 | public function setFillColorHex(string $color, int $seriesIndex = 0) |
| 113 | { |
| 114 | list($red, $green, $blue) = $this->colorHexToRGB($color); |
| 115 | $this->setFillColorRGB($red, $green, $blue, $seriesIndex); |
| 116 | } |
| 117 | /** |
| 118 | * @param int $red |
| 119 | * @param int $green |
| 120 | * @param int $blue |
| 121 | * @param int $seriesIndex |
| 122 | */ |
| 123 | public function setFillColorRGB(int $red, int $green, int $blue, int $seriesIndex = 0) |
| 124 | { |
| 125 | $this->fillColor[$seriesIndex] = [$red, $green, $blue]; |
| 126 | } |
| 127 | /** |
| 128 | * @param int $seriesIndex |
| 129 | * @return array |
| 130 | */ |
| 131 | public function getFillColor(int $seriesIndex = 0) : array |
| 132 | { |
| 133 | if (!isset($this->fillColor[$seriesIndex])) { |
| 134 | return []; |
| 135 | } |
| 136 | return $this->fillColor[$seriesIndex]; |
| 137 | } |
| 138 | /** |
| 139 | * @param string $color (hexadecimal) |
| 140 | * @exceptions \InvalidArgumentException |
| 141 | * |
| 142 | * @return array (r,g,b) |
| 143 | */ |
| 144 | protected function colorHexToRGB(string $color) : array |
| 145 | { |
| 146 | if (!$this->checkColorHex($color)) { |
| 147 | throw new InvalidArgumentException('Invalid hexadecimal value ' . $color); |
| 148 | } |
| 149 | $color = mb_strtolower($color); |
| 150 | $color = ltrim($color, '#'); |
| 151 | if (mb_strlen($color) === static::HEXADECIMAL_ALIAS_LENGTH) { |
| 152 | $color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2]; |
| 153 | } |
| 154 | $color = hexdec($color); |
| 155 | return [ |
| 156 | 0xff & $color >> 0x10, |
| 157 | // Red |
| 158 | 0xff & $color >> 0x8, |
| 159 | // Green |
| 160 | 0xff & $color, |
| 161 | ]; |
| 162 | } |
| 163 | /** |
| 164 | * Formats: |
| 165 | * all |
| 166 | * vertical horizontal |
| 167 | * top horizontal bottom |
| 168 | * top right bottom left. |
| 169 | * |
| 170 | * @param string $padding |
| 171 | * |
| 172 | * @return array |
| 173 | */ |
| 174 | protected function paddingStringToArray(string $padding) : array |
| 175 | { |
| 176 | $parts = explode(' ', $padding); |
| 177 | switch (count($parts)) { |
| 178 | case static::CSS_PADDING_ONE: |
| 179 | $value = (float) $parts[0]; |
| 180 | return [$value, $value, $value, $value]; |
| 181 | case static::CSS_PADDING_TWO: |
| 182 | $verticalValue = (float) $parts[0]; |
| 183 | $horizontalValue = (float) $parts[1]; |
| 184 | return [$verticalValue, $horizontalValue, $verticalValue, $horizontalValue]; |
| 185 | case static::CSS_PADDING_THREE: |
| 186 | $parts[3] = $parts[1]; |
| 187 | return $parts; |
| 188 | case static::CSS_PADDING: |
| 189 | return $parts; |
| 190 | default: |
| 191 | throw new InvalidArgumentException('Invalid padding format'); |
| 192 | } |
| 193 | } |
| 194 | /** |
| 195 | * @param $color |
| 196 | * |
| 197 | * @return int |
| 198 | */ |
| 199 | protected static function checkColorHex($color) : int |
| 200 | { |
| 201 | return preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/i', $color); |
| 202 | } |
| 203 | } |
| 204 |