View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 BWImageFilter implements ImageFilter
31  {
32  
33    private double Yr, Yg, Yb;
34    private Color backgroundColor;
35    private Color foregroundColor;
36  
37  
38    public BWImageFilter ()
39  	{
40      this(0.2126d, 0.7152d, 0.0722d);
41    }
42  
43  
44    public BWImageFilter (double Yr, double Yg, double Yb)
45  	{
46      this.Yr = Yr;
47      this.Yg = Yg;
48      this.Yb = Yb;
49      backgroundColor = SwtDeviceComponent.getColor(new RGB(
50      		((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getBackgroundColor().getRed(),
51      		((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getBackgroundColor().getGreen(),
52      		((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getBackgroundColor().getBlue()));
53      foregroundColor = SwtDeviceComponent.getColor(new RGB(
54      		((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getForegroundColor().getRed(),
55      		((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getForegroundColor().getGreen(),
56      		((SwtDeviceDisplay) DeviceFactory.getDevice().getDeviceDisplay()).getForegroundColor().getBlue()));
57    }
58  
59  
60    public RGB filterRGB (int x, int y, RGB rgb)
61  	{
62      int Y = (int)(Yr * rgb.red + Yg * rgb.green + Yb * rgb.blue);
63      if (Y > 127) {
64      	return backgroundColor.getRGB();
65  		} else {
66  	    return foregroundColor.getRGB();
67  		}
68    }
69  
70  }
71