Beans_Lessc::funcToColor PHP Method

funcToColor() protected method

Convert the rgb, rgba, hsl color literals of function type as returned by the parser into values of color type.
protected funcToColor ( $func )
    protected function funcToColor($func)
    {
        $fname = $func[1];
        if ($func[2][0] != 'list') {
            return false;
        }
        // need a list of arguments
        $rawComponents = $func[2][2];
        if ($fname == 'hsl' || $fname == 'hsla') {
            $hsl = array('hsl');
            $i = 0;
            foreach ($rawComponents as $c) {
                $val = $this->reduce($c);
                $val = isset($val[1]) ? floatval($val[1]) : 0;
                if ($i == 0) {
                    $clamp = 360;
                } elseif ($i < 3) {
                    $clamp = 100;
                } else {
                    $clamp = 1;
                }
                $hsl[] = $this->clamp($val, $clamp);
                $i++;
            }
            while (count($hsl) < 4) {
                $hsl[] = 0;
            }
            return $this->toRGB($hsl);
        } elseif ($fname == 'rgb' || $fname == 'rgba') {
            $components = array();
            $i = 1;
            foreach ($rawComponents as $c) {
                $c = $this->reduce($c);
                if ($i < 4) {
                    if ($c[0] == "number" && $c[2] == "%") {
                        $components[] = 255 * ($c[1] / 100);
                    } else {
                        $components[] = floatval($c[1]);
                    }
                } elseif ($i == 4) {
                    if ($c[0] == "number" && $c[2] == "%") {
                        $components[] = 1.0 * ($c[1] / 100);
                    } else {
                        $components[] = floatval($c[1]);
                    }
                } else {
                    break;
                }
                $i++;
            }
            while (count($components) < 3) {
                $components[] = 0;
            }
            array_unshift($components, 'color');
            return $this->fixColor($components);
        }
        return false;
    }