1 | package org.apollo.gui;
|
---|
2 |
|
---|
3 | import java.awt.Rectangle;
|
---|
4 | import java.awt.image.ColorModel;
|
---|
5 | import 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 | */
|
---|
15 | public 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 |
|
---|
65 | if (raster == null) {
|
---|
66 | throw new NullPointerException("raster");
|
---|
67 | } else if (colorModel == null) {
|
---|
68 | throw new NullPointerException("raster");
|
---|
69 | }
|
---|
70 |
|
---|
71 | int endcol = x + width;
|
---|
72 | int endrow = y + height;
|
---|
73 |
|
---|
74 | // Clamp bounds
|
---|
75 | if (x < 0) x = 0;
|
---|
76 | if (y < 0) y = 0;
|
---|
77 | if (endcol >= raster.getWidth()) endcol = raster.getWidth() - 1;
|
---|
78 | if (endrow >= raster.getHeight()) endrow = raster.getHeight() - 1;
|
---|
79 |
|
---|
80 | // Safe note: If endrow <= y or endcol <= x then the loops wont be entered
|
---|
81 |
|
---|
82 | boolean offsetStart = false;
|
---|
83 |
|
---|
84 | Object colorData = colorModel.getDataElements(rgb, null);
|
---|
85 |
|
---|
86 | for (int row = y; row < endrow; row++) {
|
---|
87 |
|
---|
88 |
|
---|
89 | for (int col = (offsetStart) ? x + 1 : x; col < endcol; col+=2) { // paint every second pixel
|
---|
90 |
|
---|
91 | raster.setDataElements(col, row, colorData);
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Get checker effect
|
---|
96 | offsetStart = !offsetStart;
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 | }
|
---|