util.go 1.0 KB

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