]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/colors/Colors.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / colors / Colors.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/colors/Colors.java b/bundles/org.simantics.ui/src/org/simantics/ui/colors/Colors.java
new file mode 100644 (file)
index 0000000..362a104
--- /dev/null
@@ -0,0 +1,145 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 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.ui.colors;\r
+\r
+import org.eclipse.jface.resource.ColorDescriptor;\r
+import org.eclipse.swt.graphics.Device;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.simantics.common.color.Color;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.util.Bean;\r
+\r
+/**\r
+ * @author Antti Villberg\r
+ */\r
+public class Colors {\r
+\r
+    private static double i2d(int value) {\r
+        return (double) value / 255.0;\r
+    }\r
+\r
+    private static int d2i256(double value) {\r
+        // TODO: how close to 256 can we go? \r
+        return (int)(255.9999*value);\r
+    }\r
+\r
+    public static Color irgb(RGB rgb) {\r
+        return new RGBColor(i2d(rgb.red), i2d(rgb.green), i2d(rgb.blue));\r
+    }\r
+\r
+    public static Color irgb(int r, int g, int b) {\r
+        return new RGBColor(i2d(r), i2d(g), i2d(b));\r
+    }\r
+\r
+    public static Color rgb(double r, double g, double b) {\r
+       return new RGBColor(r,g,b);\r
+    }\r
+\r
+    public static Color rgb(double[] rgb) {\r
+       return new RGBColor(rgb[0], rgb[1], rgb[2]);\r
+    }\r
+\r
+    /**\r
+     * Returns AWT Color\r
+     * @return\r
+     */\r
+    public static java.awt.Color awt(Color color) {\r
+        return new java.awt.Color(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
+    }\r
+\r
+    public static java.awt.Color awt(org.simantics.datatypes.literal.RGB.Integer color) {\r
+        return new java.awt.Color(color.red, color.green, color.blue);\r
+    }\r
+\r
+    /**\r
+     * Returns SWT Color instance. Be aware that this is a native resource and\r
+     * must be disposed of properly.\r
+     * \r
+     * @param device\r
+     * @param color\r
+     * @return\r
+     */\r
+    public static org.eclipse.swt.graphics.Color swt(Device device, Color color) {\r
+        return new org.eclipse.swt.graphics.Color(device,d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
+    }\r
+\r
+    /**\r
+     * Returns SWT Color instance. Be aware that this is a native resource and\r
+     * must be disposed of properly.\r
+     * \r
+     * @param device\r
+     * @param color\r
+     * @return\r
+     */\r
+    public static org.eclipse.swt.graphics.Color swt(Device device, org.simantics.datatypes.adt.Color color) {\r
+        return new org.eclipse.swt.graphics.Color(device,d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
+    }\r
+\r
+    /**\r
+     * Returns SWT Color instance. Be aware that this is a native resource and\r
+     * must be disposed of properly.\r
+     * \r
+     * @param device\r
+     * @param color\r
+     * @return\r
+     */\r
+    public static org.eclipse.swt.graphics.Color swt(Device device, org.simantics.datatypes.literal.RGB.Integer color) {\r
+        return new org.eclipse.swt.graphics.Color(device,color.red, color.green, color.blue);\r
+    }\r
+\r
+    /**\r
+     * Returns JFace ColorDescriptor instance.\r
+     * \r
+     * @param color\r
+     * @return\r
+     */\r
+    public static ColorDescriptor swt(Color color) {\r
+        return ColorDescriptor.createFrom( new RGB( d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()) ) );\r
+    }\r
+\r
+    /**\r
+     * Returns JFace ColorDescriptor instance.\r
+     * \r
+     * @param color\r
+     * @return\r
+     */\r
+    public static ColorDescriptor swt(org.simantics.datatypes.adt.Color color) {\r
+        return ColorDescriptor.createFrom( new RGB( d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()) ) );\r
+    }\r
+\r
+    /**\r
+     * Returns JFace ColorDescriptor instance.\r
+     * \r
+     * @param color\r
+     * @return\r
+     */\r
+    public static ColorDescriptor swt(org.simantics.datatypes.literal.RGB.Integer color) {\r
+        return ColorDescriptor.createFrom( new RGB(color.red, color.green, color.blue) );\r
+    }\r
+\r
+    public static ColorDescriptor swt(Bean bean) throws BindingException {\r
+       Integer r = (Integer)bean.getField("r", Bindings.INTEGER);\r
+       Integer g = (Integer)bean.getField("g", Bindings.INTEGER);\r
+       Integer b = (Integer)bean.getField("b", Bindings.INTEGER);\r
+        return ColorDescriptor.createFrom( new RGB(r, g, b) );\r
+    }\r
+\r
+    public static RGB rgb(Color color) {\r
+        return new RGB(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
+    }\r
+    \r
+    public static org.simantics.datatypes.literal.RGB.Integer integerRGB(java.awt.Color awt) {\r
+       return new org.simantics.datatypes.literal.RGB.Integer(awt.getRed(), awt.getGreen(), awt.getBlue());\r
+    }\r
+\r
+}\r