]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/HSVAdjustmentImageDescriptor.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / HSVAdjustmentImageDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * 27.12.2006
14  */
15 package org.simantics.utils.ui.gfx;
16
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;
22
23 /**
24  * HSVAdjustmentImageDescriptor
25  * @author Toni Kalajainen
26  */
27 @SuppressWarnings("deprecation")
28 public class HSVAdjustmentImageDescriptor extends ImageDescriptor {
29     
30     public static final PaletteData DEFAULT_PALETTEDATA = 
31         new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
32     
33     ImageDescriptor desc;
34     float h,s,v;
35
36     /**
37      * Adjust hue value
38      * @param image image
39      * @param hue 0..360
40      * @return
41      */
42     public static ImageDescriptor adjustHue(ImageDescriptor image, float hue)
43     {
44         assert(image!=null);
45         if (hue==0) return image;
46         hue = mod360(hue);
47         return new HSVAdjustmentImageDescriptor(image, hue, 1.0f, 1.0f);
48     }
49     
50     /**
51      * Adjust saturation
52      * @param image image
53      * @param saturation 0..* (1.0=normal)
54      * @return
55      */
56     public static ImageDescriptor adjust(ImageDescriptor image, float hue, float saturation, float value)
57     {
58         assert(image!=null);
59         if (saturation==1.0f && value==1.0f && hue==0.0f) return image;
60         return new HSVAdjustmentImageDescriptor(image, hue, saturation, value);
61     }
62
63     /**
64      * Adjust value
65      * @param image image
66      * @param value 0..* (1.0=normal)
67      * @return
68      */
69     public static ImageDescriptor adjustSaturation(ImageDescriptor image, float saturation)
70     {
71         assert(image!=null);
72         if (saturation==1.0f) return image;
73         return new HSVAdjustmentImageDescriptor(image, 0.0f, saturation, 1.0f);
74     }
75     
76     /**
77      * Adjust hue, saturation and value
78      * @param image image
79      * @param hue 0..360
80      * @param saturation 0..* (1.0=normal)
81      * @param value 0..* (1.0=normal)
82      * @return
83      */
84     public static ImageDescriptor adjustValue(ImageDescriptor image, float value)
85     {
86         assert(image!=null);
87         if (value==1.0f) return image;
88         return new HSVAdjustmentImageDescriptor(image, 0.0f, 1.0f, value);
89     }
90     
91     
92
93     /**
94      * Adjusts hue, saturation and value of image
95      * 
96      * @param 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)
100      */
101     public HSVAdjustmentImageDescriptor(ImageDescriptor image, float hue, float saturation, float value)
102     {
103         desc = image;
104         this.h = hue;
105         this.s = saturation;
106         this.v = value;
107     }
108
109     @Override
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);
114         id.setAlpha(0,0,0);
115         PaletteData origPalette = orig.palette;        
116         
117         ImageData mask = null;
118         if (orig.getTransparencyType()==SWT.TRANSPARENCY_MASK ||
119             orig.getTransparencyType()==SWT.TRANSPARENCY_PIXEL)        
120             mask = orig.getTransparencyMask();
121         
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);
127                 hsv[1] *= s;
128                 hsv[2] *= v;
129                 rgb = new RGB(hsv[0], hsv[1], hsv[2]);
130                 id.setPixel(x, y, palette.getPixel(rgb));
131                 int alpha;
132                 if (mask==null) 
133                     alpha = orig.getAlpha(x, y);
134                 else
135                     alpha = (mask.getPixel(x, y)==1?255:0);
136                 id.setAlpha(x, y, alpha);
137             }        
138         return id;
139     }
140
141     public static float mod360(float value)
142     {
143         int d;
144         if (value>=0)
145             d = ((int)value)/360;
146         else
147             d = (((int)value)/360)-1;
148         return value-d*360.0f;
149     }
150     
151     @Override
152     public boolean equals(Object obj) {
153         if (obj==this) return true;
154         if (!(obj instanceof HSVAdjustmentImageDescriptor))
155             return false;
156         HSVAdjustmentImageDescriptor other = (HSVAdjustmentImageDescriptor) obj;
157         return other.desc.equals(desc) && other.h==h && other.s==s && other.v==v;        
158     }
159     
160     @Override
161     public int hashCode() {
162         return desc.hashCode() ^ (new Float(h).hashCode()) ^ (new Float(s).hashCode()) ^ (new Float(v).hashCode());
163     }
164     
165 }