]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
3  * in 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.ui.colors;\r
13 \r
14 import org.eclipse.jface.resource.ColorDescriptor;\r
15 import org.eclipse.swt.graphics.Device;\r
16 import org.eclipse.swt.graphics.RGB;\r
17 import org.simantics.common.color.Color;\r
18 import org.simantics.databoard.Bindings;\r
19 import org.simantics.databoard.binding.error.BindingException;\r
20 import org.simantics.databoard.util.Bean;\r
21 \r
22 /**\r
23  * @author Antti Villberg\r
24  */\r
25 public class Colors {\r
26 \r
27     private static double i2d(int value) {\r
28         return (double) value / 255.0;\r
29     }\r
30 \r
31     private static int d2i256(double value) {\r
32         // TODO: how close to 256 can we go? \r
33         return (int)(255.9999*value);\r
34     }\r
35 \r
36     public static Color irgb(RGB rgb) {\r
37         return new RGBColor(i2d(rgb.red), i2d(rgb.green), i2d(rgb.blue));\r
38     }\r
39 \r
40     public static Color irgb(int r, int g, int b) {\r
41         return new RGBColor(i2d(r), i2d(g), i2d(b));\r
42     }\r
43 \r
44     public static Color rgb(double r, double g, double b) {\r
45         return new RGBColor(r,g,b);\r
46     }\r
47 \r
48     public static Color rgb(double[] rgb) {\r
49         return new RGBColor(rgb[0], rgb[1], rgb[2]);\r
50     }\r
51 \r
52     /**\r
53      * Returns AWT Color\r
54      * @return\r
55      */\r
56     public static java.awt.Color awt(Color color) {\r
57         return new java.awt.Color(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
58     }\r
59 \r
60     public static java.awt.Color awt(org.simantics.datatypes.literal.RGB.Integer color) {\r
61         return new java.awt.Color(color.red, color.green, color.blue);\r
62     }\r
63 \r
64     /**\r
65      * Returns SWT Color instance. Be aware that this is a native resource and\r
66      * must be disposed of properly.\r
67      * \r
68      * @param device\r
69      * @param color\r
70      * @return\r
71      */\r
72     public static org.eclipse.swt.graphics.Color swt(Device device, Color color) {\r
73         return new org.eclipse.swt.graphics.Color(device,d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
74     }\r
75 \r
76     /**\r
77      * Returns SWT Color instance. Be aware that this is a native resource and\r
78      * must be disposed of properly.\r
79      * \r
80      * @param device\r
81      * @param color\r
82      * @return\r
83      */\r
84     public static org.eclipse.swt.graphics.Color swt(Device device, org.simantics.datatypes.adt.Color color) {\r
85         return new org.eclipse.swt.graphics.Color(device,d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
86     }\r
87 \r
88     /**\r
89      * Returns SWT Color instance. Be aware that this is a native resource and\r
90      * must be disposed of properly.\r
91      * \r
92      * @param device\r
93      * @param color\r
94      * @return\r
95      */\r
96     public static org.eclipse.swt.graphics.Color swt(Device device, org.simantics.datatypes.literal.RGB.Integer color) {\r
97         return new org.eclipse.swt.graphics.Color(device,color.red, color.green, color.blue);\r
98     }\r
99 \r
100     /**\r
101      * Returns JFace ColorDescriptor instance.\r
102      * \r
103      * @param color\r
104      * @return\r
105      */\r
106     public static ColorDescriptor swt(Color color) {\r
107         return ColorDescriptor.createFrom( new RGB( d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()) ) );\r
108     }\r
109 \r
110     /**\r
111      * Returns JFace ColorDescriptor instance.\r
112      * \r
113      * @param color\r
114      * @return\r
115      */\r
116     public static ColorDescriptor swt(org.simantics.datatypes.adt.Color color) {\r
117         return ColorDescriptor.createFrom( new RGB( d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()) ) );\r
118     }\r
119 \r
120     /**\r
121      * Returns JFace ColorDescriptor instance.\r
122      * \r
123      * @param color\r
124      * @return\r
125      */\r
126     public static ColorDescriptor swt(org.simantics.datatypes.literal.RGB.Integer color) {\r
127         return ColorDescriptor.createFrom( new RGB(color.red, color.green, color.blue) );\r
128     }\r
129 \r
130     public static ColorDescriptor swt(Bean bean) throws BindingException {\r
131         Integer r = (Integer)bean.getField("r", Bindings.INTEGER);\r
132         Integer g = (Integer)bean.getField("g", Bindings.INTEGER);\r
133         Integer b = (Integer)bean.getField("b", Bindings.INTEGER);\r
134         return ColorDescriptor.createFrom( new RGB(r, g, b) );\r
135     }\r
136 \r
137     public static RGB rgb(Color color) {\r
138         return new RGB(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));\r
139     }\r
140     \r
141     public static org.simantics.datatypes.literal.RGB.Integer integerRGB(java.awt.Color awt) {\r
142         return new org.simantics.datatypes.literal.RGB.Integer(awt.getRed(), awt.getGreen(), awt.getBlue());\r
143     }\r
144 \r
145 }\r