-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTintFX.java
More file actions
93 lines (75 loc) · 2.64 KB
/
TintFX.java
File metadata and controls
93 lines (75 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class TintFX implements ImageFX {
private int x;
private int y;
private int width;
private int height;
private BufferedImage originalImage;
private BufferedImage tintedImage;
private int tintColor; // RGB tint color
private int tintStrength;
private int time;
private int timeChange;
private boolean active;
public TintFX(int xPos, int yPos, int w, int h, String imagePath, int tintRGB) {
x = xPos;
y = yPos;
width = w;
height = h;
tintColor = tintRGB;
tintStrength = 0;
time = 0;
timeChange = 1;
active = true;
originalImage = ImageManager.loadBufferedImage(imagePath);
if (originalImage != null) {
tintedImage = applyTint(ImageManager.copyImage(originalImage), tintColor);
}
}
private BufferedImage applyTint(BufferedImage src, int tintRGB) {
if (src == null) return null;
int imWidth = src.getWidth();
int imHeight = src.getHeight();
int tintRed = (tintRGB >> 16) & 255;
int tintGreen = (tintRGB >> 8) & 255;
int tintBlue = tintRGB & 255;
int[] pixels = new int[imWidth * imHeight];
src.getRGB(0, 0, imWidth, imHeight, pixels, 0, imWidth);
for (int i = 0; i < pixels.length; i++) {
int alpha = (pixels[i] >> 24) & 255;
int red = (pixels[i] >> 16) & 255;
int green = (pixels[i] >> 8) & 255;
int blue = pixels[i] & 255;
// Blend with tint color
red = (red + tintRed) / 2;
green = (green + tintGreen) / 2;
blue = (blue + tintBlue) / 2;
pixels[i] = (alpha << 24) | (red << 16) | (green << 8) | blue;
}
src.setRGB(0, 0, imWidth, imHeight, pixels, 0, imWidth);
return src;
}
@Override
public void update() {
if (!active) return;
time += timeChange;
// Oscillate between original and tinted
if (time < 20) {
tintStrength = 0;
} else if (time < 40) {
tintStrength = 100;
} else {
time = 0;
}
}
@Override
public void draw(Graphics2D g2) {
if (!active) return;
if (tintStrength > 0 && tintedImage != null) {
g2.drawImage(tintedImage, x, y, width, height, null);
} else if (originalImage != null) {
g2.drawImage(originalImage, x, y, width, height, null);
}
}
}