]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/color/Graphics2DWithColorFilter.java
Render elements using custom color filters
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / color / Graphics2DWithColorFilter.java
1 /*******************************************************************************
2  * Copyright (c) 2020 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.g2d.color;
13
14 import java.awt.Color;
15 import java.awt.Composite;
16 import java.awt.Font;
17 import java.awt.FontMetrics;
18 import java.awt.Graphics;
19 import java.awt.Graphics2D;
20 import java.awt.GraphicsConfiguration;
21 import java.awt.Image;
22 import java.awt.Paint;
23 import java.awt.Polygon;
24 import java.awt.Rectangle;
25 import java.awt.RenderingHints;
26 import java.awt.RenderingHints.Key;
27 import java.awt.Shape;
28 import java.awt.Stroke;
29 import java.awt.font.FontRenderContext;
30 import java.awt.font.GlyphVector;
31 import java.awt.geom.AffineTransform;
32 import java.awt.image.BufferedImage;
33 import java.awt.image.BufferedImageOp;
34 import java.awt.image.ImageObserver;
35 import java.awt.image.RenderedImage;
36 import java.awt.image.renderable.RenderableImage;
37 import java.text.AttributedCharacterIterator;
38 import java.util.Map;
39
40 import com.kitfox.svg.batik.LinearGradientPaint;
41 import com.kitfox.svg.batik.RadialGradientPaint;
42
43 /**
44  * Modified methods: setPaint, setColor.
45  * Colors of bitmap images must be handled externally.
46  */
47 public class Graphics2DWithColorFilter extends Graphics2D {
48     private Graphics2D g2d;
49     private ColorFilter colorFilter;
50
51     public Graphics2DWithColorFilter(Graphics2D g2d, ColorFilter colorFilter) {
52         this.g2d = g2d;
53         this.colorFilter = colorFilter;
54     }
55
56     public void setColorFilter(ColorFilter colorFilter) {
57         this.colorFilter = colorFilter;
58     }
59
60     public Color filter(Color c) {
61         if (colorFilter != null) {
62             return colorFilter.filter(c);
63         } else {
64             return c;
65         }
66     }
67
68     public void setColor(Color c) {
69         g2d.setColor(filter(c));
70     }
71
72     public void setPaint(Paint paint) {
73         if (paint instanceof Color) {
74             g2d.setColor(filter((Color)paint));
75         } else if (paint instanceof LinearGradientPaint) {
76             LinearGradientPaint lgp = (LinearGradientPaint)paint;
77             Color[] colors = new Color[lgp.getColors().length];
78             for (int i = 0; i < lgp.getColors().length; i++) {
79                 colors[i] = filter(lgp.getColors()[i]);
80             }
81             LinearGradientPaint lgp2 = new LinearGradientPaint(lgp.getStartPoint(), lgp.getEndPoint(), lgp.getFractions(), colors);
82             g2d.setPaint(lgp2);
83         } else if (paint instanceof RadialGradientPaint) {
84             RadialGradientPaint rgp = (RadialGradientPaint)paint;
85             Color[] colors = new Color[rgp.getColors().length];
86             for (int i = 0; i < rgp.getColors().length; i++) {
87                 colors[i] = filter(rgp.getColors()[i]);
88             }
89             RadialGradientPaint rgp2 = new RadialGradientPaint(rgp.getCenterPoint(), rgp.getRadius(), rgp.getFractions(), colors);
90             g2d.setPaint(rgp2);
91         } else {
92             // Not implemented yet. Show red!
93             Color c2 = new Color(255, 0, 0, 255);
94             g2d.setPaint(c2);
95         }
96     }
97
98     public int hashCode() {
99         return g2d.hashCode();
100     }
101     public boolean equals(Object obj) {
102         return g2d.equals(obj);
103     }
104     public Graphics create() {
105         return g2d.create();
106     }
107     public Graphics create(int x, int y, int width, int height) {
108         return g2d.create(x, y, width, height);
109     }
110     public Color getColor() {
111         return g2d.getColor();
112     }
113     public void setPaintMode() {
114         g2d.setPaintMode();
115     }
116     public void setXORMode(Color c1) {
117         g2d.setXORMode(c1);
118     }
119     public Font getFont() {
120         return g2d.getFont();
121     }
122     public void setFont(Font font) {
123         g2d.setFont(font);
124     }
125     public FontMetrics getFontMetrics() {
126         return g2d.getFontMetrics();
127     }
128     public FontMetrics getFontMetrics(Font f) {
129         return g2d.getFontMetrics(f);
130     }
131     public Rectangle getClipBounds() {
132         return g2d.getClipBounds();
133     }
134     public void clipRect(int x, int y, int width, int height) {
135         g2d.clipRect(x, y, width, height);
136     }
137     public void setClip(int x, int y, int width, int height) {
138         g2d.setClip(x, y, width, height);
139     }
140     public Shape getClip() {
141         return g2d.getClip();
142     }
143     public void setClip(Shape clip) {
144         g2d.setClip(clip);
145     }
146     public void copyArea(int x, int y, int width, int height, int dx, int dy) {
147         g2d.copyArea(x, y, width, height, dx, dy);
148     }
149     public void drawLine(int x1, int y1, int x2, int y2) {
150         g2d.drawLine(x1, y1, x2, y2);
151     }
152     public void fillRect(int x, int y, int width, int height) {
153         g2d.fillRect(x, y, width, height);
154     }
155     public void drawRect(int x, int y, int width, int height) {
156         g2d.drawRect(x, y, width, height);
157     }
158     public void draw3DRect(int x, int y, int width, int height, boolean raised) {
159         g2d.draw3DRect(x, y, width, height, raised);
160     }
161     public void clearRect(int x, int y, int width, int height) {
162         g2d.clearRect(x, y, width, height);
163     }
164     public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
165         g2d.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
166     }
167     public void fill3DRect(int x, int y, int width, int height, boolean raised) {
168         g2d.fill3DRect(x, y, width, height, raised);
169     }
170     public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
171         g2d.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
172     }
173     public void draw(Shape s) {
174         g2d.draw(s);
175     }
176     public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
177         return g2d.drawImage(img, xform, obs);
178     }
179     public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
180         g2d.drawImage(img, op, x, y);
181     }
182     public void drawOval(int x, int y, int width, int height) {
183         g2d.drawOval(x, y, width, height);
184     }
185     public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
186         g2d.drawRenderedImage(img, xform);
187     }
188     public void fillOval(int x, int y, int width, int height) {
189         g2d.fillOval(x, y, width, height);
190     }
191     public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
192         g2d.drawRenderableImage(img, xform);
193     }
194     public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
195         g2d.drawArc(x, y, width, height, startAngle, arcAngle);
196     }
197     public void drawString(String str, int x, int y) {
198         g2d.drawString(str, x, y);
199     }
200     public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
201         g2d.fillArc(x, y, width, height, startAngle, arcAngle);
202     }
203     public void drawString(String str, float x, float y) {
204         g2d.drawString(str, x, y);
205     }
206     public void drawString(AttributedCharacterIterator iterator, int x, int y) {
207         g2d.drawString(iterator, x, y);
208     }
209     public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
210         g2d.drawPolyline(xPoints, yPoints, nPoints);
211     }
212     public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
213         g2d.drawPolygon(xPoints, yPoints, nPoints);
214     }
215     public void drawString(AttributedCharacterIterator iterator, float x, float y) {
216         g2d.drawString(iterator, x, y);
217     }
218     public void drawPolygon(Polygon p) {
219         g2d.drawPolygon(p);
220     }
221     public void drawGlyphVector(GlyphVector g, float x, float y) {
222         g2d.drawGlyphVector(g, x, y);
223     }
224     public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
225         g2d.fillPolygon(xPoints, yPoints, nPoints);
226     }
227     public void fill(Shape s) {
228         g2d.fill(s);
229     }
230     public void fillPolygon(Polygon p) {
231         g2d.fillPolygon(p);
232     }
233     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
234         return g2d.hit(rect, s, onStroke);
235     }
236     public GraphicsConfiguration getDeviceConfiguration() {
237         return g2d.getDeviceConfiguration();
238     }
239     public void setComposite(Composite comp) {
240         g2d.setComposite(comp);
241     }
242     public void drawChars(char[] data, int offset, int length, int x, int y) {
243         g2d.drawChars(data, offset, length, x, y);
244     }
245     public void drawBytes(byte[] data, int offset, int length, int x, int y) {
246         g2d.drawBytes(data, offset, length, x, y);
247     }
248     public void setStroke(Stroke s) {
249         g2d.setStroke(s);
250     }
251     public void setRenderingHint(Key hintKey, Object hintValue) {
252         g2d.setRenderingHint(hintKey, hintValue);
253     }
254     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
255         return g2d.drawImage(img, x, y, observer);
256     }
257     public Object getRenderingHint(Key hintKey) {
258         return g2d.getRenderingHint(hintKey);
259     }
260     public void setRenderingHints(Map<?, ?> hints) {
261         g2d.setRenderingHints(hints);
262     }
263     public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
264         return g2d.drawImage(img, x, y, width, height, observer);
265     }
266     public void addRenderingHints(Map<?, ?> hints) {
267         g2d.addRenderingHints(hints);
268     }
269     public RenderingHints getRenderingHints() {
270         return g2d.getRenderingHints();
271     }
272     public void translate(int x, int y) {
273         g2d.translate(x, y);
274     }
275     public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
276         return g2d.drawImage(img, x, y, bgcolor, observer);
277     }
278     public void translate(double tx, double ty) {
279         g2d.translate(tx, ty);
280     }
281     public void rotate(double theta) {
282         g2d.rotate(theta);
283     }
284     public void rotate(double theta, double x, double y) {
285         g2d.rotate(theta, x, y);
286     }
287     public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
288         return g2d.drawImage(img, x, y, width, height, bgcolor, observer);
289     }
290     public void scale(double sx, double sy) {
291         g2d.scale(sx, sy);
292     }
293     public void shear(double shx, double shy) {
294         g2d.shear(shx, shy);
295     }
296     public void transform(AffineTransform Tx) {
297         g2d.transform(Tx);
298     }
299     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
300             ImageObserver observer) {
301         return g2d.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
302     }
303     public void setTransform(AffineTransform Tx) {
304         g2d.setTransform(Tx);
305     }
306     public AffineTransform getTransform() {
307         return g2d.getTransform();
308     }
309     public Paint getPaint() {
310         return g2d.getPaint();
311     }
312     public Composite getComposite() {
313         return g2d.getComposite();
314     }
315     public void setBackground(Color color) {
316         g2d.setBackground(color);
317     }
318     public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2,
319             Color bgcolor, ImageObserver observer) {
320         return g2d.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer);
321     }
322     public Color getBackground() {
323         return g2d.getBackground();
324     }
325     public Stroke getStroke() {
326         return g2d.getStroke();
327     }
328     public void clip(Shape s) {
329         g2d.clip(s);
330     }
331     public FontRenderContext getFontRenderContext() {
332         return g2d.getFontRenderContext();
333     }
334     public void dispose() {
335         g2d.dispose();
336     }
337     @SuppressWarnings("deprecation")
338     public void finalize() {
339         g2d.finalize();
340     }
341     public String toString() {
342         return g2d.toString();
343     }
344     @SuppressWarnings("deprecation")
345     public Rectangle getClipRect() {
346         return g2d.getClipRect();
347     }
348     public boolean hitClip(int x, int y, int width, int height) {
349         return g2d.hitClip(x, y, width, height);
350     }
351     public Rectangle getClipBounds(Rectangle r) {
352         return g2d.getClipBounds(r);
353     }
354 }