]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ColorImageDescriptor.java
Implement ImageDescriptor.getImageData(int zoom)
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / ColorImageDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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 package org.simantics.utils.ui.gfx;
13
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.swt.graphics.ImageData;
16 import org.eclipse.swt.graphics.PaletteData;
17
18 /**
19  * Colored rectangle with black borders.
20  *  
21  * @author toni.kalajainen
22  */
23 public class ColorImageDescriptor extends ImageDescriptor {
24     static final PaletteData PALETTEDATA = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
25
26         int width;
27         int height;
28         int c;
29         boolean selected;
30         
31         public ColorImageDescriptor(int red, int green, int blue, int width, int height, boolean selected)
32         {
33         c = (red<<16) | (green<<8) | (blue);
34                 this.width = width;
35                 this.height = height;
36                 this.selected = selected;
37         }
38
39         @Override
40         public ImageData getImageData(int zoom) {
41                 int w = width;
42                 int h = height;
43                 if (zoom > 100) {
44                         float s = zoom / 100.0f;
45                         w = Math.round(width * s);
46                         h = Math.round(height * s);
47                 }
48                 return getImageData(w, h);
49         }
50
51         @Override
52         public ImageData getImageData() {
53                 return getImageData(100);
54         }
55
56         private ImageData getImageData(int width, int height) {
57                 ImageData id = new ImageData(width, height, 24, PALETTEDATA);
58                 int cx = width / 2;
59                 int cy = height / 2;
60                 int dst = height * width / 23;
61                 for (int x=0; x<width; x++) {
62                         for (int y=0; y<height; y++) {
63                                 int color = c;
64                                 boolean border = x==0||x==width-1||y==0||y==height-1;
65                                 if ( border ) color = 0;
66                                 if (selected) {
67                                         int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);
68                                         if ( dist < dst ) color = 0xcccccc; 
69                                 }
70                                 id.setPixel(x, y, color);
71                         }
72                 }
73
74                 return id;
75         }
76
77         @Override
78         public int hashCode() {
79                 return c + 7*width + 13*height + (selected?234234:4235);
80         }
81         
82         @Override
83         public boolean equals(Object obj) {
84                 if ( obj instanceof ColorImageDescriptor == false ) return false;
85                 ColorImageDescriptor other = (ColorImageDescriptor) obj;
86                 if ( other.width!=width ) return false;
87                 if ( other.height!=height ) return false;
88                 if ( other.c!=c ) return false;
89                 if ( other.selected!=selected ) return false;
90                 return true;
91         }
92         
93 }