questionable patch: merge of a lot of stuff from the svg branch
[org.ibex.core.git] / src / org / ibex / graphics / Paint.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 // FIXME
6 package org.ibex.graphics;
7 import java.util.*;
8
9 public abstract class Paint {
10     //public abstract void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf);
11
12     public static class SingleColorPaint extends Paint {
13         public int color;
14         public SingleColorPaint(int color) { this.color = color; }
15         /*
16         public void fillTrapezoid(float x1, float x2, float y1, float x3, float x4, float y2, PixelBuffer buf) {
17             buf.fillTrapezoid((int)Math.round(x1), (int)Math.round(x2), (int)Math.round(y1),
18                               (int)Math.round(x3), (int)Math.round(x4), (int)Math.round(y2), color);
19         }
20         */
21     }
22
23     public static class TexturePaint extends Paint {
24         Affine a, invert;
25         Picture p;
26         public TexturePaint(Picture p, Affine a) { this.p = p; this.a = a.copy(); this.invert = a.copy().invert(); }
27         //public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
28             /*
29             float x1 = invert.multiply_px(tx1, ty1);
30             float x2 = invert.multiply_px(tx2, ty1);
31             float x3 = invert.multiply_px(tx3, ty2);
32             float x4 = invert.multiply_px(tx4, ty2);
33             float y1 = invert.multiply_py(tx1, ty1);
34             float y2 = invert.multiply_py(tx3, ty2);
35             */
36             //buf.paintTrapezoid((int)tx1, (int)tx2, (int)ty1, (int)tx3, (int)tx4, (int)ty2, p, a);
37         //}
38     }
39
40     public static abstract class GradientPaint extends Paint {
41         public GradientPaint(boolean reflect, boolean repeat, Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
42             this.reflect = reflect;
43             this.repeat = repeat;
44             this.gradientTransform = gradientTransform;
45             this.stop_colors = stop_colors;
46             this.stop_offsets = stop_offsets;
47         }
48
49         Affine gradientTransform = Affine.identity();
50         boolean useBoundingBox = false;               // FIXME not supported
51         boolean patternUseBoundingBox = false;        // FIXME not supported
52         
53         // it's invalid for both of these to be true
54         boolean reflect = false;                   // FIXME not supported
55         boolean repeat = false;                    // FIXME not supported
56         int[] stop_colors;
57         float[] stop_offsets;
58         float cx, cy, r, fx, fy;
59         
60         //public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
61             /*
62             Affine a = buf.a;
63             Affine inverse = a.copy().invert();
64             float slope1 = (tx3 - tx1) / (ty2 - ty1);
65             float slope2 = (tx4 - tx2) / (ty2 - ty1);
66             for(float y=ty1; y<ty2; y++) {
67                 float _x1 = (y - ty1) * slope1 + tx1;
68                 float _x2 = (y - ty1) * slope2 + tx2;
69                 if (_x1 > _x2) { float _x0 = _x1; _x1 = _x2; _x2 = _x0; }
70                 
71                 for(float x=_x1; x<_x2; x++) {
72                     
73                     float distance = isLinear ?
74                         // length of projection of <x,y> onto the gradient vector == {<x,y> \dot {grad \over |grad|}}
75                         (x * (x2 - x1) + y * (y2 - y1)) / (float)Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) :
76                         
77                         // radial form is simple! FIXME, not quite right
78                         (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
79                         
80                     // FIXME: offsets are 0..1, not 0..length(gradient)
81                     int i = 0; for(; i<stop_offsets.length; i++) if (distance < stop_offsets[i]) break;
82
83                     // FIXME: handle points beyond the bounds
84                     if (i < 0 || i >= stop_offsets.length) continue;
85
86                     // gradate from offsets[i - 1] to offsets[i]
87                     float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
88
89                     int a = (int)((((stop_colors[i] >> 24) & 0xff) - ((stop_colors[i - 1] >> 24) & 0xff)) * percentage) +
90                         ((stop_colors[i - 1] >> 24) & 0xff);
91                     int r = (int)((((stop_colors[i] >> 16) & 0xff) - ((stop_colors[i - 1] >> 16) & 0xff)) * percentage) +
92                         ((stop_colors[i - 1] >> 16) & 0xff);
93                     int g = (int)((((stop_colors[i] >> 8) & 0xff)  - ((stop_colors[i - 1] >> 8) & 0xff)) * percentage) +
94                         ((stop_colors[i - 1] >> 8) & 0xff);
95                     int b = (int)((((stop_colors[i] >> 0) & 0xff)  - ((stop_colors[i - 1] >> 0) & 0xff)) * percentage) +
96                         ((stop_colors[i - 1] >> 0) & 0xff);
97                     int argb = (a << 24) | (r << 16) | (g << 8) | b;
98                     buf.drawPoint((int)x, (int)Math.floor(y), argb);
99                 }
100             }
101             */
102         //}
103     }
104     /*
105     public static class LinearGradientPaint extends GradientPaint {
106         public LinearGradientPaint(float x1, float y1, float x2, float y2, boolean reflect, boolean repeat,
107                                    Affine gradientTransform,  int[] stop_colors, float[] stop_offsets) {
108             super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
109             this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
110         }
111         float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
112     }
113
114     public static class RadialGradientPaint extends GradientPaint {
115         public RadialGradientPaint(float cx, float cy, float fx, float fy, float r, boolean reflect, boolean repeat,
116                                    Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
117             super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
118             this.cx = cx; this.cy = cy; this.fx = fx; this.fy = fy; this.r = r;
119         }
120             
121     }
122     */
123 }