/** * Given a color, we determine which predefined color in `static::$_colors`best match. * * {{{ * use imageColor\util\Color; * use MischiefCollective\ColorJizz\Formats\RGB; * * $red = new RGB(255, 128, 128); * var_dump(Color::bestColor($red)); // light-red * }}} * * @param Color $color * @param array $options Array of optinos to overwrite: * - `'threshold'`: Set minimum threshold for matching. * @return string|false If no numbers are below the threshold, false will be returned. */ public static function bestColor(ColorJizz $color, array $options = array()) { $options += array('threshold' => 30); $min = 9999; $bestColor = null; static::updateColors(); foreach (static::$_colors as $key => $value) { $tmin = $color->distance($value); if ($min > $tmin) { $bestColor = $key; $min = $tmin; } } return $min < $options['threshold'] ? $bestColor : false; }
/** * Find the distance to the destination color * * @param MischiefCollective\ColorJizz\ColorJizz $destinationColor The destination color * * @return int distance to destination color */ public function distance(ColorJizz $destinationColor) { $a = $this->toCIELab(); $b = $destinationColor->toCIELab(); $lightness_pow = pow($a->lightness - $b->lightness, 2); $a_dimension_pow = pow($a->a_dimension - $b->a_dimension, 2); $b_dimension_pow = pow($a->b_dimension - $b->b_dimension, 2); return sqrt($lightness_pow + $a_dimension_pow + $b_dimension_pow); }