-
Notifications
You must be signed in to change notification settings - Fork 103
/
color.go
158 lines (128 loc) · 3.16 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package schema
import (
"fmt"
"strconv"
"strings"
"github.com/mitchellh/hashstructure/v2"
"go.starlark.net/starlark"
)
type Color struct {
SchemaField
starlarkPalette *starlark.List
}
func normalizeHexColor(hex string) (string, error) {
hex = strings.TrimPrefix(strings.ToLower(hex), "#")
if len(hex) != 3 && len(hex) != 6 {
return "", fmt.Errorf(
"expected 3 or 6 hex chars but found %d",
len(hex),
)
}
if _, err := strconv.ParseInt(hex, 16, 64); err != nil {
return "", fmt.Errorf(
"expected hex chars a-f,0-9 but found %s",
hex,
)
}
return fmt.Sprintf("#%s", hex), nil
}
func newColor(
thread *starlark.Thread,
_ *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
var (
id starlark.String
name starlark.String
desc starlark.String
icon starlark.String
def starlark.String
palette *starlark.List
)
var err error
if err = starlark.UnpackArgs(
"Color",
args, kwargs,
"id", &id,
"name", &name,
"desc", &desc,
"icon", &icon,
"default", &def,
"palette?", &palette,
); err != nil {
return nil, fmt.Errorf("unpacking arguments for Color: %s", err)
}
s := &Color{}
s.SchemaField.Type = "color"
s.ID = id.GoString()
s.Name = name.GoString()
s.Description = desc.GoString()
s.Icon = icon.GoString()
s.Default, err = normalizeHexColor(def.GoString())
if err != nil {
return nil, fmt.Errorf("malformed default color: %w", err)
}
if palette == nil {
return s, nil
}
s.starlarkPalette = starlark.NewList([]starlark.Value{})
s.Palette = []string{}
var paletteVal starlark.Value
paletteIter := palette.Iterate()
defer paletteIter.Done()
for i := 0; paletteIter.Next(&paletteVal); {
if _, isNone := paletteVal.(starlark.NoneType); isNone {
continue
}
col, ok := paletteVal.(starlark.String)
if !ok {
return nil, fmt.Errorf(
"expected palette to be a list of string but found: %s (at index %d)",
paletteVal.Type(),
i,
)
}
hex, err := normalizeHexColor(col.GoString())
if err != nil {
return nil, fmt.Errorf("malformed palette color at index %d: %w", i, err)
}
s.Palette = append(s.Palette, hex)
s.starlarkPalette.Append(starlark.String(hex))
}
return s, nil
}
func (s *Color) AsSchemaField() SchemaField {
return s.SchemaField
}
func (s *Color) AttrNames() []string {
return []string{
"id", "name", "desc", "icon", "default", "palette",
}
}
func (s *Color) Attr(name string) (starlark.Value, error) {
switch name {
case "id":
return starlark.String(s.ID), nil
case "name":
return starlark.String(s.Name), nil
case "desc":
return starlark.String(s.Description), nil
case "icon":
return starlark.String(s.Icon), nil
case "default":
return starlark.String(s.Default), nil
case "palette":
return s.starlarkPalette, nil
default:
return nil, nil
}
}
func (s *Color) String() string { return "Color(...)" }
func (s *Color) Type() string { return "Color" }
func (s *Color) Freeze() {}
func (s *Color) Truth() starlark.Bool { return true }
func (s *Color) Hash() (uint32, error) {
sum, err := hashstructure.Hash(s, hashstructure.FormatV2, nil)
return uint32(sum), err
}