package colorful
import (
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
)
type HexColor Color
type errUnsupportedType struct {
got interface {}
want reflect .Type
}
func (hc *HexColor ) Scan (value interface {}) error {
s , ok := value .(string )
if !ok {
return errUnsupportedType {got : reflect .TypeOf (value ), want : reflect .TypeOf ("" )}
}
c , err := Hex (s )
if err != nil {
return err
}
*hc = HexColor (c )
return nil
}
func (hc *HexColor ) Value () (driver .Value , error ) {
return Color (*hc ).Hex (), nil
}
func (e errUnsupportedType ) Error () string {
return fmt .Sprintf ("unsupported type: got %v, want a %s" , e .got , e .want )
}
func (hc *HexColor ) UnmarshalJSON (data []byte ) error {
var hexCode string
if err := json .Unmarshal (data , &hexCode ); err != nil {
return err
}
var col , err = Hex (hexCode )
if err != nil {
return err
}
*hc = HexColor (col )
return nil
}
func (hc HexColor ) MarshalJSON () ([]byte , error ) {
return json .Marshal (Color (hc ).Hex ())
}
func (hc *HexColor ) Decode (hexCode string ) error {
var col , err = Hex (hexCode )
if err != nil {
return err
}
*hc = HexColor (col )
return nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .