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