Source code: com/arranger/jarl/base/GradientManager.java
1 package com.arranger.jarl.base;
2
3 import com.jhlabs.image.Gradient;
4 import com.arranger.jarl.util.*;
5
6 import java.util.Map;
7 import java.util.HashMap;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.awt.*;
13
14
15 /**
16 * GradientManager manages all the gradients available in the system
17 */
18 public class GradientManager implements IGradientManager {
19
20 protected static final String GRADIENTS_SER = "gradients.ser";
21
22 protected String m_gradientFile;
23 protected Map m_gradientMap = new HashMap();
24 protected List m_gradientList = new ArrayList();
25
26 /**
27 * Init the available gradients
28 * @param gradientFile
29 * @throws IOException
30 */
31 public void init(String gradientFile) throws IOException {
32 m_gradientFile = gradientFile;
33 InputStream inputStream = null;
34 if (StringTools.isEmpty(gradientFile)) {
35 inputStream = getClass().getResourceAsStream('/' + GRADIENTS_SER);
36 } else {
37 inputStream = IOUtil.getInputStream(m_gradientFile);
38 }
39
40 Object[] gradients = (Object[])ObjectUtil.load(inputStream);
41 inputStream.close();
42
43 for (int index = 0; index < gradients.length; index++) {
44 Gradient gradient = (Gradient)gradients[index];
45 m_gradientList.add(gradient);
46 }
47 }
48
49 /**
50 * @param index
51 * @return the gradient by index
52 */
53 public Gradient getGradient(int index) {
54 return (Gradient)m_gradientList.get(index);
55 }
56
57 /**
58 * @param name
59 * @return the gradient by name
60 */
61 public Gradient getGradient(String name) {
62 return (Gradient)m_gradientMap.get(name);
63 }
64
65 /**
66 * @return the map of all gradients
67 */
68 public Map getGradientMap() {
69 return m_gradientMap;
70 }
71
72 /**
73 * @return a list of all gradients
74 */
75 public List getGradientList() {
76 return m_gradientList;
77 }
78
79 /**
80 * Create an interpolated gradient between grad1 and gradient two
81 * @param gradient1 first gradient
82 * @param gradient2 second gradient
83 * @param pct between 0 & 1
84 * @return an interpolated gradient
85 */
86 public Gradient interpolate(Gradient gradient1, Gradient gradient2, double pct) {
87 if (pct < 0 || pct > 1) {
88 throw new IllegalArgumentException("pct is out of bounds: " + pct);
89 }
90
91 int [] map1 = gradient1.getMap();
92 int [] map2 = gradient2.getMap();
93
94 int [] resultMap = new int[Math.max(map1.length, map2.length)];
95 for (int index = 0; index < resultMap.length; index++) {
96
97 int color1 = (index < map1.length) ? map1[index] : map1[map1.length - 1];
98 int color2 = (index < map2.length) ? map2[index] : map2[map2.length - 1];
99
100 Color newColor = PaintUtil.interpolatePaint(new Color(color1), new Color(color2), pct);
101 resultMap[index] = newColor.getRGB();
102 }
103
104 Gradient resultGradient = new Gradient();
105 resultGradient.setMap(resultMap);
106 return resultGradient;
107 }
108
109 /**
110 * Create an interpolated gradient between grad1 and gradient two
111 * @param gradient1 first gradient
112 * @param color second color
113 * @param pct between 0 & 1
114 * @return an interpolated gradient
115 */
116 public Gradient interpolate(Gradient gradient1, Color color, double pct) {
117 if (pct < 0 || pct > 1) {
118 throw new IllegalArgumentException("pct is out of bounds: " + pct);
119 }
120
121 int [] map1 = gradient1.getMap();
122
123 int [] resultMap = new int[map1.length];
124 for (int index = 0; index < resultMap.length; index++) {
125
126 int color1 = (index < map1.length) ? map1[index] : map1[map1.length - 1];
127
128 Color newColor = PaintUtil.interpolatePaint(new Color(color1), color, pct);
129 resultMap[index] = newColor.getRGB();
130 }
131
132 Gradient resultGradient = new Gradient();
133 resultGradient.setMap(resultMap);
134 return resultGradient;
135 }
136 }