]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ColorImageDescriptor.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / ColorImageDescriptor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui.gfx;\r
13 \r
14 import org.eclipse.jface.resource.ImageDescriptor;\r
15 import org.eclipse.swt.graphics.ImageData;\r
16 import org.eclipse.swt.graphics.PaletteData;\r
17 \r
18 /**\r
19  * Colored rectangle with black borders.\r
20  *  \r
21  * @author toni.kalajainen\r
22  */\r
23 public class ColorImageDescriptor extends ImageDescriptor {\r
24     static final PaletteData PALETTEDATA = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);\r
25 \r
26         int width;\r
27         int height;\r
28         int c;\r
29         boolean selected;\r
30         \r
31         public ColorImageDescriptor(int red, int green, int blue, int width, int height, boolean selected)\r
32         {\r
33         c = (red<<16) | (green<<8) | (blue);\r
34                 this.width = width;\r
35                 this.height = height;\r
36                 this.selected = selected;\r
37         }\r
38 \r
39         @Override\r
40         public ImageData getImageData() {\r
41         ImageData id = new ImageData(width, height, 24, PALETTEDATA);\r
42         int cx = width / 2;\r
43         int cy = height / 2;\r
44         int dst = height * width / 23;\r
45         for (int x=0; x<width; x++) {\r
46             for (int y=0; y<height; y++) {\r
47                 int color = c;\r
48                 boolean border = x==0||x==width-1||y==0||y==height-1;\r
49                 if ( border ) color = 0;\r
50                 if (selected) {\r
51                         int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);\r
52                         if ( dist < dst ) color = 0xcccccc; \r
53                 }\r
54                 id.setPixel(x, y, color);\r
55             }\r
56         }\r
57         \r
58                 return id;\r
59         }\r
60         \r
61         @Override\r
62         public int hashCode() {\r
63                 return c + 7*width + 13*height + (selected?234234:4235);\r
64         }\r
65         \r
66         @Override\r
67         public boolean equals(Object obj) {\r
68                 if ( obj instanceof ColorImageDescriptor == false ) return false;\r
69                 ColorImageDescriptor other = (ColorImageDescriptor) obj;\r
70                 if ( other.width!=width ) return false;\r
71                 if ( other.height!=height ) return false;\r
72                 if ( other.c!=c ) return false;\r
73                 if ( other.selected!=selected ) return false;\r
74                 return true;\r
75         }\r
76         \r
77 }\r