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 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  }