mirror of
https://gitea.com/Sirherobrine23/tea.git
synced 2024-08-21 03:57:23 +00:00
0d98cbd657
* update & migrate gitea sdk (Fix Delete Tag Issue) * upgraded github.com/AlecAivazis/survey v2.2.7 => v2.2.8 * upgraded github.com/adrg/xdg v0.2.3 => v0.3.1 * upgraded github.com/araddon/dateparse * upgraded github.com/olekukonko/tablewriter v0.0.4 => v0.0.5 * upgraded gopkg.in/yaml.v2 v2.3.0 => v2.4.0 Reviewed-on: https://gitea.com/gitea/tea/pulls/337 Reviewed-by: Norwin <noerw@noreply.gitea.io> Reviewed-by: khmarbaise <khmarbaise@noreply.gitea.io> Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de>
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package colorful
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
// A HexColor is a Color stored as a hex string "#rrggbb". It implements the
|
|
// database/sql.Scanner, database/sql/driver.Value,
|
|
// encoding/json.Unmarshaler and encoding/json.Marshaler interfaces.
|
|
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())
|
|
}
|
|
|
|
// Decode - deserialize function for https://github.com/kelseyhightower/envconfig
|
|
func (hc *HexColor) Decode(hexCode string) error {
|
|
var col, err = Hex(hexCode)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*hc = HexColor(col)
|
|
return nil
|
|
}
|