FreeHEP API
Version current

org.freehep.swing
Class ColorConverter

java.lang.Object
  extended byorg.freehep.swing.ColorConverter

public class ColorConverter
extends Object

A utility class for converting strings to color's.

Version:
$Id: ColorConverter.java,v 1.2 2004/07/24 04:04:31 duns Exp $
Author:
Tony Johnson, M.Donszelmann
Source Code:
ColorConverter.java

Nested Class Summary
static class ColorConverter.ColorConversionException
          An exception thrown if a given string cannot be converted to a Color
 
Constructor Summary
ColorConverter()
          Creates an instance of ColorConverter with the default color conversions
 
Method Summary
 void addEntry(Color c, String name)
          Add an entry to the color converter map
 void clear()
          Clear all the mappings from the color converter
 String colorToString(Color color)
          Convert the given color to a string.
protected  Color createColor(float red, float green, float blue, float alpha)
          Return a color for the given values.
protected  Color createColor(int red, int green, int blue, int alpha)
          Return a color for the given values.
static String get(Color color)
          Convert a color to a string, using the default ColorConverter
static Color get(String name)
          Converts a string to a color, using the default ColorConverter
 Color stringToColor(String name)
          this method returns a Color.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorConverter

public ColorConverter()
Creates an instance of ColorConverter with the default color conversions

Method Detail

get

public static String get(Color color)
Convert a color to a string, using the default ColorConverter

Parameters:
color - The color to convert
Returns:
The resulting String

get

public static Color get(String name)
                 throws ColorConverter.ColorConversionException
Converts a string to a color, using the default ColorConverter

Parameters:
name - The string to convert
Returns:
The resulting color
Throws:
ColorConverter.ColorConversionException - Thrown if the given string cannot be converted to a color

addEntry

public void addEntry(Color c,
                     String name)
Add an entry to the color converter map

Parameters:
c - The color to add
name - The name corresponding to the color

clear

public void clear()
Clear all the mappings from the color converter


colorToString

public String colorToString(Color color)
Convert the given color to a string.

Parameters:
color - The color to be converted
Returns:
Teh resulting string

stringToColor

public Color stringToColor(String name)
                    throws ColorConverter.ColorConversionException
this method returns a Color. Colors are supposedly immutable and are returned from the same table. The supported formats are:
      by name:          "yellow"                      , where alpha is always 1.0
      by int r,g,b,a:   "128, 255, 64, 255"           , where alpha (a) is optional
      by float r,g,b,a: "0.5, 1.0, 0.25, 1.0"         , where alpha (a) is optional
      by single number: "64637" or "0x0FFF08"         , where alpha is always 1.0
 

Parameters:
name - name/number of the color
Returns:
requested Color or defaulting to white in case of a invalid name (message is printed).
Throws:
ColorConverter.ColorConversionException - Thrown if the given string cannot be converted to a color

createColor

protected Color createColor(int red,
                            int green,
                            int blue,
                            int alpha)
Return a color for the given values. Subclasses may override this to tweak colors.


createColor

protected Color createColor(float red,
                            float green,
                            float blue,
                            float alpha)
Return a color for the given values. Subclasses may override this to tweak colors.


FreeHEP API
Version current

Copyright © 2000-2004 FreeHEP, All Rights Reserved.