1package css
 2
 3import "github.com/tdewolff/parse/v2"
 4
 5// IsIdent returns true if the bytes are a valid identifier.
 6func IsIdent(b []byte) bool {
 7	l := NewLexer(parse.NewInputBytes(b))
 8	l.consumeIdentToken()
 9	l.r.Restore()
10	return l.r.Pos() == len(b)
11}
12
13// IsURLUnquoted returns true if the bytes are a valid unquoted URL.
14func IsURLUnquoted(b []byte) bool {
15	l := NewLexer(parse.NewInputBytes(b))
16	l.consumeUnquotedURL()
17	l.r.Restore()
18	return l.r.Pos() == len(b)
19}
20
21// HSL2RGB converts HSL to RGB with all of range [0,1]
22// from http://www.w3.org/TR/css3-color/#hsl-color
23func HSL2RGB(h, s, l float64) (float64, float64, float64) {
24	m2 := l * (s + 1)
25	if l > 0.5 {
26		m2 = l + s - l*s
27	}
28	m1 := l*2 - m2
29	return hue2rgb(m1, m2, h+1.0/3.0), hue2rgb(m1, m2, h), hue2rgb(m1, m2, h-1.0/3.0)
30}
31
32func hue2rgb(m1, m2, h float64) float64 {
33	if h < 0.0 {
34		h += 1.0
35	}
36	if h > 1.0 {
37		h -= 1.0
38	}
39	if h*6.0 < 1.0 {
40		return m1 + (m2-m1)*h*6.0
41	} else if h*2.0 < 1.0 {
42		return m2
43	} else if h*3.0 < 2.0 {
44		return m1 + (m2-m1)*(2.0/3.0-h)*6.0
45	}
46	return m1
47}