View Javadoc

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