]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 63c1dabe839320f07427e164b27f9de6caf3028b..6c0f26d4e809bc3611898a4e9559e64a94e17707 100644 (file)
@@ -1,77 +1,93 @@
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.utils.ui.gfx;\r
-\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.swt.graphics.ImageData;\r
-import org.eclipse.swt.graphics.PaletteData;\r
-\r
-/**\r
- * Colored rectangle with black borders.\r
- *  \r
- * @author toni.kalajainen\r
- */\r
-public class ColorImageDescriptor extends ImageDescriptor {\r
-    static final PaletteData PALETTEDATA = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);\r
-\r
-       int width;\r
-       int height;\r
-       int c;\r
-       boolean selected;\r
-       \r
-       public ColorImageDescriptor(int red, int green, int blue, int width, int height, boolean selected)\r
-       {\r
-        c = (red<<16) | (green<<8) | (blue);\r
-               this.width = width;\r
-               this.height = height;\r
-               this.selected = selected;\r
-       }\r
-\r
-       @Override\r
-       public ImageData getImageData() {\r
-        ImageData id = new ImageData(width, height, 24, PALETTEDATA);\r
-        int cx = width / 2;\r
-        int cy = height / 2;\r
-        int dst = height * width / 23;\r
-        for (int x=0; x<width; x++) {\r
-            for (int y=0; y<height; y++) {\r
-                int color = c;\r
-                boolean border = x==0||x==width-1||y==0||y==height-1;\r
-                if ( border ) color = 0;\r
-                if (selected) {\r
-                       int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);\r
-                       if ( dist < dst ) color = 0xcccccc; \r
-                }\r
-                id.setPixel(x, y, color);\r
-            }\r
-        }\r
-        \r
-               return id;\r
-       }\r
-       \r
-       @Override\r
-       public int hashCode() {\r
-               return c + 7*width + 13*height + (selected?234234:4235);\r
-       }\r
-       \r
-       @Override\r
-       public boolean equals(Object obj) {\r
-               if ( obj instanceof ColorImageDescriptor == false ) return false;\r
-               ColorImageDescriptor other = (ColorImageDescriptor) obj;\r
-               if ( other.width!=width ) return false;\r
-               if ( other.height!=height ) return false;\r
-               if ( other.c!=c ) return false;\r
-               if ( other.selected!=selected ) return false;\r
-               return true;\r
-       }\r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.utils.ui.gfx;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.graphics.PaletteData;
+
+/**
+ * Colored rectangle with black borders.
+ *  
+ * @author toni.kalajainen
+ */
+public class ColorImageDescriptor extends ImageDescriptor {
+    static final PaletteData PALETTEDATA = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);
+
+       int width;
+       int height;
+       int c;
+       boolean selected;
+       
+       public ColorImageDescriptor(int red, int green, int blue, int width, int height, boolean selected)
+       {
+        c = (red<<16) | (green<<8) | (blue);
+               this.width = width;
+               this.height = height;
+               this.selected = selected;
+       }
+
+       @Override
+       public ImageData getImageData(int zoom) {
+               int w = width;
+               int h = height;
+               if (zoom > 100) {
+                       float s = zoom / 100.0f;
+                       w = Math.round(width * s);
+                       h = Math.round(height * s);
+               }
+               return getImageData(w, h);
+       }
+
+       @Override
+       public ImageData getImageData() {
+               return getImageData(100);
+       }
+
+       private ImageData getImageData(int width, int height) {
+               ImageData id = new ImageData(width, height, 24, PALETTEDATA);
+               int cx = width / 2;
+               int cy = height / 2;
+               int dst = height * width / 23;
+               for (int x=0; x<width; x++) {
+                       for (int y=0; y<height; y++) {
+                               int color = c;
+                               boolean border = x==0||x==width-1||y==0||y==height-1;
+                               if ( border ) color = 0;
+                               if (selected) {
+                                       int dist = (x-cx)*(x-cx)+(y-cy)*(y-cy);
+                                       if ( dist < dst ) color = 0xcccccc; 
+                               }
+                               id.setPixel(x, y, color);
+                       }
+               }
+
+               return id;
+       }
+
+       @Override
+       public int hashCode() {
+               return c + 7*width + 13*height + (selected?234234:4235);
+       }
+       
+       @Override
+       public boolean equals(Object obj) {
+               if ( obj instanceof ColorImageDescriptor == false ) return false;
+               ColorImageDescriptor other = (ColorImageDescriptor) obj;
+               if ( other.width!=width ) return false;
+               if ( other.height!=height ) return false;
+               if ( other.c!=c ) return false;
+               if ( other.selected!=selected ) return false;
+               return true;
+       }
+       
+}