gradient.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package canvas
  2. import (
  3. "image"
  4. "image/color"
  5. "math"
  6. "fyne.io/fyne/v2"
  7. )
  8. // LinearGradient defines a Gradient travelling straight at a given angle.
  9. // The only supported values for the angle are `0.0` (vertical) and `90.0` (horizontal), currently.
  10. type LinearGradient struct {
  11. baseObject
  12. StartColor color.Color // The beginning color of the gradient
  13. EndColor color.Color // The end color of the gradient
  14. Angle float64 // The angle of the gradient (0/180 for vertical; 90/270 for horizontal)
  15. }
  16. // Generate calculates an image of the gradient with the specified width and height.
  17. func (g *LinearGradient) Generate(iw, ih int) image.Image {
  18. w, h := float64(iw), float64(ih)
  19. var generator func(x, y float64) float64
  20. switch g.Angle {
  21. case 90: // horizontal flipped
  22. generator = func(x, _ float64) float64 {
  23. return (w - x) / w
  24. }
  25. case 270: // horizontal
  26. generator = func(x, _ float64) float64 {
  27. return x / w
  28. }
  29. case 45: // diagonal negative flipped
  30. generator = func(x, y float64) float64 {
  31. return math.Abs((w - x + y) / (w + h)) // ((w+h)-(x+h-y)) / (w+h)
  32. }
  33. case 225: // diagonal negative
  34. generator = func(x, y float64) float64 {
  35. return math.Abs((x + h - y) / (w + h))
  36. }
  37. case 135: // diagonal positive flipped
  38. generator = func(x, y float64) float64 {
  39. return math.Abs((w + h - (x + y)) / (w + h))
  40. }
  41. case 315: // diagonal positive
  42. generator = func(x, y float64) float64 {
  43. return math.Abs((x + y) / (w + h))
  44. }
  45. case 180: // vertical flipped
  46. generator = func(_, y float64) float64 {
  47. return (h - y) / h
  48. }
  49. default: // vertical
  50. generator = func(_, y float64) float64 {
  51. return y / h
  52. }
  53. }
  54. return computeGradient(generator, iw, ih, g.StartColor, g.EndColor)
  55. }
  56. // Hide will set this gradient to not be visible
  57. func (g *LinearGradient) Hide() {
  58. g.baseObject.Hide()
  59. repaint(g)
  60. }
  61. // Move the gradient to a new position, relative to its parent / canvas
  62. func (g *LinearGradient) Move(pos fyne.Position) {
  63. g.baseObject.Move(pos)
  64. repaint(g)
  65. }
  66. // Refresh causes this gradient to be redrawn with its configured state.
  67. func (g *LinearGradient) Refresh() {
  68. Refresh(g)
  69. }
  70. // RadialGradient defines a Gradient travelling radially from a center point outward.
  71. type RadialGradient struct {
  72. baseObject
  73. StartColor color.Color // The beginning color of the gradient
  74. EndColor color.Color // The end color of the gradient
  75. // The offset of the center for generation of the gradient.
  76. // This is not a DP measure but relates to the width/height.
  77. // A value of 0.5 would move the center by the half width/height.
  78. CenterOffsetX, CenterOffsetY float64
  79. }
  80. // Generate calculates an image of the gradient with the specified width and height.
  81. func (g *RadialGradient) Generate(iw, ih int) image.Image {
  82. w, h := float64(iw), float64(ih)
  83. // define center plus offset
  84. centerX := w/2 + w*g.CenterOffsetX
  85. centerY := h/2 + h*g.CenterOffsetY
  86. // handle negative offsets
  87. var a, b float64
  88. if g.CenterOffsetX < 0 {
  89. a = w - centerX
  90. } else {
  91. a = centerX
  92. }
  93. if g.CenterOffsetY < 0 {
  94. b = h - centerY
  95. } else {
  96. b = centerY
  97. }
  98. generator := func(x, y float64) float64 {
  99. // calculate distance from center for gradient multiplier
  100. dx, dy := centerX-x, centerY-y
  101. da := math.Sqrt(dx*dx + dy*dy*a*a/b/b)
  102. if da > a {
  103. return 1
  104. }
  105. return da / a
  106. }
  107. return computeGradient(generator, iw, ih, g.StartColor, g.EndColor)
  108. }
  109. // Hide will set this gradient to not be visible
  110. func (g *RadialGradient) Hide() {
  111. g.baseObject.Hide()
  112. repaint(g)
  113. }
  114. // Move the gradient to a new position, relative to its parent / canvas
  115. func (g *RadialGradient) Move(pos fyne.Position) {
  116. g.baseObject.Move(pos)
  117. repaint(g)
  118. }
  119. // Refresh causes this gradient to be redrawn with its configured state.
  120. func (g *RadialGradient) Refresh() {
  121. Refresh(g)
  122. }
  123. func calculatePixel(d float64, startColor, endColor color.Color) color.Color {
  124. // fetch RGBA values
  125. aR, aG, aB, aA := startColor.RGBA()
  126. bR, bG, bB, bA := endColor.RGBA()
  127. // Get difference
  128. dR := float64(bR) - float64(aR)
  129. dG := float64(bG) - float64(aG)
  130. dB := float64(bB) - float64(aB)
  131. dA := float64(bA) - float64(aA)
  132. // Apply gradations
  133. pixel := &color.RGBA64{
  134. R: uint16(float64(aR) + d*dR),
  135. B: uint16(float64(aB) + d*dB),
  136. G: uint16(float64(aG) + d*dG),
  137. A: uint16(float64(aA) + d*dA),
  138. }
  139. return pixel
  140. }
  141. func computeGradient(generator func(x, y float64) float64, w, h int, startColor, endColor color.Color) image.Image {
  142. img := image.NewNRGBA(image.Rect(0, 0, w, h))
  143. if startColor == nil && endColor == nil {
  144. return img
  145. } else if startColor == nil {
  146. startColor = color.Transparent
  147. } else if endColor == nil {
  148. endColor = color.Transparent
  149. }
  150. for x := 0; x < w; x++ {
  151. for y := 0; y < h; y++ {
  152. distance := generator(float64(x)+0.5, float64(y)+0.5)
  153. img.Set(x, y, calculatePixel(distance, startColor, endColor))
  154. }
  155. }
  156. return img
  157. }
  158. // NewHorizontalGradient creates a new horizontally travelling linear gradient.
  159. // The start color will be at the left of the gradient and the end color will be at the right.
  160. func NewHorizontalGradient(start, end color.Color) *LinearGradient {
  161. g := &LinearGradient{StartColor: start, EndColor: end}
  162. g.Angle = 270
  163. return g
  164. }
  165. // NewLinearGradient creates a linear gradient at the specified angle.
  166. // The angle parameter is the degree angle along which the gradient is calculated.
  167. // A NewHorizontalGradient uses 270 degrees and NewVerticalGradient is 0 degrees.
  168. func NewLinearGradient(start, end color.Color, angle float64) *LinearGradient {
  169. g := &LinearGradient{StartColor: start, EndColor: end}
  170. g.Angle = angle
  171. return g
  172. }
  173. // NewRadialGradient creates a new radial gradient.
  174. func NewRadialGradient(start, end color.Color) *RadialGradient {
  175. return &RadialGradient{StartColor: start, EndColor: end}
  176. }
  177. // NewVerticalGradient creates a new vertically travelling linear gradient.
  178. // The start color will be at the top of the gradient and the end color will be at the bottom.
  179. func NewVerticalGradient(start color.Color, end color.Color) *LinearGradient {
  180. return &LinearGradient{StartColor: start, EndColor: end}
  181. }