-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.go
54 lines (48 loc) · 879 Bytes
/
temp.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
package main
import (
"fmt"
"strconv"
)
type Temp struct {
Value int
Name string
}
var (
defTemp = Temp{6500, "default"}
minTemp = Temp{1000, "minimal"}
maxTemp = Temp{10000, "maximal"}
presets = []Temp{
{2300, "candle"},
{2700, "tungsten"},
{3400, "halogen"},
{4200, "fluorescent"},
{5000, "daylight"},
}
)
func (m *Temp) Set(s string) error {
for _, v := range presets {
if v.Name == s {
*m = v
return nil
}
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
if i < minTemp.Value || i > maxTemp.Value {
return fmt.Errorf("out of range")
}
*m = Temp{i, "manual"}
return nil
}
func (m Temp) String() string {
return fmt.Sprint(m.Value)
}
func (_ Temp) Usage() string {
s := fmt.Sprintf("color temperature in range %v..%v or", minTemp, maxTemp)
for _, v := range presets {
s += fmt.Sprintf(" %v", v.Name)
}
return s
}