]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/ColorGradient.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / ColorGradient.java
index 5fc3d747c26790cbd3795676312eeb39c4126322..619cb17e89e330a5cde9b483650d4e5036a0f7fb 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in 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.color;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.graphics.GC;\r
-import org.eclipse.swt.graphics.Image;\r
-import org.eclipse.swt.widgets.Display;\r
-\r
-/**\r
- * \r
- * <p>\r
- * Color gradient between multiple colors\r
- * </p>\r
- * <p>\r
- * Supports RGB and HSV linear interpolation between colors\r
- * </p>\r
- * \r
- * @author Marko Luukkainen\r
- *\r
- */\r
-public class ColorGradient {\r
-    public static final int RGB = 0;\r
-\r
-    public static final int HSV = 1;\r
-\r
-    protected ArrayList<ColorValue> values;\r
-\r
-    protected int type;\r
-    \r
-    public ColorGradient() {\r
-        this.values = new ArrayList<ColorValue>();\r
-        this.type = RGB;\r
-    }\r
-\r
-    public ColorGradient(ColorGradient copyFrom) {\r
-        this.values = new ArrayList<ColorValue>(copyFrom.values);\r
-        this.type = copyFrom.type;\r
-    }\r
-\r
-    public ColorGradient(Collection<ColorValue> values) {\r
-        this.values = new ArrayList<ColorValue>(values);\r
-        Collections.sort(this.values, new ColorValueComparator());\r
-        this.type = RGB;\r
-    }\r
-\r
-    public ColorGradient(ColorValue array[]) {\r
-        this.values = new ArrayList<ColorValue>(array.length);\r
-        for (ColorValue c : array) {\r
-            values.add(c);\r
-        }\r
-        Collections.sort(this.values, new ColorValueComparator());\r
-        this.type = RGB;\r
-    }\r
-\r
-    public ColorGradient(Collection<ColorValue> values, int type) {\r
-        this.values = new ArrayList<ColorValue>(values);\r
-        Collections.sort(this.values, new ColorValueComparator());\r
-        this.type = type;\r
-    }\r
-\r
-    public ColorGradient(ColorValue array[], int type) {\r
-        this.values = new ArrayList<ColorValue>();\r
-        for (ColorValue c : array) {\r
-            values.add(c);\r
-        }\r
-        Collections.sort(this.values, new ColorValueComparator());\r
-        this.type = type;\r
-    }\r
-\r
-    /**\r
-     * Interpolates color in RGB space\r
-     * \r
-     * @param value\r
-     * @return\r
-     */\r
-    private byte[] getRGBColor(double value) {\r
-        int index = 1;\r
-        while (values.get(index).getValue() <= value && index < values.size()-1)\r
-            index++;\r
-\r
-        value -= values.get(index - 1).getValue();\r
-        value /= (values.get(index).getValue() - values.get(index - 1).getValue());\r
-        double valuei = 1.0 - value;\r
-        byte color[] = new byte[] {\r
-                (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getR() + valuei * values.get(index - 1).getColor().getR())),\r
-                (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getG() + valuei * values.get(index - 1).getColor().getG())),\r
-                (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getB() + valuei * values.get(index - 1).getColor().getB())) };\r
-        return color;\r
-\r
-    }\r
-\r
-    /**\r
-     * Interpolates color in HSV space\r
-     * \r
-     * @param value\r
-     * @return\r
-     */\r
-    private byte[] getHSVColor(double value) {\r
-        int index = 1;\r
-        while (values.get(index).getValue() <= value && index < values.size()-1)\r
-            index++;\r
-\r
-        value -= values.get(index - 1).getValue();\r
-        value /= (values.get(index).getValue() - values.get(index - 1).getValue());\r
-        double valuei = 1.0 - value;\r
-        double h;\r
-        if (Float.isNaN(values.get(index).getColor().getH())) {\r
-            h = values.get(index-1).getColor().getH();\r
-        } else if (Float.isNaN(values.get(index-1).getColor().getH())) {\r
-            h = values.get(index).getColor().getH();\r
-        } else {\r
-            // selecting shortest direction between hues\r
-            float angle = values.get(index).getColor().getH() - values.get(index - 1).getColor().getH();\r
-            if (angle > 180.f)\r
-                angle -= 360.f;\r
-            else if (angle < -180.f)\r
-                angle += 360.f;\r
-            h = values.get(index - 1).getColor().getH() + value * angle;\r
-            if (h > 360.f)\r
-                h -= 360.f;\r
-            else if (h < 0.f)\r
-                h+= 360.f;\r
-        }\r
-        org.simantics.utils.ui.color.Color interpolated = new org.simantics.utils.ui.color.Color(h, value * values.get(index).getColor().getS() + valuei * values.get(index - 1).getColor().getS(),\r
-                value * values.get(index).getColor().getV() + valuei * values.get(index - 1).getColor().getV());\r
-        byte color[] = new byte[] { (byte) interpolated.getR(), (byte) interpolated.getG(), (byte) interpolated.getB() };\r
-\r
-        return color;\r
-\r
-    }\r
-\r
-    /**\r
-     * <p>\r
-     * Returns gradient in array of bytes. Array is RGB order and int contains 3 * requested size of bytes.\r
-     * </p>\r
-     * <p>\r
-     * If gradient contains only one color array is filled with that color\r
-     * </p>\r
-     * <p>\r
-     * if gradient has no colors array is filled with white\r
-     * </p>\r
-     * @param size number of pixels\r
-     * @return gradient in array of bytes\r
-     */\r
-    public byte[] getGradientArray(int size) {\r
-        byte array[] = new byte[size * 3];\r
-        if (values.size() > 1) {\r
-            for (int i = 0; i < size; i++) {\r
-                int index = i * 3;\r
-                double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) i / (double) size;\r
-                byte color[];\r
-                if (type == RGB)\r
-                    color = getRGBColor(value);\r
-                else\r
-                    color = getHSVColor(value);\r
-                array[index] = color[0];\r
-                array[index + 1] = color[1];\r
-                array[index + 2] = color[2];\r
-            }\r
-        } else if (values.size() == 1) {\r
-            byte color[] = new byte[3];\r
-            color[0] = (byte)values.get(0).getColor().getR();\r
-            color[1] = (byte)values.get(0).getColor().getG();\r
-            color[2] = (byte)values.get(0).getColor().getB();\r
-            for (int i = 0; i < size; i++) {\r
-                int index = i * 3;\r
-                array[index] = color[0];\r
-                array[index + 1] = color[1];\r
-                array[index + 2] = color[2];\r
-            }\r
-        } else {\r
-            for (int i = 0; i < size; i++) {\r
-                int index = i * 3;\r
-                array[index] = (byte)255;\r
-                array[index + 1] = (byte)255;\r
-                array[index + 2] = (byte)255;\r
-            }\r
-        }\r
-        return array;\r
-    }\r
-\r
-    /**\r
-     * <p>\r
-     * Returns gradient in image.\r
-     * </p>\r
-     * <p>\r
-     * If gradient contains only one color image is filled with that color\r
-     * </p>\r
-     * <p>\r
-     * if gradient has no colors image is filled with white\r
-     * </p>\r
-     * <p>\r
-     * Style must be set to  <code>SWT.HORIZONTAL</code> or <code>SWT.VERTICAL</code>\r
-     * </p>\r
-     * @param size number of pixels\r
-     * @return gradient in array of bytes\r
-     */\r
-    \r
-    public Image getGradientImage(int width, int height, int style) {\r
-        Image image = new Image(Display.getCurrent(), width, height);\r
-        GC gc = new GC(image);\r
-        gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));\r
-        gc.fillRectangle(0, 0, width, height);\r
-        if (values.size() > 1) {\r
-            if (SWT.HORIZONTAL == (style | SWT.HORIZONTAL)) {\r
-                for (int x = 0; x < width; x++) {\r
-                    double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) x\r
-                            / (double) (width - 1);\r
-                    byte byteColor[];\r
-                    if (type == RGB)\r
-                        byteColor = getRGBColor(value);\r
-                    else\r
-                        byteColor = getHSVColor(value);\r
-                    Color color = new Color(Display.getCurrent(), byteColor[0] & 0xff, byteColor[1] & 0xff, byteColor[2] & 0xff);\r
-                    gc.setForeground(color);\r
-                    gc.drawLine(x, 0, x, height);\r
-                    color.dispose();\r
-                }\r
-            } else if (SWT.VERTICAL == (style | SWT.VERTICAL)){\r
-                for (int y = 0; y < height; y++) {\r
-                    double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) y\r
-                            / (double) (height - 1);\r
-                    byte byteColor[];\r
-                    if (type == RGB)\r
-                        byteColor = getRGBColor(value);\r
-                    else\r
-                        byteColor = getHSVColor(value);\r
-                    Color color = new Color(Display.getCurrent(), byteColor[0] & 0xff, byteColor[1] & 0xff, byteColor[2] & 0xff);\r
-                    gc.setForeground(color);\r
-                    gc.drawLine(0, y, width, y);\r
-                    color.dispose();\r
-                }\r
-            } else {\r
-                gc.dispose();\r
-                image.dispose();\r
-                SWT.error(SWT.ERROR_INVALID_ARGUMENT);\r
-            }\r
-        } else if (values.size() == 1) {\r
-            Color color = new Color(Display.getCurrent(), values.get(0).getColor().getR(), values.get(0).getColor().getG(), values.get(0).getColor().getB());      \r
-            gc.setBackground(color);\r
-            gc.fillRectangle(0, 0, width, height);\r
-            color.dispose();\r
-        } else {\r
-            gc.fillRectangle(0, 0, width, height);\r
-        }\r
-        gc.dispose();\r
-        return image;\r
-    }\r
-    \r
-    public int getType() {\r
-        return type;\r
-    }\r
-    \r
-    public List<ColorValue> getColorValues() {\r
-        return values;\r
-    }\r
-    \r
-    public ColorValue[] getColorValueArray() {\r
-        return values.toArray(new ColorValue[values.size()]);\r
-    }\r
-    \r
-    @Deprecated\r
-    public void addCastorColorValue(ColorValue value) {\r
-        values.add(value);\r
-        Collections.sort(this.values, new ColorValueComparator());\r
-    }\r
-    \r
-    @Deprecated\r
-    public void setCastorType(int type) {\r
-        this.type = type;\r
-    }\r
-    \r
-    @Override\r
-    public int hashCode() {\r
-        int hash = 0x58fb3;\r
-        for (ColorValue cv : values)\r
-            hash ^= cv.hashCode();\r
-        return hash;\r
-    }\r
-    \r
-    @Override\r
-    public boolean equals(Object obj) {\r
-       if (obj == null)\r
-               return false;\r
-       if (obj.getClass() != getClass())\r
-               return false;\r
-        ColorGradient cg = (ColorGradient) obj;\r
-        if (cg.type != type) return false;\r
-        if (values.size()!=cg.values.size()) return false;\r
-        if (!values.containsAll(cg.values)) return false;\r
-        return true;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 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.color;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Display;
+
+/**
+ * 
+ * <p>
+ * Color gradient between multiple colors
+ * </p>
+ * <p>
+ * Supports RGB and HSV linear interpolation between colors
+ * </p>
+ * 
+ * @author Marko Luukkainen
+ *
+ */
+public class ColorGradient {
+    public static final int RGB = 0;
+
+    public static final int HSV = 1;
+
+    protected ArrayList<ColorValue> values;
+
+    protected int type;
+    
+    public ColorGradient() {
+        this.values = new ArrayList<ColorValue>();
+        this.type = RGB;
+    }
+
+    public ColorGradient(ColorGradient copyFrom) {
+        this.values = new ArrayList<ColorValue>(copyFrom.values);
+        this.type = copyFrom.type;
+    }
+
+    public ColorGradient(Collection<ColorValue> values) {
+        this.values = new ArrayList<ColorValue>(values);
+        Collections.sort(this.values, new ColorValueComparator());
+        this.type = RGB;
+    }
+
+    public ColorGradient(ColorValue array[]) {
+        this.values = new ArrayList<ColorValue>(array.length);
+        for (ColorValue c : array) {
+            values.add(c);
+        }
+        Collections.sort(this.values, new ColorValueComparator());
+        this.type = RGB;
+    }
+
+    public ColorGradient(Collection<ColorValue> values, int type) {
+        this.values = new ArrayList<ColorValue>(values);
+        Collections.sort(this.values, new ColorValueComparator());
+        this.type = type;
+    }
+
+    public ColorGradient(ColorValue array[], int type) {
+        this.values = new ArrayList<ColorValue>();
+        for (ColorValue c : array) {
+            values.add(c);
+        }
+        Collections.sort(this.values, new ColorValueComparator());
+        this.type = type;
+    }
+
+    /**
+     * Interpolates color in RGB space
+     * 
+     * @param value
+     * @return
+     */
+    private byte[] getRGBColor(double value) {
+        int index = 1;
+        while (values.get(index).getValue() <= value && index < values.size()-1)
+            index++;
+
+        value -= values.get(index - 1).getValue();
+        value /= (values.get(index).getValue() - values.get(index - 1).getValue());
+        double valuei = 1.0 - value;
+        byte color[] = new byte[] {
+                (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getR() + valuei * values.get(index - 1).getColor().getR())),
+                (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getG() + valuei * values.get(index - 1).getColor().getG())),
+                (byte) Math.min(255.0, Math.floor(value * values.get(index).getColor().getB() + valuei * values.get(index - 1).getColor().getB())) };
+        return color;
+
+    }
+
+    /**
+     * Interpolates color in HSV space
+     * 
+     * @param value
+     * @return
+     */
+    private byte[] getHSVColor(double value) {
+        int index = 1;
+        while (values.get(index).getValue() <= value && index < values.size()-1)
+            index++;
+
+        value -= values.get(index - 1).getValue();
+        value /= (values.get(index).getValue() - values.get(index - 1).getValue());
+        double valuei = 1.0 - value;
+        double h;
+        if (Float.isNaN(values.get(index).getColor().getH())) {
+            h = values.get(index-1).getColor().getH();
+        } else if (Float.isNaN(values.get(index-1).getColor().getH())) {
+            h = values.get(index).getColor().getH();
+        } else {
+            // selecting shortest direction between hues
+            float angle = values.get(index).getColor().getH() - values.get(index - 1).getColor().getH();
+            if (angle > 180.f)
+                angle -= 360.f;
+            else if (angle < -180.f)
+                angle += 360.f;
+            h = values.get(index - 1).getColor().getH() + value * angle;
+            if (h > 360.f)
+                h -= 360.f;
+            else if (h < 0.f)
+                h+= 360.f;
+        }
+        org.simantics.utils.ui.color.Color interpolated = new org.simantics.utils.ui.color.Color(h, value * values.get(index).getColor().getS() + valuei * values.get(index - 1).getColor().getS(),
+                value * values.get(index).getColor().getV() + valuei * values.get(index - 1).getColor().getV());
+        byte color[] = new byte[] { (byte) interpolated.getR(), (byte) interpolated.getG(), (byte) interpolated.getB() };
+
+        return color;
+
+    }
+
+    /**
+     * <p>
+     * Returns gradient in array of bytes. Array is RGB order and int contains 3 * requested size of bytes.
+     * </p>
+     * <p>
+     * If gradient contains only one color array is filled with that color
+     * </p>
+     * <p>
+     * if gradient has no colors array is filled with white
+     * </p>
+     * @param size number of pixels
+     * @return gradient in array of bytes
+     */
+    public byte[] getGradientArray(int size) {
+        byte array[] = new byte[size * 3];
+        if (values.size() > 1) {
+            for (int i = 0; i < size; i++) {
+                int index = i * 3;
+                double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) i / (double) size;
+                byte color[];
+                if (type == RGB)
+                    color = getRGBColor(value);
+                else
+                    color = getHSVColor(value);
+                array[index] = color[0];
+                array[index + 1] = color[1];
+                array[index + 2] = color[2];
+            }
+        } else if (values.size() == 1) {
+            byte color[] = new byte[3];
+            color[0] = (byte)values.get(0).getColor().getR();
+            color[1] = (byte)values.get(0).getColor().getG();
+            color[2] = (byte)values.get(0).getColor().getB();
+            for (int i = 0; i < size; i++) {
+                int index = i * 3;
+                array[index] = color[0];
+                array[index + 1] = color[1];
+                array[index + 2] = color[2];
+            }
+        } else {
+            for (int i = 0; i < size; i++) {
+                int index = i * 3;
+                array[index] = (byte)255;
+                array[index + 1] = (byte)255;
+                array[index + 2] = (byte)255;
+            }
+        }
+        return array;
+    }
+
+    /**
+     * <p>
+     * Returns gradient in image.
+     * </p>
+     * <p>
+     * If gradient contains only one color image is filled with that color
+     * </p>
+     * <p>
+     * if gradient has no colors image is filled with white
+     * </p>
+     * <p>
+     * Style must be set to  <code>SWT.HORIZONTAL</code> or <code>SWT.VERTICAL</code>
+     * </p>
+     * @param size number of pixels
+     * @return gradient in array of bytes
+     */
+    
+    public Image getGradientImage(int width, int height, int style) {
+        Image image = new Image(Display.getCurrent(), width, height);
+        GC gc = new GC(image);
+        gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
+        gc.fillRectangle(0, 0, width, height);
+        if (values.size() > 1) {
+            if (SWT.HORIZONTAL == (style | SWT.HORIZONTAL)) {
+                for (int x = 0; x < width; x++) {
+                    double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) x
+                            / (double) (width - 1);
+                    byte byteColor[];
+                    if (type == RGB)
+                        byteColor = getRGBColor(value);
+                    else
+                        byteColor = getHSVColor(value);
+                    Color color = new Color(Display.getCurrent(), byteColor[0] & 0xff, byteColor[1] & 0xff, byteColor[2] & 0xff);
+                    gc.setForeground(color);
+                    gc.drawLine(x, 0, x, height);
+                    color.dispose();
+                }
+            } else if (SWT.VERTICAL == (style | SWT.VERTICAL)){
+                for (int y = 0; y < height; y++) {
+                    double value = values.get(0).getValue() + (values.get(values.size() - 1).getValue() - values.get(0).getValue()) * (double) y
+                            / (double) (height - 1);
+                    byte byteColor[];
+                    if (type == RGB)
+                        byteColor = getRGBColor(value);
+                    else
+                        byteColor = getHSVColor(value);
+                    Color color = new Color(Display.getCurrent(), byteColor[0] & 0xff, byteColor[1] & 0xff, byteColor[2] & 0xff);
+                    gc.setForeground(color);
+                    gc.drawLine(0, y, width, y);
+                    color.dispose();
+                }
+            } else {
+                gc.dispose();
+                image.dispose();
+                SWT.error(SWT.ERROR_INVALID_ARGUMENT);
+            }
+        } else if (values.size() == 1) {
+            Color color = new Color(Display.getCurrent(), values.get(0).getColor().getR(), values.get(0).getColor().getG(), values.get(0).getColor().getB());      
+            gc.setBackground(color);
+            gc.fillRectangle(0, 0, width, height);
+            color.dispose();
+        } else {
+            gc.fillRectangle(0, 0, width, height);
+        }
+        gc.dispose();
+        return image;
+    }
+    
+    public int getType() {
+        return type;
+    }
+    
+    public List<ColorValue> getColorValues() {
+        return values;
+    }
+    
+    public ColorValue[] getColorValueArray() {
+        return values.toArray(new ColorValue[values.size()]);
+    }
+    
+    @Deprecated
+    public void addCastorColorValue(ColorValue value) {
+        values.add(value);
+        Collections.sort(this.values, new ColorValueComparator());
+    }
+    
+    @Deprecated
+    public void setCastorType(int type) {
+        this.type = type;
+    }
+    
+    @Override
+    public int hashCode() {
+        int hash = 0x58fb3;
+        for (ColorValue cv : values)
+            hash ^= cv.hashCode();
+        return hash;
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+       if (obj == null)
+               return false;
+       if (obj.getClass() != getClass())
+               return false;
+        ColorGradient cg = (ColorGradient) obj;
+        if (cg.type != type) return false;
+        if (values.size()!=cg.values.size()) return false;
+        if (!values.containsAll(cg.values)) return false;
+        return true;
+    }
+
+}