source: trunk/src_apollo/org/apollo/gui/FastAlphaEffect.java@ 315

Last change on this file since 315 was 315, checked in by bjn8, 16 years ago

Apollo spin-off added

File size: 2.0 KB
Line 
1package org.apollo.gui;
2
3import java.awt.Rectangle;
4import java.awt.image.ColorModel;
5import java.awt.image.WritableRaster;
6
7/**
8 * I remember this little trick back in days of DirectDraw:
9 *
10 * A fast technique for drawing a 50% alpha blend.
11 *
12 * @author Brook Novak
13 *
14 */
15public final class FastAlphaEffect {
16
17 private FastAlphaEffect() {
18 }
19
20 /**
21 * @see FastAlphaEffect#fastBlendSingleColor(WritableRaster, ColorModel, int, int, int, int, int)
22 * @param raster
23 * @param colorModel
24 * @param area
25 * @param rgb
26 */
27 public static final void fastBlendSingleColor(
28 WritableRaster raster,
29 ColorModel colorModel,
30 Rectangle area,
31 int rgb) {
32 fastBlendSingleColor(raster, colorModel, area.x, area.y, area.width, area.height, rgb);
33 }
34
35 /**
36 *
37 * @param raster
38 *
39 * @param colorModel
40 *
41 * @param x
42 *
43 * @param y
44 *
45 * @param width
46 *
47 * @param height
48 *
49 * @param rgb
50 * The color to blend.
51 *
52 *
53 * @throws NullPointerException
54 * If raster or colorModel is null
55 *
56 */
57 public static final void fastBlendSingleColor(
58 WritableRaster raster,
59 ColorModel colorModel,
60 int x, int y,
61 int width, int height,
62 int rgb) {
63
64 if (raster == null)
65 throw new NullPointerException("raster");
66 else if (colorModel == null)
67 throw new NullPointerException("raster");
68
69 int endcol = x + width;
70 int endrow = y + height;
71
72 // Clamp bounds
73 if (x < 0) x = 0;
74 if (y < 0) y = 0;
75 if (endcol >= raster.getWidth()) endcol = raster.getWidth() - 1;
76 if (endrow >= raster.getHeight()) endrow = raster.getHeight() - 1;
77
78 // Safe note: If endrow <= y or endcol <= x then the loops wont be entered
79
80 boolean offsetStart = false;
81
82 Object colorData = colorModel.getDataElements(rgb, null);
83
84 for (int row = y; row < endrow; row++) {
85
86
87 for (int col = (offsetStart) ? x + 1 : x; col < endcol; col+=2) { // paint every second pixel
88
89 raster.setDataElements(col, row, colorData);
90
91 }
92
93 // Get checker effect
94 offsetStart = !offsetStart;
95
96 }
97
98 }
99}
Note: See TracBrowser for help on using the repository browser.