--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.utils;\r
+\r
+import java.awt.Color;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ColorUtil {\r
+\r
+ /**\r
+ * Blend two colors (AWT) by linearly interpolating between the specified\r
+ * ratio (0-1). 0 means <code>src</code> color only and 1 means\r
+ * <code>dst</code> color only.\r
+ * \r
+ * @param src\r
+ * @param dst\r
+ * @param ratio a value between [0,1]\r
+ * @return a new instance of Color, containing the interpolated result.\r
+ * Remember to dispose of this color when its no longer needed!\r
+ */\r
+ public static Color blend(Color src, Color dst, double ratio) {\r
+ if (ratio < 0 || ratio > 1)\r
+ throw new IllegalArgumentException("expected t in [0,1], got t " + ratio);\r
+\r
+ double r1 = src.getRed();\r
+ double g1 = src.getGreen();\r
+ double b1 = src.getBlue();\r
+ double a1 = src.getAlpha();\r
+ double r2 = dst.getRed();\r
+ double g2 = dst.getGreen();\r
+ double b2 = dst.getBlue();\r
+ double a2 = dst.getAlpha();\r
+\r
+ double r = r1 + (r2 - r1) * ratio;\r
+ double g = g1 + (g2 - g1) * ratio;\r
+ double b = b1 + (b2 - b1) * ratio;\r
+ double a = a1 + (a2 - a1) * ratio;\r
+\r
+ return new Color((int) r, (int) g, (int) b, (int) a);\r
+ }\r
+\r
+ /**\r
+ * @param c\r
+ * @param alpha\r
+ * @return\r
+ */\r
+ public static Color withAlpha(Color c, int alpha) {\r
+ return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);\r
+ }\r
+\r
+ /**\r
+ * @param c\r
+ * @param alpha\r
+ * @return\r
+ */\r
+ public static Color withAlpha(Color c, float alpha) {\r
+ return withAlpha(c, (int) (255.0f * alpha));\r
+ }\r
+ \r
+ public static Color gamma(Color c, double y) {\r
+ double oneOverGamma = 1.0D / y;\r
+ int r = c.getRed();\r
+ int g = c.getGreen();\r
+ int b = c.getBlue();\r
+ r = (int) ( 255. * ( Math.pow( r / 255.0D, oneOverGamma ) + 0.5D ) );\r
+ g = (int) ( 255. * ( Math.pow( g / 255.0D, oneOverGamma ) + 0.5D ) );\r
+ b = (int) ( 255. * ( Math.pow( b / 255.0D, oneOverGamma ) + 0.5D ) );\r
+ if ( r > 255 ) r = 255; \r
+ if ( g > 255 ) g = 255; \r
+ if ( b > 255 ) b = 255; \r
+ \r
+ return new Color(r, g, b, c.getAlpha());\r
+ } \r
+\r
+}\r