--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * 22.12.2006\r
+ */\r
+package org.simantics.utils.ui.gfx;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.ImageData;\r
+\r
+/**\r
+ * AlphaAdjustmentImageDescriptor multiplies alpha by (adjustment value/256)\r
+ * <p>\r
+ * Usage:\r
+ * ImageDescriptor icon;\r
+ * icon = AlphaAdjustmentImageDescriptor.adjustAlpha(icon, 128);\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class AlphaAdjustmentImageDescriptor extends ImageDescriptor {\r
+ \r
+ public static ImageDescriptor adjustAlpha(ImageDescriptor desc, int alphaAdjustment)\r
+ {\r
+ if (alphaAdjustment==256) return desc;\r
+ return new AlphaAdjustmentImageDescriptor(desc, alphaAdjustment);\r
+ }\r
+ \r
+ int alphaAdjustment;\r
+ ImageDescriptor desc;\r
+ \r
+ /**\r
+ * \r
+ * @param original\r
+ * @param alphaAdjustment 0..256\r
+ */\r
+ public AlphaAdjustmentImageDescriptor(ImageDescriptor original, int alphaAdjustment)\r
+ {\r
+ assert(alphaAdjustment>=0 && alphaAdjustment<=256);\r
+ assert(original!=null);\r
+ this.alphaAdjustment = alphaAdjustment;\r
+ this.desc = original;\r
+ }\r
+\r
+ @SuppressWarnings("deprecation")\r
+ @Override\r
+ public ImageData getImageData() {\r
+ ImageData orig = ImageCache.getInstance().getImage(desc).getImageData();\r
+ ImageData id = new ImageData(orig.width, orig.height, orig.depth, orig.palette);\r
+ id.setAlpha(0,0,0);\r
+ \r
+ if (orig.getTransparencyType()==SWT.TRANSPARENCY_ALPHA ||\r
+ orig.getTransparencyType()==SWT.TRANSPARENCY_NONE)\r
+ {\r
+ for (int x=0; x<orig.width; x++)\r
+ for (int y=0; y<orig.height; y++) {\r
+ id.setPixel(x, y, orig.getPixel(x, y));\r
+ int alpha = (orig.getAlpha(x, y) * alphaAdjustment) >> 8;\r
+ id.setAlpha(x, y, alpha);\r
+ }\r
+ } else\r
+ if (orig.getTransparencyType()==SWT.TRANSPARENCY_MASK ||\r
+ orig.getTransparencyType()==SWT.TRANSPARENCY_PIXEL) {\r
+ ImageData mask = orig.getTransparencyMask();\r
+ for (int x=0; x<orig.width; x++)\r
+ for (int y=0; y<orig.height; y++) {\r
+ id.setPixel(x, y, orig.getPixel(x, y));\r
+ int alpha = (mask.getPixel(x, y)==1?alphaAdjustment:0);\r
+ id.setAlpha(x, y, alpha);\r
+ }\r
+ }\r
+ \r
+ return id;\r
+ }\r
+ \r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (!(obj instanceof AlphaAdjustmentImageDescriptor))\r
+ return false;\r
+ AlphaAdjustmentImageDescriptor other = (AlphaAdjustmentImageDescriptor) obj;\r
+ if (!other.desc.equals(desc))\r
+ return false;\r
+ if (other.alphaAdjustment!=alphaAdjustment)\r
+ return false;\r
+ return true;\r
+ }\r
+ \r
+ @Override\r
+ public int hashCode() {\r
+ return desc.hashCode() ^ 0x58fb2 ^ alphaAdjustment;\r
+ }\r
+\r
+}\r