Automattic\WooCommerce\Vendor\Sabberworm\CSS\Value

Color::parsepublic staticWC 1.0

Метод класса: Color{}

Хуков нет.

Возвращает

Color|CSSFunction.

Использование

$result = Color::parse( $oParserState, $bIgnoreCase );
$oParserState(ParserState) (обязательный)
.
$bIgnoreCase(true|false)
.
По умолчанию: false

Код Color::parse() WC 10.4.3

public static function parse(ParserState $oParserState, $bIgnoreCase = false)
{
    $aColor = [];
    if ($oParserState->comes('#')) {
        $oParserState->consume('#');
        $sValue = $oParserState->parseIdentifier(false);
        if ($oParserState->strlen($sValue) === 3) {
            $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2];
        } elseif ($oParserState->strlen($sValue) === 4) {
            $sValue = $sValue[0] . $sValue[0] . $sValue[1] . $sValue[1] . $sValue[2] . $sValue[2] . $sValue[3]
                . $sValue[3];
        }

        if ($oParserState->strlen($sValue) === 8) {
            $aColor = [
                'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
                'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
                'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
                'a' => new Size(
                    round(self::mapRange(intval($sValue[6] . $sValue[7], 16), 0, 255, 0, 1), 2),
                    null,
                    true,
                    $oParserState->currentLine()
                ),
            ];
        } elseif ($oParserState->strlen($sValue) === 6) {
            $aColor = [
                'r' => new Size(intval($sValue[0] . $sValue[1], 16), null, true, $oParserState->currentLine()),
                'g' => new Size(intval($sValue[2] . $sValue[3], 16), null, true, $oParserState->currentLine()),
                'b' => new Size(intval($sValue[4] . $sValue[5], 16), null, true, $oParserState->currentLine()),
            ];
        } else {
            throw new UnexpectedTokenException(
                'Invalid hex color value',
                $sValue,
                'custom',
                $oParserState->currentLine()
            );
        }
    } else {
        $sColorMode = $oParserState->parseIdentifier(true);
        $oParserState->consumeWhiteSpace();
        $oParserState->consume('(');

        $bContainsVar = false;
        $iLength = $oParserState->strlen($sColorMode);
        for ($i = 0; $i < $iLength; ++$i) {
            $oParserState->consumeWhiteSpace();
            if ($oParserState->comes('var')) {
                $aColor[$sColorMode[$i]] = CSSFunction::parseIdentifierOrFunction($oParserState);
                $bContainsVar = true;
            } else {
                $aColor[$sColorMode[$i]] = Size::parse($oParserState, true);
            }

            if ($bContainsVar && $oParserState->comes(')')) {
                // With a var argument the function can have fewer arguments
                break;
            }

            $oParserState->consumeWhiteSpace();
            if ($i < ($iLength - 1)) {
                $oParserState->consume(',');
            }
        }
        $oParserState->consume(')');

        if ($bContainsVar) {
            return new CSSFunction($sColorMode, array_values($aColor), ',', $oParserState->currentLine());
        }
    }
    return new Color($aColor, $oParserState->currentLine());
}