1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.microemu.device.swt;
21
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.RGB;
24 import org.microemu.app.ui.swt.ImageFilter;
25 import org.microemu.app.ui.swt.SwtDeviceComponent;
26 import org.microemu.device.DeviceFactory;
27
28
29
30 public final class GrayImageFilter implements ImageFilter
31 {
32
33 private double Yr, Yg, Yb;
34 private double Rr, Rg, Rb;
35 private Color foregroundColor;
36
37 public GrayImageFilter ()
38 {
39 this(0.2126d, 0.7152d, 0.0722d);
40 }
41
42
43 public GrayImageFilter (double Yr, double Yg, double Yb)
44 {
45 this.Yr = Yr;
46 this.Yg = Yg;
47 this.Yb = Yb;
48 Color backgroundColor = SwtDeviceComponent.getColor(new RGB(
49 ((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getBackgroundColor().getRed(),
50 ((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getBackgroundColor().getGreen(),
51 ((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getBackgroundColor().getBlue()));
52 foregroundColor = SwtDeviceComponent.getColor(new RGB(
53 ((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getForegroundColor().getRed(),
54 ((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getForegroundColor().getGreen(),
55 ((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getForegroundColor().getBlue()));
56 Rr = (backgroundColor.getRed() - foregroundColor.getRed()) / 256d;
57 Rg = (backgroundColor.getGreen() - foregroundColor.getGreen()) / 256d;
58 Rb = (backgroundColor.getBlue() - foregroundColor.getBlue()) / 256d;
59 }
60
61
62 public RGB filterRGB (int x, int y, RGB rgb)
63 {
64 int Y = (int)(Yr * rgb.red + Yg * rgb.green + Yb * rgb.blue) % 256;
65 if (Y > 255) {
66 Y = 255;
67 }
68
69 return new RGB(
70 (int) (Rr * Y) + foregroundColor.getRed(),
71 (int) (Rg * Y) + foregroundColor.getGreen(),
72 (int) (Rb * Y) + foregroundColor.getBlue());
73 }
74
75 }