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