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.

GradientTestCase.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.gradient;
  19. import java.awt.Color;
  20. import java.awt.geom.AffineTransform;
  21. import org.junit.Test;
  22. import static org.junit.Assert.assertArrayEquals;
  23. import static org.junit.Assert.assertEquals;
  24. import org.apache.batik.ext.awt.LinearGradientPaint;
  25. import org.apache.batik.ext.awt.RadialGradientPaint;
  26. public class GradientTestCase {
  27. private static class PatternChecker {
  28. private final Pattern pattern;
  29. PatternChecker(Pattern pattern) {
  30. this.pattern = pattern;
  31. }
  32. public PatternChecker type(int expectedType) {
  33. assertEquals(expectedType, pattern.getPatternType());
  34. return this;
  35. }
  36. public PatternChecker matrix(Double... expectedMatrix) {
  37. assertArrayEquals(expectedMatrix, pattern.getMatrix().toArray());
  38. return this;
  39. }
  40. public ShadingChecker shading() {
  41. return new ShadingChecker(pattern.getShading());
  42. }
  43. }
  44. private static class ShadingChecker {
  45. private final Shading shading;
  46. ShadingChecker(Shading shading) {
  47. this.shading = shading;
  48. }
  49. ShadingChecker shadingType(int expectedShadingType) {
  50. assertEquals(expectedShadingType, shading.getShadingType());
  51. return this;
  52. }
  53. ShadingChecker coords(double... expectedCoords) {
  54. double[] coords = new double[shading.getCoords().size()];
  55. int index = 0;
  56. for (Double d : shading.getCoords()) {
  57. coords[index++] = d;
  58. }
  59. assertArrayEquals(expectedCoords, coords, 0.0001);
  60. return this;
  61. }
  62. ShadingChecker extend(Boolean... expectedExtend) {
  63. assertArrayEquals(expectedExtend, shading.getExtend().toArray());
  64. return this;
  65. }
  66. FunctionChecker function() {
  67. return new FunctionChecker(shading.getFunction());
  68. }
  69. }
  70. private static class FunctionChecker {
  71. private final Function function;
  72. FunctionChecker(Function function) {
  73. this.function = function;
  74. }
  75. FunctionChecker functionType(int expectedFunctionType) {
  76. assertEquals(expectedFunctionType, function.getFunctionType());
  77. return this;
  78. }
  79. FunctionChecker domain(Double... expectedDomain) {
  80. assertArrayEquals(expectedDomain, function.getDomain().toArray());
  81. return this;
  82. }
  83. FunctionChecker bounds(Float... expectedBounds) {
  84. assertArrayEquals(expectedBounds, function.getBounds().toArray());
  85. return this;
  86. }
  87. FunctionChecker encode(Double... expectedEncode) {
  88. assertArrayEquals(expectedEncode, function.getEncode().toArray());
  89. return this;
  90. }
  91. FunctionChecker cZero(float... expectedCZero) {
  92. assertArrayEquals(expectedCZero, function.getCZero(), 0f);
  93. return this;
  94. }
  95. FunctionChecker cOne(float... expectedCOne) {
  96. assertArrayEquals(expectedCOne, function.getCOne(), 0f);
  97. return this;
  98. }
  99. FunctionChecker functions(int expectedFunctionCount) {
  100. assertEquals(expectedFunctionCount, function.getFunctions().size());
  101. return this;
  102. }
  103. FunctionChecker function(int index) {
  104. return new FunctionChecker(function.getFunctions().get(index));
  105. }
  106. }
  107. @Test
  108. public void simpleLinearGradient() {
  109. LinearGradientPaint gradient = new LinearGradientPaint(0f, 0f, 100f, 100f,
  110. fractions(0f, 1f), colors(Color.BLUE, Color.RED));
  111. Pattern pattern = GradientMaker.makeLinearGradient(gradient,
  112. AffineTransform.getTranslateInstance(10.0, 20.0),
  113. AffineTransform.getScaleInstance(100.0, 1000.0));
  114. PatternChecker patternChecker = new PatternChecker(pattern)
  115. .type(2)
  116. .matrix(100.0, 0.0, 0.0, 1000.0, 10.0, 20.0);
  117. ShadingChecker shadingChecker = patternChecker.shading()
  118. .shadingType(2)
  119. .coords(0.0, 0.0, 100.0, 100.0)
  120. .extend(true, true);
  121. FunctionChecker functionChecker = shadingChecker.function()
  122. .functionType(3)
  123. .domain(0.0, 1.0)
  124. .bounds()
  125. .encode(0.0, 1.0)
  126. .functions(1);
  127. functionChecker.function(0)
  128. .functionType(2)
  129. .domain(0.0, 1.0)
  130. .cZero(0f, 0f, 1f)
  131. .cOne(1f, 0f, 0f)
  132. .functions(0);
  133. }
  134. @Test
  135. public void simpleRadialGradient() {
  136. RadialGradientPaint gradient = new RadialGradientPaint(100, 200, 50,
  137. fractions(0f, 1f), colors(Color.BLUE, Color.RED));
  138. Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform());
  139. PatternChecker patternChecker = new PatternChecker(pattern).type(2);
  140. ShadingChecker shadingChecker = patternChecker.shading()
  141. .shadingType(3)
  142. .coords(100.0, 200.0, 0.0, 100.0, 200.0, 50.0)
  143. .extend(true, true);
  144. FunctionChecker functionChecker = shadingChecker.function()
  145. .functionType(3)
  146. .domain(0.0, 1.0)
  147. .bounds()
  148. .encode(0.0, 1.0)
  149. .functions(1);
  150. functionChecker.function(0)
  151. .functionType(2)
  152. .domain(0.0, 1.0)
  153. .cZero(0f, 0f, 1f)
  154. .cOne(1f, 0f, 0f)
  155. .functions(0);
  156. }
  157. @Test
  158. public void threeColorLinearGradient() {
  159. LinearGradientPaint gradient = new LinearGradientPaint(0f, 10f, 20f, 30f,
  160. fractions(0f, 0.5f, 1f), colors(Color.BLUE, Color.RED, Color.GREEN));
  161. Pattern pattern = GradientMaker.makeLinearGradient(gradient, new AffineTransform(), new AffineTransform());
  162. PatternChecker patternChecker = new PatternChecker(pattern)
  163. .type(2)
  164. .matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
  165. ShadingChecker shadingChecker = patternChecker.shading()
  166. .shadingType(2)
  167. .coords(0.0, 10.0, 20.0, 30.0)
  168. .extend(true, true);
  169. FunctionChecker functionChecker = shadingChecker.function()
  170. .functionType(3)
  171. .domain(0.0, 1.0)
  172. .bounds(0.5f)
  173. .encode(0.0, 1.0, 0.0, 1.0)
  174. .functions(2);
  175. functionChecker.function(0)
  176. .functionType(2)
  177. .domain(0.0, 1.0)
  178. .cZero(0f, 0f, 1f)
  179. .cOne(1f, 0f, 0f)
  180. .functions(0);
  181. functionChecker.function(1)
  182. .functionType(2)
  183. .domain(0.0, 1.0)
  184. .cZero(1f, 0f, 0f)
  185. .cOne(0f, 1f, 0f)
  186. .functions(0);
  187. }
  188. @Test
  189. public void fourColorRadialGradientNonZeroFirstStop() {
  190. RadialGradientPaint gradient = new RadialGradientPaint(100, 200, 50, 110, 220,
  191. fractions(0.2f, 0.5f, 0.7f, 1f), colors(Color.BLUE, Color.RED, Color.GREEN, Color.WHITE));
  192. Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform());
  193. ShadingChecker shadingChecker = new PatternChecker(pattern).shading()
  194. .coords(110.0, 220.0, 0.0, 100.0, 200.0, 50.0);
  195. FunctionChecker functionChecker = shadingChecker.function()
  196. .functionType(3)
  197. .bounds(0.2f, 0.5f, 0.7f)
  198. .encode(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
  199. .functions(4);
  200. functionChecker.function(0)
  201. .functionType(2)
  202. .cZero(0f, 0f, 1f)
  203. .cOne(0f, 0f, 1f);
  204. functionChecker.function(1)
  205. .functionType(2)
  206. .cZero(0f, 0f, 1f)
  207. .cOne(1f, 0f, 0f);
  208. functionChecker.function(2)
  209. .functionType(2)
  210. .cZero(1f, 0f, 0f)
  211. .cOne(0f, 1f, 0f);
  212. functionChecker.function(3)
  213. .functionType(2)
  214. .cZero(0f, 1f, 0f)
  215. .cOne(1f, 1f, 1f);
  216. }
  217. @Test
  218. public void fourColorRadialGradientNonZeroLastStopFocalOut() {
  219. RadialGradientPaint gradient = new RadialGradientPaint(0, 0, 100, 100, 100,
  220. fractions(0f, 0.3f, 0.6f, 0.9f), colors(Color.WHITE, Color.RED, Color.GREEN, Color.BLUE));
  221. Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform());
  222. ShadingChecker shadingChecker = new PatternChecker(pattern).shading()
  223. .coords(70.7036, 70.7036, 0.0, 0.0, 0.0, 100.0);
  224. FunctionChecker functionChecker = shadingChecker.function()
  225. .functionType(3)
  226. .bounds(0.3f, 0.6f, 0.9f)
  227. .encode(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
  228. .functions(4);
  229. functionChecker.function(0)
  230. .functionType(2)
  231. .cZero(1f, 1f, 1f)
  232. .cOne(1f, 0f, 0f);
  233. functionChecker.function(1)
  234. .functionType(2)
  235. .cZero(1f, 0f, 0f)
  236. .cOne(0f, 1f, 0f);
  237. functionChecker.function(2)
  238. .functionType(2)
  239. .cZero(0f, 1f, 0f)
  240. .cOne(0f, 0f, 1f);
  241. functionChecker.function(3)
  242. .functionType(2)
  243. .cZero(0f, 0f, 1f)
  244. .cOne(0f, 0f, 1f);
  245. }
  246. private float[] fractions(float... fractions) {
  247. return fractions;
  248. }
  249. private Color[] colors(Color... colors) {
  250. return colors;
  251. }
  252. @Test
  253. public void testMakeBounds() {
  254. RadialGradientPaint gradient = new RadialGradientPaint(0, 0, 100, 100, 100,
  255. fractions(0f, 1f, 0.9f), colors(Color.WHITE, Color.RED, Color.GREEN));
  256. Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform());
  257. ShadingChecker shadingChecker = new PatternChecker(pattern).shading()
  258. .coords(70.7036, 70.7036, 0.0, 0.0, 0.0, 100.0);
  259. shadingChecker.function()
  260. .functionType(3)
  261. .bounds(1f, 0.9f)
  262. .encode(0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
  263. .functions(3);
  264. }
  265. }