-
Notifications
You must be signed in to change notification settings - Fork 6
/
color.go
95 lines (82 loc) · 2.42 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package hyprls
import (
"context"
"fmt"
"math"
"strings"
"github.com/ewen-lbh/hyprls/parser"
"github.com/mazznoer/csscolorparser"
"go.lsp.dev/protocol"
"go.uber.org/zap"
)
func (h Handler) ColorPresentation(ctx context.Context, params *protocol.ColorPresentationParams) ([]protocol.ColorPresentation, error) {
logger.Debug("LSP:ColorPresentation", zap.Any("color", params.Color), zap.Any("range", params.Range))
return []protocol.ColorPresentation{
{
Label: encodeColorLiteral(params.Color),
TextEdit: &protocol.TextEdit{
Range: params.Range,
NewText: encodeColorLiteral(params.Color),
},
},
}, nil
}
func (h Handler) DocumentColor(ctx context.Context, params *protocol.DocumentColorParams) ([]protocol.ColorInformation, error) {
document, err := parse(params.TextDocument.URI)
if err != nil {
return []protocol.ColorInformation{}, fmt.Errorf("while parsing: %w", err)
}
colors := make([]protocol.ColorInformation, 0)
document.WalkValues(func(a *parser.Assignment, v *parser.Value) {
if v.Kind == parser.Gradient {
for _, stop := range v.Gradient.Stops {
colors = append(colors, protocol.ColorInformation{
Color: stop.LSPColor(),
Range: stop.LSPRange(),
})
}
return
}
if v.Kind != parser.Color {
return
}
colors = append(colors, protocol.ColorInformation{
Color: v.LSPColor(),
Range: v.LSPRange(),
})
})
return colors, nil
}
func decodeColorLiteral(raw string) protocol.Color {
logger.Debug("decodeColorLiteral", zap.String("raw", raw))
color, err := parser.ParseColor(raw)
if err != nil {
return protocol.Color{}
}
logger.Debug("decodeColorLiteral", zap.Any("color", color))
return protocol.Color{
Red: roundToThree(float64(color.R) / 255.0),
Alpha: roundToThree(float64(color.A) / 255.0),
Blue: roundToThree(float64(color.B) / 255.0),
Green: roundToThree(float64(color.G) / 255.0),
}
}
func encodeColorLiteral(color protocol.Color) string {
logger.Debug("encodeColorLiteral", zap.Any("color", color))
out := strings.TrimPrefix(csscolorparser.Color{
R: roundToThree(color.Red),
G: roundToThree(color.Green),
B: roundToThree(color.Blue),
A: roundToThree(color.Alpha),
}.HexString(), "#")
if color.Alpha == 1.0 {
out = fmt.Sprintf("rgb(%s)", out)
} else {
out = fmt.Sprintf("rgba(%s)", out)
}
logger.Debug("encodeColorLiteral", zap.String("out", out))
return out
}
func roundToThree(f float64) float64 {
return math.Round(f*1_00) / 1_00
}