1package colorful
2
3import (
4	"database/sql/driver"
5	"fmt"
6	"reflect"
7)
8
9// A HexColor is a Color stored as a hex string "#rrggbb". It implements the
10// database/sql.Scanner and database/sql/driver.Value interfaces.
11type HexColor Color
12
13type errUnsupportedType struct {
14	got  interface{}
15	want reflect.Type
16}
17
18func (hc *HexColor) Scan(value interface{}) error {
19	s, ok := value.(string)
20	if !ok {
21		return errUnsupportedType{got: reflect.TypeOf(value), want: reflect.TypeOf("")}
22	}
23	c, err := Hex(s)
24	if err != nil {
25		return err
26	}
27	*hc = HexColor(c)
28	return nil
29}
30
31func (hc *HexColor) Value() (driver.Value, error) {
32	return Color(*hc).Hex(), nil
33}
34
35func (e errUnsupportedType) Error() string {
36	return fmt.Sprintf("unsupported type: got %v, want a %s", e.got, e.want)
37}
38