Coverage Report - org.dishevelled.iconbundle.impl.IconBundleUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
IconBundleUtils
92%
26/28
N/A
1.333
IconBundleUtils$1
100%
15/15
100%
6/6
1.333
 
 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  4508270
 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  1
     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  1
     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  1
     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  1
     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  1
     private static final RasterOp DISABLED_PATTERN_OP = new AbstractRasterOp()
 96  1
         {
 97  
 
 98  
             /** {@inheritDoc} */
 99  
             public WritableRaster filter(final Raster src, final WritableRaster dest)
 100  
             {
 101  
                 float intensity;
 102  910
                 float[] pixel = new float[PIXEL_ARRAY_SIZE];
 103  
 
 104  54860
                 for (int x = 0, w = src.getWidth(); x < w; x++)
 105  
                 {
 106  4562220
                     for (int y = 0, h = src.getHeight(); y < h; y++)
 107  
                     {
 108  4508270
                         pixel = src.getPixel(x, y, pixel);
 109  4508270
                         intensity = calculateIntensity(pixel[0], pixel[1], pixel[2]);
 110  
 
 111  4508270
                         if (((x + y) % 2) == 0)
 112  
                         {
 113  2254590
                             pixel[0] = (intensity / 2) + MEDIUM_GREY;
 114  2254590
                             pixel[1] = (intensity / 2) + MEDIUM_GREY;
 115  2254590
                             pixel[2] = (intensity / 2) + MEDIUM_GREY;
 116  
                         }
 117  
                         else
 118  
                         {
 119  2253680
                             pixel[0] *= DISABLED_PATTERN_FACTOR;
 120  2253680
                             pixel[1] *= DISABLED_PATTERN_FACTOR;
 121  2253680
                             pixel[2] *= DISABLED_PATTERN_FACTOR;
 122  
                         }
 123  4508270
                         dest.setPixel(x, y, pixel);
 124  
                     }
 125  
                 }
 126  910
                 return dest;
 127  
             }
 128  
         };
 129  
 
 130  
 
 131  
     /**
 132  
      * Private constructor.
 133  
      */
 134  
     private IconBundleUtils()
 135  0
     {
 136  
         // empty
 137  0
     }
 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  4508270
         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  910
         BufferedImage dest = new BufferedImage(src.getColorModel(),
 179  
                                                src.copyData(null),
 180  
                                                src.isAlphaPremultiplied(),
 181  
                                                null);
 182  910
         ACTIVE_OP.filter(src, dest);
 183  910
         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  910
         BufferedImage dest = new BufferedImage(src.getColorModel(),
 195  
                                                src.copyData(null),
 196  
                                                src.isAlphaPremultiplied(),
 197  
                                                null);
 198  910
         DISABLED_SATURATION_OP.filter(src, dest);
 199  910
         DISABLED_PATTERN_OP.filter(src.getRaster(), dest.getRaster());
 200  910
         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  910
         BufferedImage dest = new BufferedImage(src.getColorModel(),
 212  
                                                src.copyData(null),
 213  
                                                src.isAlphaPremultiplied(),
 214  
                                                null);
 215  910
         DRAGGING_OP.filter(src, dest);
 216  910
         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  1820
         BufferedImage dest = new BufferedImage(src.getColorModel(),
 228  
                                                src.copyData(null),
 229  
                                                src.isAlphaPremultiplied(),
 230  
                                                null);
 231  1820
         MOUSEOVER_OP.filter(src, dest);
 232  1820
         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  1820
         BufferedImage dest = new BufferedImage(src.getColorModel(),
 245  
                                                src.copyData(null),
 246  
                                                src.isAlphaPremultiplied(),
 247  
                                                null);
 248  1820
         RasterOp selectionOp = new SelectionRasterOp(selectionColor);
 249  1820
         selectionOp.filter(src.getRaster(), dest.getRaster());
 250  1820
         selectionOp = null;
 251  1820
         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  910
         return makeMouseover((BufferedImage) makeSelected(src, selectionColor));
 264  
     }
 265  
 }