View Javadoc

    1   /*
    2    * "Peko" Visual Novel System
    3    *
    4    * All Rights Reserved.
    5    * (c) Copyright 2002 by Tsukuba Bunko.
    6    *
    7    * $Id: ColorManager.java,v 1.1 2005/07/11 12:49:18 ppoi Exp $
    8    */
    9   package tsukuba_bunko.peko.resource;
    10
    11  import	java.awt.Color;
    12
    13  import	java.util.Map;
    14
    15  import	java.lang.ref.WeakReference;
    16
    17  import	tsukuba_bunko.peko.Logger;
    18
    19
    20  /***
    21   * 色の管理を提供します。
    22   * @author	$Author: ppoi $
    23   * @version	$Revision: 1.1 $
    24   */
    25  public class ColorManager	{
    26
    27  	/***
    28  	 * 唯一のインスタンス
    29  	 */
    30  	private static ColorManager	_instance = null;
    31
    32  	/***
    33  	 * キャッシュ
    34  	 */
    35  	private Map	_cache = null;
    36
    37
    38  	/***
    39  	 * <code>ColorManager</code> のインスタンスを作成します。
    40  	 */
    41  	private ColorManager()
    42  	{
    43  		super();
    44  	}
    45
    46
    47  	/***
    48  	 * 初期化を行います。
    49  	 */
    50  	private void initialize()
    51  	{
    52  		_cache = new java.util.HashMap( 89 );
    53  		_cache.put( "black", new WeakReference(Color.black) );
    54  		_cache.put( "white", new WeakReference(Color.white) );
    55  		_cache.put( "red", new WeakReference(Color.red) );
    56  		_cache.put( "blue", new WeakReference(Color.blue) );
    57  		_cache.put( "green", new WeakReference(Color.green) );
    58  		_cache.put( "yellow", new WeakReference(Color.yellow) );
    59  		_cache.put( "gray", new WeakReference(Color.gray) );
    60  		_cache.put( "darkGray", new WeakReference(Color.darkGray) );
    61  		_cache.put( "lightGray", new WeakReference(Color.lightGray) );
    62  		_cache.put( "cyan", new WeakReference(Color.cyan) );
    63  		_cache.put( "magenta", new WeakReference(Color.magenta) );
    64  		_cache.put( "orange", new WeakReference(Color.orange) );
    65  		_cache.put( "pink", new WeakReference(Color.pink) );
    66  		_cache.put( null, new WeakReference(Color.black) );
    67  	}
    68
    69  	public Color getColor( String name )
    70  	{
    71  		Color	color = null;
    72
    73  		Object	cache = _cache.get( name );
    74  		if( cache != null )	{
    75  			color = (Color)((WeakReference)cache).get();
    76  		}
    77
    78  		if( color == null )	{
    79  			if( name.startsWith("#") )	{
    80  				try	{
    81  					color = new Color( Integer.parseInt(name.substring(1), 16) );
    82  					WeakReference	ref = new WeakReference( color );
    83  					_cache.put( name, ref );
    84  				}
    85  				catch( Exception e )	{
    86  					Logger.error( "[resource] invalid color value specified. :" + name, e );
    87  					Logger.debug( "[resource] using default color. (black)" );
    88  					color = Color.black;
    89  				}
    90  			}
    91  			else	{
    92  				Logger.error( "[resource] invalid color name specified. :" + name );
    93  				Logger.debug( "[resource] using default color. (black)" );
    94  				color = Color.black;
    95  			}
    96  		}
    97  		return color;
    98  	}
    99
    100
    101 	/***
    102 	 * 唯一の <code>ColorManager</code> インスタンスを取得します。
    103 	 * @return	唯一のインスタンス
    104 	 */
    105 	public static ColorManager getInstance()
    106 	{
    107 		if( _instance == null )	{
    108 			synchronized( ColorManager.class )	{
    109 				if( _instance == null )	{
    110 					_instance = new ColorManager();
    111 					_instance.initialize();
    112 				}
    113 			}
    114 		}
    115 		return _instance;
    116 	}
    117 }