func (p *parser) lowerColor(token css_ast.Token) css_ast.Token {
	text := token.Text

	switch token.Kind {
	case css_lexer.THash, css_lexer.THashID:
		if p.options.UnsupportedCSSFeatures.Has(compat.HexRGBA) {
			switch len(text) {
			case 4:
				// "#1234" => "rgba(1, 2, 3, 0.004)"
				if hex, ok := parseHex(text); ok {
					hex = expandHex(hex)
					token.Kind = css_lexer.TFunction
					token.Text = "rgba"
					token.Children = &[]css_ast.Token{
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: floatToString(float64(hexA(hex)) / 255)},
					}
				}

			case 8:
				// "#12345678" => "rgba(18, 52, 86, 0.47)"
				if hex, ok := parseHex(text); ok {
					token.Kind = css_lexer.TFunction
					token.Text = "rgba"
					token.Children = &[]css_ast.Token{
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: floatToString(float64(hexA(hex)) / 255)},
					}
				}
			}
		}

	case css_lexer.TIdent:
		if text == "rebeccapurple" && p.options.UnsupportedCSSFeatures.Has(compat.RebeccaPurple) {
			token.Kind = css_lexer.THash
			token.Text = "663399"
		}

	case css_lexer.TFunction:
		switch text {
		case "rgb", "rgba", "hsl", "hsla":
			if p.options.UnsupportedCSSFeatures.Has(compat.Modern_RGB_HSL) {
				args := *token.Children
				removeAlpha := false
				addAlpha := false

				// "hsl(1deg, 2%, 3%)" => "hsl(1, 2%, 3%)"
				if (text == "hsl" || text == "hsla") && len(args) > 0 {
					if degrees, ok := degreesForAngle(args[0]); ok {
						args[0].Kind = css_lexer.TNumber
						args[0].Text = floatToString(degrees)
					}
				}

				switch len(args) {
				case 3:
					// "rgba(1 2 3)" => "rgb(1, 2, 3)"
					// "hsla(1 2% 3%)" => "rgb(1, 2%, 3%)"
					removeAlpha = true
					args[0].HasWhitespaceAfter = false
					args[1].HasWhitespaceAfter = false
					token.Children = &[]css_ast.Token{
						args[0], commaToken,
						args[1], commaToken,
						args[2],
					}

				case 5:
					// "rgba(1, 2, 3)" => "rgb(1, 2, 3)"
					// "hsla(1, 2%, 3%)" => "hsl(1%, 2%, 3%)"
					if args[1].Kind == css_lexer.TComma && args[3].Kind == css_lexer.TComma {
						removeAlpha = true
						break
					}

					// "rgb(1 2 3 / 4%)" => "rgba(1, 2, 3, 0.04)"
					// "hsl(1 2% 3% / 4%)" => "hsla(1, 2%, 3%, 0.04)"
					if args[3].Kind == css_lexer.TDelimSlash {
						addAlpha = true
						args[0].HasWhitespaceAfter = false
						args[1].HasWhitespaceAfter = false
						args[2].HasWhitespaceAfter = false
						token.Children = &[]css_ast.Token{
							args[0], commaToken,
							args[1], commaToken,
							args[2], commaToken,
							lowerAlphaPercentageToNumber(args[4]),
						}
					}

				case 7:
					// "rgb(1%, 2%, 3%, 4%)" => "rgba(1%, 2%, 3%, 0.04)"
					// "hsl(1, 2%, 3%, 4%)" => "hsla(1, 2%, 3%, 0.04)"
					if args[1].Kind == css_lexer.TComma && args[3].Kind == css_lexer.TComma && args[5].Kind == css_lexer.TComma {
						addAlpha = true
						args[6] = lowerAlphaPercentageToNumber(args[6])
					}
				}

				if removeAlpha {
					if text == "rgba" {
						token.Text = "rgb"
					} else if text == "hsla" {
						token.Text = "hsl"
					}
				} else if addAlpha {
					if text == "rgb" {
						token.Text = "rgba"
					} else if text == "hsl" {
						token.Text = "hsla"
					}
				}
			}
		}
	}

	return token
}
// Convert newer color syntax to older color syntax for older browsers
Convert newer color syntax to older color syntax for older browsers
// Convert newer color syntax to older color syntax for older browsers
func (p *parser) lowerColor(token css_ast.Token) css_ast.Token {
	text := token.Text
	switch token.Kind {
	case css_lexer.THash, css_lexer.THashID:
		if p.options.UnsupportedCSSFeatures.Has(compat.HexRGBA) {
			switch len(text) {
			case 4:
				if hex, ok := parseHex(text); ok {
					hex = expandHex(hex)
					token.Kind = css_lexer.TFunction
					token.Text = "rgba"
					token.Children = &[]css_ast.Token{
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: floatToString(float64(hexA(hex)) / 255)},
					}
				}
			case 8:
				if hex, ok := parseHex(text); ok {
					token.Kind = css_lexer.TFunction
					token.Text = "rgba"
					token.Children = &[]css_ast.Token{
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexR(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexG(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: strconv.Itoa(hexB(hex))}, commaToken,
						{Kind: css_lexer.TNumber, Text: floatToString(float64(hexA(hex)) / 255)},
					}
				}
			}
		}
	case css_lexer.TIdent:
		if text == "rebeccapurple" && p.options.UnsupportedCSSFeatures.Has(compat.RebeccaPurple) {
			token.Kind = css_lexer.THash
			token.Text = "663399"
		}
	case css_lexer.TFunction:
		switch text {
		case "rgb", "rgba", "hsl", "hsla":
			if p.options.UnsupportedCSSFeatures.Has(compat.Modern_RGB_HSL) {
				args := *token.Children
				removeAlpha := false
				addAlpha := false
				if (text == "hsl" || text == "hsla") && len(args) > 0 {
					if degrees, ok := degreesForAngle(args[0]); ok {
						args[0].Kind = css_lexer.TNumber
						args[0].Text = floatToString(degrees)
					}
				}
				switch len(args) {
				case 3:
					removeAlpha = true
					args[0].HasWhitespaceAfter = false
					args[1].HasWhitespaceAfter = false
					token.Children = &[]css_ast.Token{
						args[0], commaToken,
						args[1], commaToken,
						args[2],
					}
				case 5:
					if args[1].Kind == css_lexer.TComma && args[3].Kind == css_lexer.TComma {
						removeAlpha = true
						break
					}
					if args[3].Kind == css_lexer.TDelimSlash {
						addAlpha = true
						args[0].HasWhitespaceAfter = false
						args[1].HasWhitespaceAfter = false
						args[2].HasWhitespaceAfter = false
						token.Children = &[]css_ast.Token{
							args[0], commaToken,
							args[1], commaToken,
							args[2], commaToken,
							lowerAlphaPercentageToNumber(args[4]),
						}
					}
				case 7:
					if args[1].Kind == css_lexer.TComma && args[3].Kind == css_lexer.TComma && args[5].Kind == css_lexer.TComma {
						addAlpha = true
						args[6] = lowerAlphaPercentageToNumber(args[6])
					}
				}
				if removeAlpha {
					if text == "rgba" {
						token.Text = "rgb"
					} else if text == "hsla" {
						token.Text = "hsl"
					}
				} else if addAlpha {
					if text == "rgb" {
						token.Text = "rgba"
					} else if text == "hsl" {
						token.Text = "hsla"
					}
				}
			}
		}
	}
	return token
}
func ( p * parser ) lowerColor ( token css_ast . Token ) css_ast . Token { text := token . Text switch token . Kind { case css_lexer . THash , css_lexer . THashID : if p . options . UnsupportedCSSFeatures . Has ( compat . HexRGBA ) { switch len ( text ) { case 4 : if hex , ok := parseHex ( text ) ; ok { hex = expandHex ( hex ) token . Kind = css_lexer . TFunction token . Text = " " token . Children = & [ ] css_ast . Token { { Kind : css_lexer . TNumber , Text : strconv . Itoa ( hexR ( hex ) ) } , commaToken , { Kind : css_lexer . TNumber , Text : strconv . Itoa ( hexG ( hex ) ) } , commaToken , { Kind : css_lexer . TNumber , Text : strconv . Itoa ( hexB ( hex ) ) } , commaToken , { Kind : css_lexer . TNumber , Text : floatToString ( float64 ( hexA ( hex ) ) / 255 ) } , } } case 8 : if hex , ok := parseHex ( text ) ; ok { token . Kind = css_lexer . TFunction token . Text = " " token . Children = & [ ] css_ast . Token { { Kind : css_lexer . TNumber , Text : strconv . Itoa ( hexR ( hex ) ) } , commaToken , { Kind : css_lexer . TNumber , Text : strconv . Itoa ( hexG ( hex ) ) } , commaToken , { Kind : css_lexer . TNumber , Text : strconv . Itoa ( hexB ( hex ) ) } , commaToken , { Kind : css_lexer . TNumber , Text : floatToString ( float64 ( hexA ( hex ) ) / 255 ) } , } } } } case css_lexer . TIdent : if text == " " && p . options . UnsupportedCSSFeatures . Has ( compat . RebeccaPurple ) { token . Kind = css_lexer . THash token . Text = " " } case css_lexer . TFunction : switch text { case " " , " " , " " , " " : if p . options . UnsupportedCSSFeatures . Has ( compat . Modern_RGB_HSL ) { args := * token . Children removeAlpha := false addAlpha := false if ( text == " " || text == " " ) && len ( args ) > 0 { if degrees , ok := degreesForAngle ( args [ 0 ] ) ; ok { args [ 0 ] . Kind = css_lexer . TNumber args [ 0 ] . Text = floatToString ( degrees ) } } switch len ( args ) { case 3 : removeAlpha = true args [ 0 ] . HasWhitespaceAfter = false args [ 1 ] . HasWhitespaceAfter = false token . Children = & [ ] css_ast . Token { args [ 0 ] , commaToken , args [ 1 ] , commaToken , args [ 2 ] , } case 5 : if args [ 1 ] . Kind == css_lexer . TComma && args [ 3 ] . Kind == css_lexer . TComma { removeAlpha = true break } if args [ 3 ] . Kind == css_lexer . TDelimSlash { addAlpha = true args [ 0 ] . HasWhitespaceAfter = false args [ 1 ] . HasWhitespaceAfter = false args [ 2 ] . HasWhitespaceAfter = false token . Children = & [ ] css_ast . Token { args [ 0 ] , commaToken , args [ 1 ] , commaToken , args [ 2 ] , commaToken , lowerAlphaPercentageToNumber ( args [ 4 ] ) , } } case 7 : if args [ 1 ] . Kind == css_lexer . TComma && args [ 3 ] . Kind == css_lexer . TComma && args [ 5 ] . Kind == css_lexer . TComma { addAlpha = true args [ 6 ] = lowerAlphaPercentageToNumber ( args [ 6 ] ) } } if removeAlpha { if text == " " { token . Text = " " } else if text == " " { token . Text = " " } } else if addAlpha { if text == " " { token . Text = " " } else if text == " " { token . Text = " " } } } } } return token }
Convert newer color syntax to older color syntax for older browsers
Convert newer color syntax to older color syntax for older browsers
// "#1234" => "rgba(1, 2, 3, 0.004)" // "#12345678" => "rgba(18, 52, 86, 0.47)" // "hsl(1deg, 2%, 3%)" => "hsl(1, 2%, 3%)" // "rgba(1 2 3)" => "rgb(1, 2, 3)" // "hsla(1 2% 3%)" => "rgb(1, 2%, 3%)" // "rgba(1, 2, 3)" => "rgb(1, 2, 3)" // "hsla(1, 2%, 3%)" => "hsl(1%, 2%, 3%)" // "rgb(1 2 3 / 4%)" => "rgba(1, 2, 3, 0.04)" // "hsl(1 2% 3% / 4%)" => "hsla(1, 2%, 3%, 0.04)" // "rgb(1%, 2%, 3%, 4%)" => "rgba(1%, 2%, 3%, 0.04)" // "hsl(1, 2%, 3%, 4%)" => "hsla(1, 2%, 3%, 0.04)"