package color import ( "github.com/lucasb-eyer/go-colorful" "math" ) type RGB struct { Red float64 `json:"red"` Green float64 `json:"green"` Blue float64 `json:"blue"` } func (rgb RGB) AtIntensity(intensity float64) RGB { hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv() hsv2 := colorful.Hsv(hue, sat, intensity) return RGB{Red: hsv2.R, Green: hsv2.G, Blue: hsv2.B} } func (rgb RGB) Bytes() [3]uint8 { return [3]uint8{ uint8(rgb.Red * 255.0), uint8(rgb.Green * 255.0), uint8(rgb.Blue * 255.0), } } func (rgb RGB) ToHS() HueSat { hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv() return HueSat{Hue: hue, Sat: sat} } func (rgb RGB) ToXY() XY { x, y, z := (colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}).Xyz() return XY{ X: x / (x + y + z), Y: y / (x + y + z), } } 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} newRGB := hs.ToRGB() return newRGB, value }