src/Luminous/Utils/ColorUtils.php
public static function nearestColor($refLabs, $lab, $algorithm, $returnArrayKey = false, $cacheKey = "")
    {
        // Calculating a unique cache key takes even longer than calculating the distance, so the caller needs to craft
        // a cache key by hand
        $subKey = $lab[0] . $lab[1] . $lab[2];
        $result = array();
        if (empty($cacheKey) || !isset(static::$nearestColors[$cacheKey]) ||
            !isset(static::$nearestColors[$cacheKey][$subKey])) {
            // Nearest color not yet in cache, needs to be calculated
            $nearestColor = [0, 10000];
            foreach ($refLabs as $key => $refLab) {
                $distance = static::{$algorithm}($refLab, $lab);
                if ($distance < $nearestColor[1]) {
                    // New low
                    $nearestColor = [$key, $distance];
                }
            }
            $result = array($nearestColor[0], $refLabs[$nearestColor[0]]);
            if (!empty($cacheKey)) {
                // Put the result into the cache
                if (!isset(static::$nearestColors[$cacheKey])) {
                    static::$nearestColors[$cacheKey] = array();
                }
                static::$nearestColors[$cacheKey][$subKey] = $result;
            }
        }
        if (empty($result)) {
            // Fetch nearest color from cache
            $result = static::$nearestColors[$cacheKey][$subKey];
        }
        if ($returnArrayKey) {
            return $result[0];
        }
        return $result[1];
    }
public static function nearestColor($refLabs, $lab, $algorithm, $returnArrayKey = false, $cacheKey = "")
    {
        // Calculating a unique cache key takes even longer than calculating the distance, so the caller needs to craft
        // a cache key by hand
        $subKey = $lab[0] . $lab[1] . $lab[2];
        $result = array();
        if (empty($cacheKey) || !isset(static::$nearestColors[$cacheKey]) ||
            !isset(static::$nearestColors[$cacheKey][$subKey])) {
            // Nearest color not yet in cache, needs to be calculated
            $nearestColor = [0, 10000];
            foreach ($refLabs as $key => $refLab) {
                $distance = static::{$algorithm}($refLab, $lab);
                if ($distance < $nearestColor[1]) {
                    // New low
                    $nearestColor = [$key, $distance];
                }
            }
            $result = array($nearestColor[0], $refLabs[$nearestColor[0]]);
            if (!empty($cacheKey)) {
                // Put the result into the cache
                if (!isset(static::$nearestColors[$cacheKey])) {
                    static::$nearestColors[$cacheKey] = array();
                }
                static::$nearestColors[$cacheKey][$subKey] = $result;
            }
        }
        if (empty($result)) {
            // Fetch nearest color from cache
            $result = static::$nearestColors[$cacheKey][$subKey];
        }
        if ($returnArrayKey) {
            return $result[0];
        }
        return $result[1];
    }
public static function nearestColor ( $ refLabs , $ lab , $ algorithm , $ returnArrayKey = false , $ cacheKey = "" ) { // Calculating a unique cache key takes even longer than calculating the distance, so the caller needs to craft // a cache key by hand $ subKey = $ lab [ 0 ] . $ lab [ 1 ] . $ lab [ 2 ] ; $ result = array ( ) ; if ( empty ( $ cacheKey ) || ! isset ( static :: $ nearestColors [ $ cacheKey ] ) || ! isset ( static :: $ nearestColors [ $ cacheKey ] [ $ subKey ] ) ) { // Nearest color not yet in cache, needs to be calculated $ nearestColor = [ 0 , 10000 ] ; foreach ( $ refLabs as $ key => $ refLab ) { $ distance = static :: { $ algorithm } ( $ refLab , $ lab ) ; if ( $ distance < $ nearestColor [ 1 ] ) { // New low $ nearestColor = [ $ key , $ distance ] ; } } $ result = array ( $ nearestColor [ 0 ] , $ refLabs [ $ nearestColor [ 0 ] ] ) ; if ( ! empty ( $ cacheKey ) ) { // Put the result into the cache if ( ! isset ( static :: $ nearestColors [ $ cacheKey ] ) ) { static :: $ nearestColors [ $ cacheKey ] = array ( ) ; } static :: $ nearestColors [ $ cacheKey ] [ $ subKey ] = $ result ; } } if ( empty ( $ result ) ) { // Fetch nearest color from cache $ result = static :: $ nearestColors [ $ cacheKey ] [ $ subKey ] ; } if ( $ returnArrayKey ) { return $ result [ 0 ] ; } return $ result [ 1 ] ; }
Searches a list of colors for the one with the shortest distance to a given color
Searches a list of colors for the one with the shortest distance to a given color