1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.colors;
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;
23 * @author Antti Villberg
27 private static double i2d(int value) {
28 return (double) value / 255.0;
31 private static int d2i256(double value) {
32 // TODO: how close to 256 can we go?
33 return (int)(255.9999*value);
36 public static Color irgb(RGB rgb) {
37 return new RGBColor(i2d(rgb.red), i2d(rgb.green), i2d(rgb.blue));
40 public static Color irgb(int r, int g, int b) {
41 return new RGBColor(i2d(r), i2d(g), i2d(b));
44 public static Color rgb(double r, double g, double b) {
45 return new RGBColor(r,g,b);
48 public static Color rgb(double[] rgb) {
49 return new RGBColor(rgb[0], rgb[1], rgb[2]);
56 public static java.awt.Color awt(Color color) {
57 return new java.awt.Color(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));
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);
65 * Returns SWT Color instance. Be aware that this is a native resource and
66 * must be disposed of properly.
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()));
77 * Returns SWT Color instance. Be aware that this is a native resource and
78 * must be disposed of properly.
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()));
89 * Returns SWT Color instance. Be aware that this is a native resource and
90 * must be disposed of properly.
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);
101 * Returns JFace ColorDescriptor instance.
106 public static ColorDescriptor swt(Color color) {
107 return ColorDescriptor.createFrom( new RGB( d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()) ) );
111 * Returns JFace ColorDescriptor instance.
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()) ) );
121 * Returns JFace ColorDescriptor instance.
126 public static ColorDescriptor swt(org.simantics.datatypes.literal.RGB.Integer color) {
127 return ColorDescriptor.createFrom( new RGB(color.red, color.green, color.blue) );
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) );
137 public static RGB rgb(Color color) {
138 return new RGB(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));
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());