]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/AlphaAdjustmentImageDescriptor.java
Fixed a bug related to the extent of variable frames (see test case)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / AlphaAdjustmentImageDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * 22.12.2006
14  */
15 package org.simantics.utils.ui.gfx;
16
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.ImageData;
20
21 /**
22  * AlphaAdjustmentImageDescriptor multiplies alpha by (adjustment value/256)
23  * <p>
24  * Usage:
25  * ImageDescriptor icon;
26  * icon = AlphaAdjustmentImageDescriptor.adjustAlpha(icon, 128);
27  * 
28  * @author Toni Kalajainen
29  */
30 public class AlphaAdjustmentImageDescriptor extends ImageDescriptor {
31     
32     public static ImageDescriptor adjustAlpha(ImageDescriptor desc, int alphaAdjustment)
33     {
34         if (alphaAdjustment==256) return desc;
35         return new AlphaAdjustmentImageDescriptor(desc, alphaAdjustment);
36     }
37     
38     int alphaAdjustment;
39     ImageDescriptor desc;
40     
41     /**
42      * 
43      * @param original
44      * @param alphaAdjustment 0..256
45      */
46     public AlphaAdjustmentImageDescriptor(ImageDescriptor original, int alphaAdjustment)
47     {
48         assert(alphaAdjustment>=0 && alphaAdjustment<=256);
49         assert(original!=null);
50         this.alphaAdjustment = alphaAdjustment;
51         this.desc = original;
52     }
53
54     @SuppressWarnings("deprecation")
55     @Override
56     public ImageData getImageData() {
57         ImageData orig = ImageCache.getInstance().getImage(desc).getImageData();
58         ImageData id = new ImageData(orig.width, orig.height, orig.depth, orig.palette);
59         id.setAlpha(0,0,0);
60         
61         if (orig.getTransparencyType()==SWT.TRANSPARENCY_ALPHA ||
62             orig.getTransparencyType()==SWT.TRANSPARENCY_NONE)
63         {
64             for (int x=0; x<orig.width; x++)
65                 for (int y=0; y<orig.height; y++) {
66                     id.setPixel(x, y, orig.getPixel(x, y));
67                     int alpha = (orig.getAlpha(x, y) * alphaAdjustment) >> 8;
68                     id.setAlpha(x, y, alpha);
69                 }
70         } else
71         if (orig.getTransparencyType()==SWT.TRANSPARENCY_MASK ||
72             orig.getTransparencyType()==SWT.TRANSPARENCY_PIXEL) {
73             ImageData mask = orig.getTransparencyMask();
74             for (int x=0; x<orig.width; x++)
75                 for (int y=0; y<orig.height; y++) {
76                     id.setPixel(x, y, orig.getPixel(x, y));
77                     int alpha = (mask.getPixel(x, y)==1?alphaAdjustment:0);
78                     id.setAlpha(x, y, alpha);
79                 }
80         }
81         
82         return id;
83     }
84     
85     @Override
86     public boolean equals(Object obj) {
87         if (!(obj instanceof AlphaAdjustmentImageDescriptor))
88             return false;
89         AlphaAdjustmentImageDescriptor other = (AlphaAdjustmentImageDescriptor) obj;
90         if (!other.desc.equals(desc))
91             return false;
92         if (other.alphaAdjustment!=alphaAdjustment)
93             return false;
94         return true;
95     }
96     
97     @Override
98     public int hashCode() {
99         return desc.hashCode() ^ 0x58fb2 ^ alphaAdjustment;
100     }
101
102 }