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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.colors;
\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
23 * @author Antti Villberg
\r
25 public class Colors {
\r
27 private static double i2d(int value) {
\r
28 return (double) value / 255.0;
\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
36 public static Color irgb(RGB rgb) {
\r
37 return new RGBColor(i2d(rgb.red), i2d(rgb.green), i2d(rgb.blue));
\r
40 public static Color irgb(int r, int g, int b) {
\r
41 return new RGBColor(i2d(r), i2d(g), i2d(b));
\r
44 public static Color rgb(double r, double g, double b) {
\r
45 return new RGBColor(r,g,b);
\r
48 public static Color rgb(double[] rgb) {
\r
49 return new RGBColor(rgb[0], rgb[1], rgb[2]);
\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
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
65 * Returns SWT Color instance. Be aware that this is a native resource and
\r
66 * must be disposed of properly.
\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
77 * Returns SWT Color instance. Be aware that this is a native resource and
\r
78 * must be disposed of properly.
\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
89 * Returns SWT Color instance. Be aware that this is a native resource and
\r
90 * must be disposed of properly.
\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
101 * Returns JFace ColorDescriptor instance.
\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
111 * Returns JFace ColorDescriptor instance.
\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
121 * Returns JFace ColorDescriptor instance.
\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
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
137 public static RGB rgb(Color color) {
\r
138 return new RGB(d2i256(color.getR()), d2i256(color.getG()), d2i256(color.getB()));
\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