001package com.hfg.graphics; 002 003import java.awt.Color; 004 005 006//------------------------------------------------------------------------------ 007/** 008 Color-related utility functions. 009 @author J. Alex Taylor, hairyfatguy.com 010 */ 011//------------------------------------------------------------------------------ 012// com.hfg XML/HTML Coding Library 013// 014// This library is free software; you can redistribute it and/or 015// modify it under the terms of the GNU Lesser General Public 016// License as published by the Free Software Foundation; either 017// version 2.1 of the License, or (at your option) any later version. 018// 019// This library is distributed in the hope that it will be useful, 020// but WITHOUT ANY WARRANTY; without even the implied warranty of 021// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 022// Lesser General Public License for more details. 023// 024// You should have received a copy of the GNU Lesser General Public 025// License along with this library; if not, write to the Free Software 026// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 027// 028// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 029// jataylor@hairyfatguy.com 030//------------------------------------------------------------------------------ 031 032public class ColorUtil 033{ 034 035 //########################################################################## 036 // PUBLIC METHODS 037 //########################################################################## 038 039 040 //-------------------------------------------------------------------------- 041 /** 042 Converts a Color object in to a hex string. Useful for creating HTML. 043 If the alpha value is not fully opaque, the hex value for alpha is included. 044 <p> 045 Color.decode() can be used to convert a hex string into a Color object. 046 </p> 047 */ 048 public static String colorToHex(Color inColor) 049 { 050 String outString = null; 051 052 if (inColor != null) 053 { 054 String red = Integer.toHexString(inColor.getRed()); 055 String green = Integer.toHexString(inColor.getGreen()); 056 String blue = Integer.toHexString(inColor.getBlue()); 057 058 if (red.length() == 1) red = "0" + red; 059 if (green.length() == 1) green = "0" + green; 060 if (blue.length() == 1) blue = "0" + blue; 061 062 outString = red + green + blue; 063 064 if (inColor.getAlpha() < 255) 065 { 066 String alpha = Integer.toHexString(inColor.getAlpha()); 067 if (alpha.length() == 1) alpha = "0" + alpha; 068 outString += alpha; 069 } 070 } 071 072 return outString; 073 } 074 075 //-------------------------------------------------------------------------- 076 /** 077 Returns a equal proportion blend of the specified Colors. 078 */ 079 public static Color blend(Color inColor1, Color inColor2) 080 { 081 return blend(inColor1, inColor2, 0.5f); 082 } 083 084 //-------------------------------------------------------------------------- 085 /** 086 Returns a Color blended in the specified proportions. 087 */ 088 public static Color blend(Color inColor1, Color inColor2, float inProportion) 089 { 090 if (inProportion < 0 || inProportion > 1.0) 091 { 092 throw new RuntimeException("The proportion must be a value between 0 and 1!"); 093 } 094 095 return new Color(Math.round(inColor1.getRed() * inProportion + inColor2.getRed() * (1 - inProportion)), 096 Math.round(inColor1.getGreen() * inProportion + inColor2.getGreen() * (1 - inProportion)), 097 Math.round(inColor1.getBlue() * inProportion + inColor2.getBlue() * (1 - inProportion))); 098 } 099}