]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ColorImageDescriptor.java
Fixed all line endings of the repository
[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() {
41         ImageData id = new ImageData(width, height, 24, PALETTEDATA);
42         int cx = width / 2;
43         int cy = height / 2;
44         int dst = height * width / 23;
45         for (int x=0; x<width; x++) {
46             for (int y=0; y<height; y++) {
47                 int color = c;
48                 boolean border = x==0||x==width-1||y==0||y==height-1;
49                 if ( border ) color = 0;
50                 if (selected) {
51                         int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);
52                         if ( dist < dst ) color = 0xcccccc; 
53                 }
54                 id.setPixel(x, y, color);
55             }
56         }
57         
58                 return id;
59         }
60         
61         @Override
62         public int hashCode() {
63                 return c + 7*width + 13*height + (selected?234234:4235);
64         }
65         
66         @Override
67         public boolean equals(Object obj) {
68                 if ( obj instanceof ColorImageDescriptor == false ) return false;
69                 ColorImageDescriptor other = (ColorImageDescriptor) obj;
70                 if ( other.width!=width ) return false;
71                 if ( other.height!=height ) return false;
72                 if ( other.c!=c ) return false;
73                 if ( other.selected!=selected ) return false;
74                 return true;
75         }
76         
77 }