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