-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
50 lines (42 loc) · 1.11 KB
/
color.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package languageserver
import (
"math"
"regexp"
"github.com/mazznoer/csscolorparser"
"go.lsp.dev/protocol"
"go.uber.org/zap"
)
func decodeColorLiteral(raw string) protocol.Color {
logger.Debug("decodeColorLiteral", zap.String("raw", raw))
if hexLike(raw) {
raw = "#" + raw
}
color, err := csscolorparser.Parse(raw)
if err != nil {
return protocol.Color{}
}
logger.Debug("decodeColorLiteral", zap.Any("color", color))
return protocol.Color{
Red: roundToThree(color.R),
Alpha: roundToThree(color.A),
Blue: roundToThree(color.B),
Green: roundToThree(color.G),
}
}
func encodeColorLiteral(color protocol.Color) string {
logger.Debug("encodeColorLiteral", zap.Any("color", color))
out := csscolorparser.Color{
R: roundToThree(color.Red),
G: roundToThree(color.Green),
B: roundToThree(color.Blue),
A: roundToThree(color.Alpha),
}.HexString()
logger.Debug("encodeColorLiteral", zap.String("out", out))
return out
}
func hexLike(s string) bool {
return regexp.MustCompile(`^[0-9a-fA-F]{3,8}$`).MatchString(s)
}
func roundToThree(f float64) float64 {
return math.Round(f*1_00) / 1_00
}