2 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
3 package org.ibex.graphics;
6 public abstract class Paint {
7 public abstract void fillTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, PixelBuffer buf);
9 public static class SingleColorPaint extends Paint {
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);
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;
27 Affine gradientTransform = Affine.identity();
28 boolean useBoundingBox = false; // FIXME not supported
29 boolean patternUseBoundingBox = false; // FIXME not supported
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
37 public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
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; }
47 for(float x=_x1; x<_x2; x++) {
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)) :
53 // radial form is simple! FIXME, not quite right
54 (float)Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
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;
59 // FIXME: handle points beyond the bounds
60 if (i < 0 || i >= stop_offsets.length) continue;
62 // gradate from offsets[i - 1] to offsets[i]
63 float percentage = ((distance - stop_offsets[i - 1]) / (stop_offsets[i] - stop_offsets[i - 1]));
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);
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;
86 float x1 = 0, y1 = 0, x2 = 300, y2 = 300;
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;
96 float cx, cy, r, fx, fy;