reorganized file layout (part 2: edits)
[org.ibex.core.git] / src / org / ibex / graphics / Paint.java
1 // FIXME
2 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
3 package org.ibex.graphics;
4 import java.util.*;
5
6 public abstract class Paint {
7     public abstract void fillTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, PixelBuffer buf);
8
9     public static class SingleColorPaint extends Paint {
10         int color;
11         public SingleColorPaint(int color) { this.color = color; }
12         public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, PixelBuffer buf) {
13             buf.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
14         }
15     }
16
17
18     /*
19       public static abstract class GradientPaint extends Paint {
20       public GradientPaint(boolean reflect, boolean repeat, Affine gradientTransform,
21       int[] stop_colors, float[] stop_offsets) {
22       this.reflect = reflect; this.repeat = repeat;
23       this.gradientTransform = gradientTransform;
24       this.stop_colors = stop_colors;
25       this.stop_offsets = stop_offsets;
26       }
27       Affine gradientTransform = Affine.identity();
28       boolean useBoundingBox = false;            // FIXME not supported
29       boolean patternUseBoundingBox = false;     // FIXME not supported
30
31       // it's invalid for both of these to be true
32       boolean reflect = false;                   // FIXME not supported
33       boolean repeat = false;                    // FIXME not supported
34       int[] stop_colors;
35       float[] stop_offsets;
36
37       public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
38       Affine a = buf.a;
39       Affine inverse = a.copy().invert();
40       float slope1 = (tx3 - tx1) / (ty2 - ty1);
41       float slope2 = (tx4 - tx2) / (ty2 - ty1);
42       for(float y=ty1; y<ty2; y++) {
43       float _x1 = (y - ty1) * slope1 + tx1;
44       float _x2 = (y - ty1) * slope2 + tx2;
45       if (_x1 > _x2) { float _x0 = _x1; _x1 = _x2; _x2 = _x0; }
46
47       for(float x=_x1; x<_x2; x++) {
48                     
49       float distance = isLinear ?
50       // length of projection of <x,y> onto the gradient vector == {<x,y> \dot {grad \over |grad|}}
51       (x * (x2 - x1) + y * (y2 - y1)) / (float)Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) :
52                         
53       // radial form is simple! FIXME, not quite right
54       (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
55                         
56       // FIXME: offsets are 0..1, not 0..length(gradient)
57       int i = 0; for(; i<stop_offsets.length; i++) if (distance < stop_offsets[i]) break;
58
59       // FIXME: handle points beyond the bounds
60       if (i < 0 || i >= stop_offsets.length) continue;
61
62       // gradate from offsets[i - 1] to offsets[i]
63       float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
64
65       int a = (int)((((stop_colors[i] >> 24) & 0xff) - ((stop_colors[i - 1] >> 24) & 0xff)) * percentage) +
66       ((stop_colors[i - 1] >> 24) & 0xff);
67       int r = (int)((((stop_colors[i] >> 16) & 0xff) - ((stop_colors[i - 1] >> 16) & 0xff)) * percentage) +
68       ((stop_colors[i - 1] >> 16) & 0xff);
69       int g = (int)((((stop_colors[i] >> 8) & 0xff)  - ((stop_colors[i - 1] >> 8) & 0xff)) * percentage) +
70       ((stop_colors[i - 1] >> 8) & 0xff);
71       int b = (int)((((stop_colors[i] >> 0) & 0xff)  - ((stop_colors[i - 1] >> 0) & 0xff)) * percentage) +
72       ((stop_colors[i - 1] >> 0) & 0xff);
73       int argb = (a << 24) | (r << 16) | (g << 8) | b;
74       buf.drawPoint((int)x, (int)Math.floor(y), argb);
75       }
76       }
77       }
78       }
79
80       public static class LinearGradientPaint extends GradientPaint {
81       public LinearGradientPaint(float x1, float y1, float x2, float y2, boolean reflect, boolean repeat,
82       Affine gradientTransform,  int[] stop_colors, float[] stop_offsets) {
83       super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
84       this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
85       }
86       float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
87       }
88
89       public static class RadialGradientPaint extends GradientPaint {
90       public RadialGradientPaint(float cx, float cy, float fx, float fy, float r, boolean reflect, boolean repeat,
91       Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
92       super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
93       this.cx = cx; this.cy = cy; this.fx = fx; this.fy = fy; this.r = r;
94       }
95             
96       float cx, cy, r, fx, fy;
97
98       }
99     */
100
101
102 }