View Javadoc

1   /*
2   
3       dsh-iconbundle  Bundles of variants for icon images.
4       Copyright (c) 2003-2011 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  import java.awt.Image;
28  
29  import java.awt.image.Raster;
30  import java.awt.image.RasterOp;
31  import java.awt.image.RescaleOp;
32  import java.awt.image.BufferedImage;
33  import java.awt.image.WritableRaster;
34  
35  /**
36   * IconBundle static utility class.
37   *
38   * @author  Michael Heuer
39   * @version $Revision: 974 $ $Date: 2011-01-04 21:40:23 -0600 (Tue, 04 Jan 2011) $
40   */
41  public final class IconBundleUtils
42  {
43      /** Intensity factor for red. */
44      private static final float RED_FACTOR = 0.30f;
45  
46      /** Intensity factor for green. */
47      private static final float GREEN_FACTOR = 0.59f;
48  
49      /** Intensity factor for blue. */
50      private static final float BLUE_FACTOR = 0.11f;
51  
52      /** Pixel array size. */
53      private static final int PIXEL_ARRAY_SIZE = 4;
54  
55      /** Medium grey color component. */
56      private static final int MEDIUM_GREY = 127;
57  
58      /** Disabled pattern factor. */
59      private static final float DISABLED_PATTERN_FACTOR = 0.7f;
60  
61      /**
62       * RescaleOp for saturating active images.
63       */
64      private static final RescaleOp ACTIVE_OP = new RescaleOp(new float[] { 0.8f, 0.8f, 0.8f, 1.0f },
65                                                               new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
66                                                               null);
67      //private static final RescaleOp ACTIVE_OP = new RescaleOp(0.8f, 0.0f, null);
68  
69      /**
70       * RescaleOp for saturating mouseover images.
71       */
72      private static final RescaleOp MOUSEOVER_OP = new RescaleOp(new float[] { 1.2f, 1.2f, 1.2f, 1.0f },
73                                                                  new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
74                                                                  null);
75      //private static final RescaleOp MOUSEOVER_OP = new RescaleOp(1.2f, 0.0f, null);
76  
77      /**
78       * RescaleOp for creating translucent dragging images.
79       */
80      private static final RescaleOp DRAGGING_OP = new RescaleOp(new float[] { 1.0f, 1.0f, 1.0f, 0.2f },
81                                                                 new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
82                                                                 null);
83  
84      /**
85       * RescaleOp for desaturating disabled images.
86       */
87      private static final RescaleOp DISABLED_SATURATION_OP = new RescaleOp(new float[] { 0.8f, 0.8f, 0.8f, 1.0f },
88                                                                            new float[] { 0.0f, 0.0f, 0.0f, 0.0f },
89                                                                           null);
90      //private static final RescaleOp DISABLED_SATURATION_OP = new RescaleOp(0.8f, 0.0f, null);
91  
92  
93      /**
94       * RasterOp for creating the pattern overlay for disabled images.
95       */
96      private static final RasterOp DISABLED_PATTERN_OP = new AbstractRasterOp()
97          {
98  
99              /** {@inheritDoc} */
100             public WritableRaster filter(final Raster src, final WritableRaster dest)
101             {
102                 float intensity;
103                 float[] pixel = new float[PIXEL_ARRAY_SIZE];
104 
105                 for (int x = 0, w = src.getWidth(); x < w; x++)
106                 {
107                     for (int y = 0, h = src.getHeight(); y < h; y++)
108                     {
109                         pixel = src.getPixel(x, y, pixel);
110                         intensity = calculateIntensity(pixel[0], pixel[1], pixel[2]);
111 
112                         if (((x + y) % 2) == 0)
113                         {
114                             pixel[0] = (intensity / 2) + MEDIUM_GREY;
115                             pixel[1] = (intensity / 2) + MEDIUM_GREY;
116                             pixel[2] = (intensity / 2) + MEDIUM_GREY;
117                         }
118                         else
119                         {
120                             pixel[0] *= DISABLED_PATTERN_FACTOR;
121                             pixel[1] *= DISABLED_PATTERN_FACTOR;
122                             pixel[2] *= DISABLED_PATTERN_FACTOR;
123                         }
124                         dest.setPixel(x, y, pixel);
125                     }
126                 }
127                 return dest;
128             }
129         };
130 
131 
132     /**
133      * Private constructor.
134      */
135     private IconBundleUtils()
136     {
137         // empty
138     }
139 
140 
141     /**
142      * Calculate a measure of intensity for the specified RGB values.
143      *
144      * @param r red value
145      * @param g green value
146      * @param b blue value
147      * @return a measure of intensity for the specified RGB values
148      */
149     private static float calculateIntensity(final float r, final float g, final float b)
150     {
151         return (float) (r * RED_FACTOR + g * GREEN_FACTOR + b * BLUE_FACTOR);
152     }
153 
154     /**
155      * Make the specified source image active.
156      *
157      * @param src source image
158      * @return filtered source image
159      */
160     public static Image makeActive(final BufferedImage src)
161     {
162         /*
163         BufferedImage dest;
164         try
165         {
166             dest = new BufferedImage(src.getColorModel(),
167                                                src.copyData(null),
168                                                src.isAlphaPremultiplied(),
169                                                null);
170         }
171         catch (IllegalArgumentException e)
172         {
173             dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
174             Graphics2D graphics = dest.createGraphics();
175             graphics.drawImage(src, 0, 0, null);
176             graphics.dispose();
177         }
178         */
179         BufferedImage dest = new BufferedImage(src.getColorModel(),
180                                                src.copyData(null),
181                                                src.isAlphaPremultiplied(),
182                                                null);
183         ACTIVE_OP.filter(src, dest);
184         return dest;
185     }
186 
187     /**
188      * Make the specified source image disabled.
189      *
190      * @param src source image
191      * @return filtered source image
192      */
193     public static Image makeDisabled(final BufferedImage src)
194     {
195         BufferedImage dest = new BufferedImage(src.getColorModel(),
196                                                src.copyData(null),
197                                                src.isAlphaPremultiplied(),
198                                                null);
199         DISABLED_SATURATION_OP.filter(src, dest);
200         DISABLED_PATTERN_OP.filter(src.getRaster(), dest.getRaster());
201         return dest;
202     }
203 
204     /**
205      * Make the specified source image dragging.
206      *
207      * @param src source image
208      * @return filtered source image
209      */
210     public static Image makeDragging(final BufferedImage src)
211     {
212         BufferedImage dest = new BufferedImage(src.getColorModel(),
213                                                src.copyData(null),
214                                                src.isAlphaPremultiplied(),
215                                                null);
216         DRAGGING_OP.filter(src, dest);
217         return dest;
218     }
219 
220     /**
221      * Make the specified source image mouseover.
222      *
223      * @param src source image
224      * @return filtered source image
225      */
226     public static Image makeMouseover(final BufferedImage src)
227     {
228         BufferedImage dest = new BufferedImage(src.getColorModel(),
229                                                src.copyData(null),
230                                                src.isAlphaPremultiplied(),
231                                                null);
232         MOUSEOVER_OP.filter(src, dest);
233         return dest;
234     }
235 
236     /**
237      * Make the specified source image selected.
238      *
239      * @param src source image
240      * @param selectionColor selection color
241      * @return filtered source image
242      */
243     public static Image makeSelected(final BufferedImage src, final Color selectionColor)
244     {
245         BufferedImage dest = new BufferedImage(src.getColorModel(),
246                                                src.copyData(null),
247                                                src.isAlphaPremultiplied(),
248                                                null);
249         RasterOp selectionOp = new SelectionRasterOp(selectionColor);
250         selectionOp.filter(src.getRaster(), dest.getRaster());
251         selectionOp = null;
252         return dest;
253     }
254 
255     /**
256      * Make the specified source image selected mouseover.
257      *
258      * @param src source image
259      * @param selectionColor selection color
260      * @return filtered source image
261      */
262     public static Image makeSelectedMouseover(final BufferedImage src, final Color selectionColor)
263     {
264         return makeMouseover((BufferedImage) makeSelected(src, selectionColor));
265     }
266 }