You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.0 KiB

  1. package color
  2. import "github.com/lucasb-eyer/go-colorful"
  3. type RGB struct {
  4. Red float64 `json:"red"`
  5. Green float64 `json:"green"`
  6. Blue float64 `json:"blue"`
  7. }
  8. func (rgb RGB) AtIntensity(intensity float64) RGB {
  9. hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
  10. hsv2 := colorful.Hsv(hue, sat, intensity)
  11. return RGB{Red: hsv2.R, Green: hsv2.G, Blue: hsv2.B}
  12. }
  13. func (rgb RGB) Bytes() [3]uint8 {
  14. return [3]uint8{
  15. uint8(rgb.Red * 255.0),
  16. uint8(rgb.Green * 255.0),
  17. uint8(rgb.Blue * 255.0),
  18. }
  19. }
  20. func (rgb RGB) ToHS() HueSat {
  21. hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
  22. return HueSat{Hue: hue, Sat: sat}
  23. }
  24. func (rgb RGB) ToXY() XY {
  25. x, y, z := (colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}).Xyz()
  26. return XY{
  27. X: x / (x + y + z),
  28. Y: y / (x + y + z),
  29. }
  30. }
  31. func (rgb RGB) Normalize() (RGB, float64) {
  32. hue, sat, value := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv()
  33. hs := HueSat{Hue: hue, Sat: sat}
  34. newRGB := hs.ToRGB()
  35. return newRGB, value
  36. }