]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/ColorUtil.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / utils / ColorUtil.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.scenegraph.utils;\r
13 \r
14 import java.awt.Color;\r
15 \r
16 /**\r
17  * @author Tuukka Lehtonen\r
18  */\r
19 public class ColorUtil {\r
20 \r
21     /**\r
22      * Blend two colors (AWT) by linearly interpolating between the specified\r
23      * ratio (0-1). 0 means <code>src</code> color only and 1 means\r
24      * <code>dst</code> color only.\r
25      * \r
26      * @param src\r
27      * @param dst\r
28      * @param ratio a value between [0,1]\r
29      * @return a new instance of Color, containing the interpolated result.\r
30      *         Remember to dispose of this color when its no longer needed!\r
31      */\r
32     public static Color blend(Color src, Color dst, double ratio) {\r
33         if (ratio < 0 || ratio > 1)\r
34             throw new IllegalArgumentException("expected t in [0,1], got t " + ratio);\r
35 \r
36         double r1 = src.getRed();\r
37         double g1 = src.getGreen();\r
38         double b1 = src.getBlue();\r
39         double a1 = src.getAlpha();\r
40         double r2 = dst.getRed();\r
41         double g2 = dst.getGreen();\r
42         double b2 = dst.getBlue();\r
43         double a2 = dst.getAlpha();\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         double a = a1 + (a2 - a1) * ratio;\r
49 \r
50         return new Color((int) r, (int) g, (int) b, (int) a);\r
51     }\r
52 \r
53     /**\r
54      * @param c\r
55      * @param alpha\r
56      * @return\r
57      */\r
58     public static Color withAlpha(Color c, int alpha) {\r
59         return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);\r
60     }\r
61 \r
62     /**\r
63      * @param c\r
64      * @param alpha\r
65      * @return\r
66      */\r
67     public static Color withAlpha(Color c, float alpha) {\r
68         return withAlpha(c, (int) (255.0f * alpha));\r
69     }\r
70     \r
71     public static Color gamma(Color c, double y) {\r
72         double oneOverGamma = 1.0D / y;\r
73         int r = c.getRed();\r
74         int g = c.getGreen();\r
75         int b = c.getBlue();\r
76         r = (int) ( 255. * ( Math.pow( r / 255.0D, oneOverGamma ) + 0.5D ) );\r
77         g = (int) ( 255. * ( Math.pow( g / 255.0D, oneOverGamma ) + 0.5D ) );\r
78         b = (int) ( 255. * ( Math.pow( b / 255.0D, oneOverGamma ) + 0.5D ) );\r
79             if ( r > 255 ) r = 255;     \r
80             if ( g > 255 ) g = 255;     \r
81             if ( b > 255 ) b = 255;     \r
82         \r
83         return new Color(r, g, b, c.getAlpha());\r
84     }    \r
85 \r
86 }\r