From 98d964adb90c08c0ac2d4cb5e1c7bc7d131149ea Mon Sep 17 00:00:00 2001 From: JCOGS Design <13821249+jcogs-design@users.noreply.github.com> Date: Tue, 7 Jun 2022 19:07:53 +0100 Subject: [PATCH] Remove some php 8.1 deprecation warnings Replace some implicit float->int conversions with use of round(x,0) --- src/Color.php | 101 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 18 deletions(-) diff --git a/src/Color.php b/src/Color.php index 5efee50..4392a1b 100644 --- a/src/Color.php +++ b/src/Color.php @@ -1,4 +1,5 @@ 255 ? 255 : $color; + $color = $color < 0 ? 0 : $color; + } + + //Normalise alpha value if necessary + $alpha = $alpha > 1 ? 1 : $alpha; + $alpha = $alpha < 0 ? 0 : $alpha; + + // Flip from CSS opacity to GD alpha form + $alpha = (1 - $alpha) * 127; + + // Expand $rgb + list($r, $g, $b) = $rgb; + + return new Color($r, $g, $b, $alpha); } /** @@ -91,8 +138,8 @@ public static function fromHsl($h, $s, $l) // Check http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSL for details $chroma = (1 - abs(2 * $l - 1)) * $s; $h_ = $h * 6; - $x = $chroma * (1 - abs((fmod($h_,2)) - 1)); // Note: fmod because % (modulo) returns int value!! - $m = $l - round($chroma/2, 10); // Bugfix for strange float behaviour (e.g. $l=0.17 and $s=1) + $x = $chroma * (1 - abs((fmod($h_, 2)) - 1)); // Note: fmod because % (modulo) returns int value!! + $m = $l - round($chroma / 2, 10); // Bugfix for strange float behaviour (e.g. $l=0.17 and $s=1) if ($h_ >= 0 && $h_ < 1) $rgb = array(($chroma + $m), ($x + $m), $m); elseif ($h_ >= 1 && $h_ < 2) $rgb = array(($x + $m), ($chroma + $m), $m); @@ -106,7 +153,7 @@ public static function fromHsl($h, $s, $l) } /** - * @param resource $image GD image resource + * @param resource|\GdImage $image GD image resource * @return int Returns the index of the specified color+alpha in the palette of the image, * or index of allocated color if the color does not exist in the image's palette. */ @@ -114,9 +161,18 @@ public function getIndex($image) { $index = $this->hasAlphaChannel() ? imagecolorexactalpha( - $image, $this->red, $this->green, $this->blue, $this->alpha) + $image, + round($this->red,0), + round($this->green,0), + round($this->blue,0), + round($this->alpha,0) + ) : imagecolorexact( - $image, $this->red, $this->green, $this->blue); + $image, + round($this->red,0), + round($this->green,0), + round($this->blue,0) + ); if ($index !== -1) { return $index; @@ -124,9 +180,18 @@ public function getIndex($image) return $this->hasAlphaChannel() ? imagecolorallocatealpha( - $image, $this->red, $this->green, $this->blue, $this->alpha) + $image, + round($this->red,0), + round($this->green,0), + round($this->blue,0), + round($this->alpha,0) + ) : imagecolorallocate( - $image, $this->red, $this->green, $this->blue); + $image, + round($this->red,0), + round($this->green,0), + round($this->blue,0), + ); } /** @@ -144,4 +209,4 @@ public function toArray() { return array($this->red, $this->green, $this->blue); } -} \ No newline at end of file +}