1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
15 package org.simantics.utils.ui.gfx;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.ImageData;
22 * AlphaAdjustmentImageDescriptor multiplies alpha by (adjustment value/256)
25 * ImageDescriptor icon;
26 * icon = AlphaAdjustmentImageDescriptor.adjustAlpha(icon, 128);
28 * @author Toni Kalajainen
30 public class AlphaAdjustmentImageDescriptor extends ImageDescriptor {
32 public static ImageDescriptor adjustAlpha(ImageDescriptor desc, int alphaAdjustment)
34 if (alphaAdjustment==256) return desc;
35 return new AlphaAdjustmentImageDescriptor(desc, alphaAdjustment);
44 * @param alphaAdjustment 0..256
46 public AlphaAdjustmentImageDescriptor(ImageDescriptor original, int alphaAdjustment)
48 assert(alphaAdjustment>=0 && alphaAdjustment<=256);
49 assert(original!=null);
50 this.alphaAdjustment = alphaAdjustment;
54 @SuppressWarnings("deprecation")
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);
61 if (orig.getTransparencyType()==SWT.TRANSPARENCY_ALPHA ||
62 orig.getTransparencyType()==SWT.TRANSPARENCY_NONE)
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);
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);
86 public boolean equals(Object obj) {
87 if (!(obj instanceof AlphaAdjustmentImageDescriptor))
89 AlphaAdjustmentImageDescriptor other = (AlphaAdjustmentImageDescriptor) obj;
90 if (!other.desc.equals(desc))
92 if (other.alphaAdjustment!=alphaAdjustment)
98 public int hashCode() {
99 return desc.hashCode() ^ 0x58fb2 ^ alphaAdjustment;