Skip to content

Commit 7d43a7b

Browse files
zMoooooritzmuesli
authored andcommitted
Support parsing color.Color config values
1 parent ea11b12 commit 7d43a7b

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"image/color"
67
"io/ioutil"
8+
"math"
79
"reflect"
810
"strconv"
911

1012
"github.com/BurntSushi/toml"
13+
colorful "github.com/lucasb-eyer/go-colorful"
1114
)
1215

1316
// DBusConfig describes a dbus action.
@@ -125,6 +128,27 @@ func ConfigValue(v interface{}, dst interface{}) error {
125128
return fmt.Errorf("unhandled type %+v for float64 conversion", reflect.TypeOf(vt))
126129
}
127130

131+
case *color.Color:
132+
switch vt := v.(type) {
133+
case int64:
134+
x := math.Min(1.0, math.Max(0.0, float64(vt)/255))
135+
*d = colorful.Color{x, x, x}
136+
case float64:
137+
x := math.Min(1.0, math.Max(0.0, vt/255))
138+
*d = colorful.Color{x, x, x}
139+
case bool:
140+
if vt {
141+
*d = colorful.Color{1.0, 1.0, 1.0}
142+
} else {
143+
*d = colorful.Color{0.0, 0.0, 0.0}
144+
}
145+
case string:
146+
x, _ := colorful.Hex(vt)
147+
*d = x
148+
default:
149+
return fmt.Errorf("unhandled type %+v for color.Color conversion", reflect.TypeOf(vt))
150+
}
151+
128152
default:
129153
return fmt.Errorf("unhandled dst type %+v", reflect.TypeOf(dst))
130154
}

0 commit comments

Comments
 (0)