View Javadoc

1   /*
2   
3       dsh-iconbundle  Bundles of variants for icon images.
4       Copyright (c) 2003-2012 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.iconbundle.impl;
25  
26  import java.awt.Color;
27  
28  import java.awt.image.Raster;
29  import java.awt.image.WritableRaster;
30  
31  /**
32   * Selection RasterOp.
33   *
34   * @author  Michael Heuer
35   * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
36   */
37  final class SelectionRasterOp
38      extends AbstractRasterOp
39  {
40      /** Pixel array size. */
41      private static final int PIXEL_ARRAY_SIZE = 4;
42  
43      /** White color component. */
44      private static final int WHITE = 255;
45  
46      /** Selection color. */
47      private final Color selectionColor;
48  
49  
50      /**
51       * Create a new selection RasterOp with the specified
52       * selection color.
53       *
54       * @param selectionColor selection color
55       */
56      public SelectionRasterOp(final Color selectionColor)
57      {
58          this.selectionColor = selectionColor;
59      }
60  
61  
62      /** {@inheritDoc} */
63      public WritableRaster filter(final Raster src, final WritableRaster dest)
64      {
65          if (selectionColor != null)
66          {
67              float[] pixel = new float[PIXEL_ARRAY_SIZE];
68              float[] selection = selectionColor.getColorComponents(new float[PIXEL_ARRAY_SIZE]);
69  
70              for (int x = 0, w = src.getWidth(); x < w; x++)
71              {
72                  for (int y = 0, h = src.getHeight(); y < h; y++)
73                  {
74                      pixel = src.getPixel(x, y, pixel);
75                      pixel[0] = Math.min(pixel[0], selection[0] * WHITE);
76                      pixel[1] = Math.min(pixel[1], selection[1] * WHITE);
77                      pixel[2] = Math.min(pixel[2], selection[2] * WHITE);
78  
79                      dest.setPixel(x, y, pixel);
80                  }
81              }
82          }
83          return dest;
84      }
85  }