]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/ColorUtils.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / ColorUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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.utils.ui.gfx;\r
13 \r
14 import org.eclipse.swt.graphics.Color;\r
15 \r
16 /**\r
17  * Utilities for dealing with SWT Color.\r
18  * \r
19  * @author Tuukka Lehtonen\r
20  */\r
21 public class ColorUtils {\r
22 \r
23         /*\r
24      * Blend two colors (SWT) by linearly interpolating between the specified ratio\r
25      * (0-1). 0 means <code>src</code> color only and 1 means <code>dst</code>\r
26      * color only.\r
27      * \r
28      * @param src\r
29      * @param dst\r
30      * @param ratio a value between [0,1]\r
31      * @return a new instance of Color, containing the interpolated result.\r
32      *         Remember to dispose of this color when its no longer needed!\r
33      */\r
34     public static Color blend(Color src, Color dst, double ratio) {\r
35         if (ratio < 0 || ratio > 1)\r
36             throw new IllegalArgumentException("expected t in [0,1], got t " + ratio);\r
37         \r
38         double r1 = src.getRed();\r
39         double g1 = src.getGreen();\r
40         double b1 = src.getBlue();\r
41         double r2 = dst.getRed();\r
42         double g2 = dst.getGreen();\r
43         double b2 = dst.getBlue();\r
44         \r
45         double r = r1 + (r2 - r1) * ratio;\r
46         double g = g1 + (g2 - g1) * ratio;\r
47         double b = b1 + (b2 - b1) * ratio;\r
48         \r
49         return new Color(src.getDevice(), (int) r, (int) g, (int) b);\r
50     }\r
51 \r
52     /**\r
53      * Blend two colors (AWT) by linearly interpolating between the specified ratio\r
54      * (0-1). 0 means <code>src</code> color only and 1 means <code>dst</code>\r
55      * color only.\r
56      * \r
57      * @param src\r
58      * @param dst\r
59      * @param ratio a value between [0,1]\r
60      * @return a new instance of Color, containing the interpolated result.\r
61      *         Remember to dispose of this color when its no longer needed!\r
62      */\r
63     public static java.awt.Color blend(java.awt.Color src, java.awt.Color dst, double ratio) {\r
64         if (ratio < 0 || ratio > 1)\r
65             throw new IllegalArgumentException("expected t in [0,1], got t " + ratio);\r
66 \r
67         double r1 = src.getRed();\r
68         double g1 = src.getGreen();\r
69         double b1 = src.getBlue();\r
70         double a1 = src.getAlpha();\r
71         double r2 = dst.getRed();\r
72         double g2 = dst.getGreen();\r
73         double b2 = dst.getBlue();\r
74         double a2 = dst.getAlpha();\r
75 \r
76         double r = r1 + (r2 - r1) * ratio;\r
77         double g = g1 + (g2 - g1) * ratio;\r
78         double b = b1 + (b2 - b1) * ratio;\r
79         double a = a1 + (a2 - a1) * ratio;\r
80 \r
81         return new java.awt.Color((int) r, (int) g, (int) b, (int) a);\r
82     }\r
83 \r
84 }\r