From: Erik V. <ev...@us...> - 2009-12-13 16:40:00
|
Update of /cvsroot/rails/18xx/rails/util In directory sfp-cvsdas-1.v30.ch3.sourceforge.com:/tmp/cvs-serv28822/rails/util Modified Files: Util.java Log Message: Implemented request to show low-price colours in SR and OR panels. Added a generic Model-to-View update mechanism (used by above) All configurable colours can now be specified as RGB decimally or hexadecimally. Index: Util.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/util/Util.java,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** Util.java 25 Sep 2009 19:13:01 -0000 1.15 --- Util.java 13 Dec 2009 16:39:49 -0000 1.16 *************** *** 2,8 **** --- 2,11 ---- package rails.util; + import java.awt.Color; import java.util.ArrayList; import java.util.List; + import org.apache.log4j.Logger; + import rails.game.ConfigurationException; import rails.game.move.Moveable; *************** *** 10,14 **** public final class Util { ! // protected static Logger log = Game.getLogger(); /** --- 13,18 ---- public final class Util { ! ! protected static Logger log; /** *************** *** 80,82 **** --- 84,138 ---- } + + /** + * Parse a colour definition string. + * Currently supported formats: + * "RRGGBB" - each character being a hexadecimal digit + * "r,g,b" - each letter representing an integer 0..255 + * @param s + * @return + */ + public static Color parseColour (String s) throws ConfigurationException{ + Color c = null; + if (s.indexOf(',') == -1) { + // Assume hexadecimal RRGGBB + try { + c = new Color (Integer.parseInt(s, 16)); + } catch (NumberFormatException e) { + getLogger().error ("Invalid hex RGB colour: "+s, e); + throw new ConfigurationException (e); + } + } else { + // Assume decimal r,g,b + try { + String[] parts = s.split(","); + c = new Color (Integer.parseInt(parts[0]), + Integer.parseInt(parts[1]), + Integer.parseInt(parts[2])); + } catch (NumberFormatException e) { + getLogger().error ("Invalid nummeric RGB colour: "+s, e); + throw new ConfigurationException (e); + } + } + //getLogger().debug("+++ String:"+s+" Color:"+c); + return c; + } + + /** + * Is a colour dark? (to check if FG colour needs be reversed) + */ + public static boolean isDark(Color c) { + if (c == null) return false; + return Math.sqrt(0.241*c.getRed()*c.getRed() + + 0.691*c.getBlue()*c.getBlue() + + 0.068*c.getGreen()*c.getGreen()) < 128; + // Copied this formula from + // http://www.nbdtech.com/blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx + } + + public static Logger getLogger () { + if (log == null) log = Logger.getLogger(Util.class.getPackage().getName()); + return log; + + } } |