org.freehep.swing
Class ColorConverter

java.lang.Object
  extended by org.freehep.swing.ColorConverter

public class ColorConverter
extends Object

A utility class for converting strings to color's.

Version:
$Id: ColorConverter.java 8981 2006-09-15 02:46:28Z serbo $
Author:
Tony Johnson, M.Donszelmann

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[] colorNames()
          Returns array of all Color names that are added, each name is unique
 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
static JColorChooser getColorChooser()
          Creates new JColorChooser with extra panel that allows to choose Color by name
static String[] getNames()
          Returns array of all Color names that are added, each name is unique
 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

getColorChooser

public static JColorChooser getColorChooser()
Creates new JColorChooser with extra panel that allows to choose Color by name


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

getNames

public static String[] getNames()
Returns array of all Color names that are added, each name is unique

Returns:
Array of names for all colors, each name is unique

colorNames

public String[] colorNames()
Returns array of all Color names that are added, each name is unique

Returns:
Array of names for all colors, each name is unique

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.



Copyright © 2000-2007 FreeHEP. All Rights Reserved.