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.

56 lines
1.2 KiB

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