From: Vincent Vanwaelscappel Date: Tue, 2 May 2023 09:13:50 +0000 (+0200) Subject: wip #5902 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=7eac111f5f462343fd51201b1e88e1a4e593b013;p=cubist_util.git wip #5902 --- diff --git a/src/Graphics/Color.php b/src/Graphics/Color.php index 131bbed..31fe8bd 100644 --- a/src/Graphics/Color.php +++ b/src/Graphics/Color.php @@ -176,18 +176,32 @@ class Color return 1 - $diff; } + /** + * @param $c string + * @return string + */ public function toHexComposante($c) { $res = dechex(self::_to255($this->{'_' . $c})); - if (strlen($res) == 2) { + $len = strlen($res); + if ($len == 2) { return $res; - } elseif (strlen($res) == 0) { + } elseif ($len == 0) { return '00'; - } elseif (strlen($res) == 1) { + } elseif ($len == 1) { return '0' . $res; } } + /** + * @param $c string + * @return int + */ + public function toIntComposante($c) + { + return self::_to255($this->{'_' . $c}); + } + public function toArray() { return array( @@ -250,4 +264,16 @@ class Color } return ['red' => $r, 'green' => $g, 'blue' => $b,]; } + + /** + * @param Color $color + * @return int + */ + public function distance(Color $color) + { + + return (30 * ($color->toIntComposante('red') - $this->toIntComposante('red'))) ** 2 + + (59 * ($color->toIntComposante('green') - $this->toIntComposante('green'))) ** 2 + + (11 * ($color->toIntComposante('blue') - $this->toIntComposante('blue'))) ** 2; + } }