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