colorm_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2014 Hajime Hoshi
  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_test
  15. import (
  16. "image/color"
  17. "math"
  18. "testing"
  19. "github.com/hajimehoshi/ebiten/v2/colorm"
  20. )
  21. func TestColorMInit(t *testing.T) {
  22. var m colorm.ColorM
  23. for i := 0; i < colorm.Dim-1; i++ {
  24. for j := 0; j < colorm.Dim; j++ {
  25. got := m.Element(i, j)
  26. want := 0.0
  27. if i == j {
  28. want = 1
  29. }
  30. if want != got {
  31. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  32. }
  33. }
  34. }
  35. m.SetElement(0, 0, 1)
  36. for i := 0; i < colorm.Dim-1; i++ {
  37. for j := 0; j < colorm.Dim; j++ {
  38. got := m.Element(i, j)
  39. want := 0.0
  40. if i == j {
  41. want = 1
  42. }
  43. if want != got {
  44. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  45. }
  46. }
  47. }
  48. }
  49. func TestColorMAssign(t *testing.T) {
  50. m := colorm.ColorM{}
  51. m.SetElement(0, 0, 1)
  52. m2 := m
  53. m.SetElement(0, 0, 0)
  54. got := m2.Element(0, 0)
  55. want := 1.0
  56. if want != got {
  57. t.Errorf("m2.Element(%d, %d) = %f, want %f", 0, 0, got, want)
  58. }
  59. }
  60. func TestColorMTranslate(t *testing.T) {
  61. expected := [4][5]float64{
  62. {1, 0, 0, 0, 0.5},
  63. {0, 1, 0, 0, 1.5},
  64. {0, 0, 1, 0, 2.5},
  65. {0, 0, 0, 1, 3.5},
  66. }
  67. m := colorm.ColorM{}
  68. m.Translate(0.5, 1.5, 2.5, 3.5)
  69. for i := 0; i < 4; i++ {
  70. for j := 0; j < 5; j++ {
  71. got := m.Element(i, j)
  72. want := expected[i][j]
  73. if want != got {
  74. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  75. }
  76. }
  77. }
  78. }
  79. func TestColorMScale(t *testing.T) {
  80. expected := [4][5]float64{
  81. {0.5, 0, 0, 0, 0},
  82. {0, 1.5, 0, 0, 0},
  83. {0, 0, 2.5, 0, 0},
  84. {0, 0, 0, 3.5, 0},
  85. }
  86. m := colorm.ColorM{}
  87. m.Scale(0.5, 1.5, 2.5, 3.5)
  88. for i := 0; i < 4; i++ {
  89. for j := 0; j < 5; j++ {
  90. got := m.Element(i, j)
  91. want := expected[i][j]
  92. if want != got {
  93. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  94. }
  95. }
  96. }
  97. }
  98. func TestColorMTranslateAndScale(t *testing.T) {
  99. expected := [4][5]float64{
  100. {1, 0, 0, 0, 0},
  101. {0, 1, 0, 0, 0},
  102. {0, 0, 1, 0, 0},
  103. {0, 0, 0, 0.5, 0.5},
  104. }
  105. m := colorm.ColorM{}
  106. m.Translate(0, 0, 0, 1)
  107. m.Scale(1, 1, 1, 0.5)
  108. for i := 0; i < 4; i++ {
  109. for j := 0; j < 5; j++ {
  110. got := m.Element(i, j)
  111. want := expected[i][j]
  112. if want != got {
  113. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  114. }
  115. }
  116. }
  117. }
  118. func TestColorMMonochrome(t *testing.T) {
  119. expected := [4][5]float64{
  120. {0.2990, 0.5870, 0.1140, 0, 0},
  121. {0.2990, 0.5870, 0.1140, 0, 0},
  122. {0.2990, 0.5870, 0.1140, 0, 0},
  123. {0, 0, 0, 1, 0},
  124. }
  125. m := colorm.ColorM{}
  126. m.ChangeHSV(0, 0, 1)
  127. for i := 0; i < 4; i++ {
  128. for j := 0; j < 5; j++ {
  129. got := m.Element(i, j)
  130. want := expected[i][j]
  131. if math.Abs(want-got) > 0.0001 {
  132. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  133. }
  134. }
  135. }
  136. }
  137. func TestColorMConcatSelf(t *testing.T) {
  138. expected := [4][5]float64{
  139. {30, 40, 30, 25, 30},
  140. {40, 54, 43, 37, 37},
  141. {30, 43, 51, 39, 34},
  142. {25, 37, 39, 46, 36},
  143. }
  144. m := colorm.ColorM{}
  145. for i := 0; i < 4; i++ {
  146. for j := 0; j < 5; j++ {
  147. m.SetElement(i, j, float64((i+j)%5+1))
  148. }
  149. }
  150. m.Concat(m)
  151. for i := 0; i < 4; i++ {
  152. for j := 0; j < 5; j++ {
  153. got := m.Element(i, j)
  154. want := expected[i][j]
  155. if want != got {
  156. t.Errorf("m.Element(%d, %d) = %f, want %f", i, j, got, want)
  157. }
  158. }
  159. }
  160. }
  161. func absDiffU32(x, y uint32) uint32 {
  162. if x < y {
  163. return y - x
  164. }
  165. return x - y
  166. }
  167. func TestColorMApply(t *testing.T) {
  168. mono := colorm.ColorM{}
  169. mono.ChangeHSV(0, 0, 1)
  170. shiny := colorm.ColorM{}
  171. shiny.Translate(1, 1, 1, 0)
  172. shift := colorm.ColorM{}
  173. shift.Translate(0.5, 0.5, 0.5, 0.5)
  174. cases := []struct {
  175. ColorM colorm.ColorM
  176. In color.Color
  177. Out color.Color
  178. Delta uint32
  179. }{
  180. {
  181. ColorM: colorm.ColorM{},
  182. In: color.RGBA{R: 1, G: 2, B: 3, A: 4},
  183. Out: color.RGBA{R: 1, G: 2, B: 3, A: 4},
  184. Delta: 0x101,
  185. },
  186. {
  187. ColorM: mono,
  188. In: color.NRGBA{R: 0xff, G: 0xff, B: 0xff},
  189. Out: color.Transparent,
  190. Delta: 0x101,
  191. },
  192. {
  193. ColorM: mono,
  194. In: color.RGBA{R: 0xff, A: 0xff},
  195. Out: color.RGBA{R: 0x4c, G: 0x4c, B: 0x4c, A: 0xff},
  196. Delta: 0x101,
  197. },
  198. {
  199. ColorM: shiny,
  200. In: color.RGBA{R: 0x80, G: 0x90, B: 0xa0, A: 0xb0},
  201. Out: color.RGBA{R: 0xb0, G: 0xb0, B: 0xb0, A: 0xb0},
  202. Delta: 1,
  203. },
  204. {
  205. ColorM: shift,
  206. In: color.RGBA{},
  207. Out: color.RGBA{R: 0x40, G: 0x40, B: 0x40, A: 0x80},
  208. Delta: 0x101,
  209. },
  210. }
  211. for _, c := range cases {
  212. out := c.ColorM.Apply(c.In)
  213. r0, g0, b0, a0 := out.RGBA()
  214. r1, g1, b1, a1 := c.Out.RGBA()
  215. if absDiffU32(r0, r1) > c.Delta || absDiffU32(g0, g1) > c.Delta ||
  216. absDiffU32(b0, b1) > c.Delta || absDiffU32(a0, a1) > c.Delta {
  217. t.Errorf("%v.Apply(%v) = {%d, %d, %d, %d}, want {%d, %d, %d, %d}", c.ColorM, c.In, r0, g0, b0, a0, r1, g1, b1, a1)
  218. }
  219. }
  220. }
  221. // #1765
  222. func TestColorMConcat(t *testing.T) {
  223. var a, b colorm.ColorM
  224. a.SetElement(1, 2, -1)
  225. a.Concat(b)
  226. if got, want := a.Element(1, 2), -1.0; got != want {
  227. t.Errorf("got: %f, want: %f", got, want)
  228. }
  229. }