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(
}
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;
+ }
}