add color conversion in parse and rgb to ui state.
This commit is contained in:
+39
-1
@@ -126,6 +126,14 @@ func (col *Color) ToRGB() (col2 Color, ok bool) {
|
||||
ok = true
|
||||
}
|
||||
|
||||
if ok {
|
||||
col2 = Color{RGB: &RGB{
|
||||
Red: math.Max(col2.RGB.Red, 0.0),
|
||||
Green: math.Max(col2.RGB.Green, 0.0),
|
||||
Blue: math.Max(col2.RGB.Blue, 0.0),
|
||||
}}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -309,7 +317,9 @@ func Parse(raw string) (col Color, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
tokens := strings.SplitN(raw, ":", 2)
|
||||
convertTokens := strings.SplitN(raw, "|", 2)
|
||||
|
||||
tokens := strings.SplitN(convertTokens[0], ":", 2)
|
||||
if len(tokens) != 2 {
|
||||
err = ErrBadColorInput
|
||||
return
|
||||
@@ -434,6 +444,34 @@ func Parse(raw string) (col Color, err error) {
|
||||
err = ErrUnknownColorFormat
|
||||
}
|
||||
|
||||
if err == nil && len(convertTokens) == 2 {
|
||||
switch convertTokens[1] {
|
||||
case "xy":
|
||||
col2, ok := col.ToXY()
|
||||
if !ok {
|
||||
err = ErrUnknownColorFormat
|
||||
} else {
|
||||
col = col2
|
||||
}
|
||||
case "rgb":
|
||||
col2, ok := col.ToRGB()
|
||||
if !ok {
|
||||
err = ErrUnknownColorFormat
|
||||
} else {
|
||||
col = col2
|
||||
}
|
||||
case "hs":
|
||||
col2, ok := col.ToHS()
|
||||
if !ok {
|
||||
err = ErrUnknownColorFormat
|
||||
} else {
|
||||
col = col2
|
||||
}
|
||||
default:
|
||||
err = ErrUnknownColorFormat
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+12
-1
@@ -1,6 +1,9 @@
|
||||
package color
|
||||
|
||||
import "github.com/lucasb-eyer/go-colorful"
|
||||
import (
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"math"
|
||||
)
|
||||
|
||||
type RGB struct {
|
||||
Red float64 `json:"red"`
|
||||
@@ -36,6 +39,14 @@ func (rgb RGB) ToXY() XY {
|
||||
}
|
||||
}
|
||||
|
||||
func (rgb RGB) Round() RGB {
|
||||
return RGB{
|
||||
Red: math.Max(math.Round(rgb.Red*1000)/1000, 0),
|
||||
Green: math.Max(math.Round(rgb.Green*1000)/1000, 0),
|
||||
Blue: math.Max(math.Round(rgb.Blue*1000)/1000, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (rgb RGB) Normalize() (RGB, float64) {
|
||||
hue, sat, value := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
|
||||
hs := HueSat{Hue: hue, Sat: sat}
|
||||
|
||||
@@ -162,13 +162,13 @@ type XY struct {
|
||||
|
||||
func (xy XY) ToRGB() RGB {
|
||||
h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv()
|
||||
c := colorful.Hsv(h, s, 1)
|
||||
c := colorful.Hsv(math.Mod(h, 360), s, 1)
|
||||
return RGB{Red: c.R, Green: c.G, Blue: c.B}
|
||||
}
|
||||
|
||||
func (xy XY) ToHS() HueSat {
|
||||
h, s, _ := colorful.Xyy(xy.X, xy.Y, 0.5).Hsv()
|
||||
return HueSat{Hue: h, Sat: s}
|
||||
return HueSat{Hue: math.Mod(h, 360), Sat: s}
|
||||
}
|
||||
|
||||
func (xy XY) DistanceTo(other XY) float64 {
|
||||
|
||||
Reference in New Issue
Block a user