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