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.
6 package org.ibex.graphics;
9 public abstract class Paint {
10 public abstract void fillTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, PixelBuffer buf);
12 public static class SingleColorPaint extends Paint {
14 public SingleColorPaint(int color) { this.color = color; }
15 public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, PixelBuffer buf) {
16 buf.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
22 public static abstract class GradientPaint extends Paint {
23 public GradientPaint(boolean reflect, boolean repeat, Affine gradientTransform,
24 int[] stop_colors, float[] stop_offsets) {
25 this.reflect = reflect; this.repeat = repeat;
26 this.gradientTransform = gradientTransform;
27 this.stop_colors = stop_colors;
28 this.stop_offsets = stop_offsets;
30 Affine gradientTransform = Affine.identity();
31 boolean useBoundingBox = false; // FIXME not supported
32 boolean patternUseBoundingBox = false; // FIXME not supported
34 // it's invalid for both of these to be true
35 boolean reflect = false; // FIXME not supported
36 boolean repeat = false; // FIXME not supported
40 public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
42 Affine inverse = a.copy().invert();
43 float slope1 = (tx3 - tx1) / (ty2 - ty1);
44 float slope2 = (tx4 - tx2) / (ty2 - ty1);
45 for(float y=ty1; y<ty2; y++) {
46 float _x1 = (y - ty1) * slope1 + tx1;
47 float _x2 = (y - ty1) * slope2 + tx2;
48 if (_x1 > _x2) { float _x0 = _x1; _x1 = _x2; _x2 = _x0; }
50 for(float x=_x1; x<_x2; x++) {
52 float distance = isLinear ?
53 // length of projection of <x,y> onto the gradient vector == {<x,y> \dot {grad \over |grad|}}
54 (x * (x2 - x1) + y * (y2 - y1)) / (float)Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) :
56 // radial form is simple! FIXME, not quite right
57 (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
59 // FIXME: offsets are 0..1, not 0..length(gradient)
60 int i = 0; for(; i<stop_offsets.length; i++) if (distance < stop_offsets[i]) break;
62 // FIXME: handle points beyond the bounds
63 if (i < 0 || i >= stop_offsets.length) continue;
65 // gradate from offsets[i - 1] to offsets[i]
66 float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
68 int a = (int)((((stop_colors[i] >> 24) & 0xff) - ((stop_colors[i - 1] >> 24) & 0xff)) * percentage) +
69 ((stop_colors[i - 1] >> 24) & 0xff);
70 int r = (int)((((stop_colors[i] >> 16) & 0xff) - ((stop_colors[i - 1] >> 16) & 0xff)) * percentage) +
71 ((stop_colors[i - 1] >> 16) & 0xff);
72 int g = (int)((((stop_colors[i] >> 8) & 0xff) - ((stop_colors[i - 1] >> 8) & 0xff)) * percentage) +
73 ((stop_colors[i - 1] >> 8) & 0xff);
74 int b = (int)((((stop_colors[i] >> 0) & 0xff) - ((stop_colors[i - 1] >> 0) & 0xff)) * percentage) +
75 ((stop_colors[i - 1] >> 0) & 0xff);
76 int argb = (a << 24) | (r << 16) | (g << 8) | b;
77 buf.drawPoint((int)x, (int)Math.floor(y), argb);
83 public static class LinearGradientPaint extends GradientPaint {
84 public LinearGradientPaint(float x1, float y1, float x2, float y2, boolean reflect, boolean repeat,
85 Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
86 super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
87 this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
89 float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
92 public static class RadialGradientPaint extends GradientPaint {
93 public RadialGradientPaint(float cx, float cy, float fx, float fy, float r, boolean reflect, boolean repeat,
94 Affine gradientTransform, int[] stop_colors, float[] stop_offsets) {
95 super(reflect, repeat, gradientTransform, stop_colors, stop_offsets);
96 this.cx = cx; this.cy = cy; this.fx = fx; this.fy = fy; this.r = r;
99 float cx, cy, r, fx, fy;