colorm.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2022 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package colorm
  15. import (
  16. "fmt"
  17. "image/color"
  18. "sync"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. "github.com/hajimehoshi/ebiten/v2/internal/affine"
  21. "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
  22. )
  23. // Dim is the dimension of a ColorM.
  24. const Dim = affine.ColorMDim
  25. // ColorM represents a matrix to transform coloring when rendering an image.
  26. //
  27. // ColorM is applied to the straight alpha color
  28. // while an Image's pixels' format is alpha premultiplied.
  29. // Before applying a matrix, a color is un-multiplied, and after applying the matrix,
  30. // the color is multiplied again.
  31. //
  32. // The initial value is identity.
  33. type ColorM struct {
  34. impl affine.ColorM
  35. _ [0]func() // Marks as non-comparable.
  36. }
  37. func (c *ColorM) affineColorM() affine.ColorM {
  38. if c.impl != nil {
  39. return c.impl
  40. }
  41. return affine.ColorMIdentity{}
  42. }
  43. // String returns a string representation of ColorM.
  44. func (c *ColorM) String() string {
  45. return c.affineColorM().String()
  46. }
  47. // Reset resets the ColorM as identity.
  48. func (c *ColorM) Reset() {
  49. c.impl = affine.ColorMIdentity{}
  50. }
  51. // Apply pre-multiplies a vector (r, g, b, a, 1) by the matrix
  52. // where r, g, b, and a are clr's values in straight-alpha format.
  53. // In other words, Apply calculates ColorM * (r, g, b, a, 1)^T.
  54. func (c *ColorM) Apply(clr color.Color) color.Color {
  55. return c.affineColorM().Apply(clr)
  56. }
  57. // Concat multiplies a color matrix with the other color matrix.
  58. // This is same as multiplying the matrix other and the matrix c in this order.
  59. func (c *ColorM) Concat(other ColorM) {
  60. o := other.impl
  61. if o == nil {
  62. return
  63. }
  64. c.impl = c.affineColorM().Concat(o)
  65. }
  66. // Scale scales the matrix by (r, g, b, a).
  67. func (c *ColorM) Scale(r, g, b, a float64) {
  68. c.impl = c.affineColorM().Scale(float32(r), float32(g), float32(b), float32(a))
  69. }
  70. // ScaleWithColor scales the matrix by clr.
  71. func (c *ColorM) ScaleWithColor(clr color.Color) {
  72. cr, cg, cb, ca := clr.RGBA()
  73. if ca == 0 {
  74. c.Scale(0, 0, 0, 0)
  75. return
  76. }
  77. c.Scale(float64(cr)/float64(ca), float64(cg)/float64(ca), float64(cb)/float64(ca), float64(ca)/0xffff)
  78. }
  79. // Translate translates the matrix by (r, g, b, a).
  80. func (c *ColorM) Translate(r, g, b, a float64) {
  81. c.impl = c.affineColorM().Translate(float32(r), float32(g), float32(b), float32(a))
  82. }
  83. // RotateHue rotates the hue.
  84. // theta represents rotating angle in radian.
  85. func (c *ColorM) RotateHue(theta float64) {
  86. c.ChangeHSV(theta, 1, 1)
  87. }
  88. // ChangeHSV changes HSV (Hue-Saturation-Value) values.
  89. // hueTheta is a radian value to rotate hue.
  90. // saturationScale is a value to scale saturation.
  91. // valueScale is a value to scale value (a.k.a. brightness).
  92. //
  93. // This conversion uses RGB to/from YCrCb conversion.
  94. func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale float64) {
  95. c.impl = affine.ChangeHSV(c.affineColorM(), hueTheta, float32(saturationScale), float32(valueScale))
  96. }
  97. // Element returns a value of a matrix at (i, j).
  98. func (c *ColorM) Element(i, j int) float64 {
  99. return float64(c.affineColorM().At(i, j))
  100. }
  101. // SetElement sets an element at (i, j).
  102. func (c *ColorM) SetElement(i, j int, element float64) {
  103. c.impl = affine.ColorMSetElement(c.affineColorM(), i, j, float32(element))
  104. }
  105. // IsInvertible returns a boolean value indicating
  106. // whether the matrix c is invertible or not.
  107. func (c *ColorM) IsInvertible() bool {
  108. return c.affineColorM().IsInvertible()
  109. }
  110. // Invert inverts the matrix.
  111. // If c is not invertible, Invert panics.
  112. func (c *ColorM) Invert() {
  113. c.impl = c.affineColorM().Invert()
  114. }
  115. // ReadElements reads the body part and the translation part to the given float32 slices.
  116. //
  117. // len(body) must be 16 and len(translation) must be 4. Otherwise, ReadElements panics.
  118. func (c *ColorM) ReadElements(body []float32, translation []float32) {
  119. if len(body) != 16 {
  120. panic(fmt.Sprintf("ebiten: len(body) must be 16 but %d", len(body)))
  121. }
  122. if len(translation) != 4 {
  123. panic(fmt.Sprintf("ebiten: len(translation) must be 4 but %d", len(translation)))
  124. }
  125. c.affineColorM().Elements(body, translation)
  126. }
  127. func uniforms(c ColorM) map[string]any {
  128. var body [16]float32
  129. var translation [4]float32
  130. c.affineColorM().Elements(body[:], translation[:])
  131. uniforms := map[string]any{}
  132. uniforms[builtinshader.UniformColorMBody] = body[:]
  133. uniforms[builtinshader.UniformColorMTranslation] = translation[:]
  134. return uniforms
  135. }
  136. type builtinShaderKey struct {
  137. filter builtinshader.Filter
  138. address builtinshader.Address
  139. }
  140. var (
  141. builtinShaders = map[builtinShaderKey]*ebiten.Shader{}
  142. builtinShadersM sync.Mutex
  143. )
  144. func builtinShader(filter builtinshader.Filter, address builtinshader.Address) *ebiten.Shader {
  145. builtinShadersM.Lock()
  146. defer builtinShadersM.Unlock()
  147. key := builtinShaderKey{
  148. filter: filter,
  149. address: address,
  150. }
  151. if s, ok := builtinShaders[key]; ok {
  152. return s
  153. }
  154. src := builtinshader.ShaderSource(filter, address, true)
  155. s, err := ebiten.NewShader(src)
  156. if err != nil {
  157. panic(fmt.Sprintf("colorm: NewShader for a built-in shader failed: %v", err))
  158. }
  159. shader := s
  160. builtinShaders[key] = shader
  161. return shader
  162. }