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;
20 import org.eclipse.swt.graphics.PaletteData;
21 import org.eclipse.swt.graphics.RGB;
24 * HSVAdjustmentImageDescriptor
25 * @author Toni Kalajainen
27 @SuppressWarnings("deprecation")
28 public class HSVAdjustmentImageDescriptor extends ImageDescriptor {
30 public static final PaletteData DEFAULT_PALETTEDATA =
31 new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
42 public static ImageDescriptor adjustHue(ImageDescriptor image, float hue)
45 if (hue==0) return image;
47 return new HSVAdjustmentImageDescriptor(image, hue, 1.0f, 1.0f);
53 * @param saturation 0..* (1.0=normal)
56 public static ImageDescriptor adjust(ImageDescriptor image, float hue, float saturation, float value)
59 if (saturation==1.0f && value==1.0f && hue==0.0f) return image;
60 return new HSVAdjustmentImageDescriptor(image, hue, saturation, value);
66 * @param value 0..* (1.0=normal)
69 public static ImageDescriptor adjustSaturation(ImageDescriptor image, float saturation)
72 if (saturation==1.0f) return image;
73 return new HSVAdjustmentImageDescriptor(image, 0.0f, saturation, 1.0f);
77 * Adjust hue, saturation and value
80 * @param saturation 0..* (1.0=normal)
81 * @param value 0..* (1.0=normal)
84 public static ImageDescriptor adjustValue(ImageDescriptor image, float value)
87 if (value==1.0f) return image;
88 return new HSVAdjustmentImageDescriptor(image, 0.0f, 1.0f, value);
94 * Adjusts hue, saturation and value of image
97 * @param hue hue adjustment 0..360
98 * @param saturation factor 0..* (1.0=no change)
99 * @param value factor 0..* (1.0=no change)
101 public HSVAdjustmentImageDescriptor(ImageDescriptor image, float hue, float saturation, float value)
110 public ImageData getImageData() {
111 PaletteData palette = DEFAULT_PALETTEDATA;
112 ImageData orig = ImageCache.getInstance().getImage(desc).getImageData();
113 ImageData id = new ImageData(orig.width, orig.height, 24, palette);
115 PaletteData origPalette = orig.palette;
117 ImageData mask = null;
118 if (orig.getTransparencyType()==SWT.TRANSPARENCY_MASK ||
119 orig.getTransparencyType()==SWT.TRANSPARENCY_PIXEL)
120 mask = orig.getTransparencyMask();
122 for (int x=0; x<orig.width; x++)
123 for (int y=0; y<orig.height; y++) {
124 RGB rgb = origPalette.getRGB( orig.getPixel(x, y) );
125 float hsv[] = rgb.getHSB();
126 hsv[0] = mod360(hsv[0] + h);
129 rgb = new RGB(hsv[0], hsv[1], hsv[2]);
130 id.setPixel(x, y, palette.getPixel(rgb));
133 alpha = orig.getAlpha(x, y);
135 alpha = (mask.getPixel(x, y)==1?255:0);
136 id.setAlpha(x, y, alpha);
141 public static float mod360(float value)
145 d = ((int)value)/360;
147 d = (((int)value)/360)-1;
148 return value-d*360.0f;
152 public boolean equals(Object obj) {
153 if (obj==this) return true;
154 if (!(obj instanceof HSVAdjustmentImageDescriptor))
156 HSVAdjustmentImageDescriptor other = (HSVAdjustmentImageDescriptor) obj;
157 return other.desc.equals(desc) && other.h==h && other.s==s && other.v==v;
161 public int hashCode() {
162 return desc.hashCode() ^ (new Float(h).hashCode()) ^ (new Float(s).hashCode()) ^ (new Float(v).hashCode());