com.davisor.graphics.data
Class ColorData

java.lang.Object
  extended byjava.awt.Color
      extended bycom.davisor.graphics.data.ColorData
All Implemented Interfaces:
BetterPaint, java.lang.Comparable, Data, DataValue, com.davisor.core.Dupable, java.awt.Paint, PaintValue, java.io.Serializable, SerializableData, java.awt.Transparency

public class ColorData
extends java.awt.Color
implements PaintValue

ColorData represents a concrete solid color. Thie class extends Color, making it compatible with standard Java graphics tools. This also makes instances of this class immutable, so any operations from the Data framework that would change the value of this data object will fail with appropriate exceptions.

Since:
JDK1.1
See Also:
brighterPaint(), darkerPaint(), next(), ColorType, PaintData, PaintParameters, Serialized Form

Field Summary
static float DEFAULTSCALE
          Default color intensity scale factor (0.7).
protected  java.lang.String M_creationString
          Name used to create this instance
protected  PaintType M_type
          The type of this data.
 
Fields inherited from class java.awt.Color
black, BLACK, blue, BLUE, cyan, CYAN, DARK_GRAY, darkGray, gray, GRAY, green, GREEN, LIGHT_GRAY, lightGray, magenta, MAGENTA, orange, ORANGE, pink, PINK, red, RED, white, WHITE, yellow, YELLOW
 
Fields inherited from interface java.awt.Transparency
BITMASK, OPAQUE, TRANSLUCENT
 
Constructor Summary
ColorData(ColorData data, boolean deep)
          Deep or shallow copy constructor.
ColorData(PaintType type, java.awt.Color color)
          Creates a new color data object from the given color type and value.
ColorData(PaintType type, java.awt.Color color, float alpha, boolean preserve)
          Creates a new color data object from the given color type and color and alpha values.
ColorData(PaintType type, int rgb)
          Creates a new color data object from the given color type and integer RGB Java color value.
ColorData(PaintType type, int argb, boolean hasAlpha)
          Creates a new color data object from the given color type and integer value that optionally may have an alpha component.
ColorData(PaintType type, int red, int green, int blue)
          Creates a new color data object from the given color type and RGB components.
ColorData(PaintType type, int red, int green, int blue, int alpha)
          Creates a new color data object from the given color type and RGB and alpha components.
ColorData(PaintType type, java.lang.String color)
          Creates a new color data object from the given color type and spesification string.
ColorData(PaintType type, java.lang.String format, java.text.ParsePosition status)
          Creates a new color data object from the given color type and spesification string fragment.
 
Method Summary
 DataValue abs()
          Returns absolute data value of current value.
 DataValue add(DataValue value)
          Adds data values.
 java.awt.Paint brighterPaint()
          Creates a brighter version of this paint.
static java.awt.Color brighterPaint(java.awt.Color color)
          Creates a brighter version of the given color.
static java.awt.Color brighterPaint(java.awt.Color color, float scale)
          Creates a brighter version of the given color.
static int brighterPaint(int value)
          Computes a brighter RGB component value.
static int brighterPaint(int value, float scale)
          Computes a brighter RGB component value.
 DataValue ceil(DataValue precision)
          Quantizes the current value up to an even value within given precision.
 int compareTo(java.lang.Object o)
          Compares this color with other color objects.
protected static float cropColorScale(float scale)
          Crops the given scale to the range of [0,1].
 java.awt.Paint darkerPaint()
          Creates a darker version of this paint.
static java.awt.Color darkerPaint(java.awt.Color color)
          Creates a darker version of the given color.
static java.awt.Color darkerPaint(java.awt.Color color, float scale)
          Creates a darker version of the given color.
static int darkerPaint(int value)
          Computes a darker RGB component value.
static int darkerPaint(int value, float scale)
          Computes a darker RGB component value.
 double deinterpolate(DataValue min, DataValue max)
          De-interpolates the current data value to a scalar value, in respect, with the given data value range.
 DataValue div(DataValue value)
          Divides current data value with given value.
 com.davisor.core.Dupable dup()
          Makes a deep copy of this object.
 DataValue floor(DataValue precision)
          Quantizes the current value down to an even value within given precision.
 java.lang.String getCreationString()
          Implements PaintValue interface method
 java.lang.String getFormat()
          Gets the type specification optimal for this particular value.
static int getRGBA(java.awt.Color color, float alpha, boolean preserve)
          Merges a color RGB value with the given normalized alpha value.
 Type getType()
          Gets the color data type.
 DataValue interpolate(double value, DataValue max)
          Interpolates the given scalar value to a data value, in respect, with the range defined by this value and the given maximum value.
 boolean isNull()
          Tests if this data object value is undetermined.
 DataValue max(DataValue value)
          Finds the largest data value.
 DataValue min(DataValue value)
          Finds the smallest data value.
 DataValue mul(DataValue value)
          Multiplies current data value with given value.
 DataValue negate()
          Returns the negation of current value.
 BetterPaint next()
          Gets the next paint in this paint series.
static java.awt.Color setAlpha(java.awt.Color color, float alpha)
          Sets color transparency for the given color.
 void setCreationString(java.lang.String creationString)
          Implements PaintValue interface method
 void setValue(java.lang.String value, java.text.ParsePosition status)
          Attempts to set the color value, but since colors are immutable objects, this will always fail.
 DataValue sub(DataValue value)
          Substracts a data value.
 void toStatement(java.sql.PreparedStatement statement, int parameter)
          Converts color data to a prepared statement input parameter.
 java.lang.String toString()
          Gets the paint spesification string of this color.
 java.lang.String toXML()
          Gets the XML string representation of this value.
 DataValue unit(DataValue targetValue, double maxSteps)
          Gets the largest suitable unit step to represent a value change between this value and the given target value.
 
Methods inherited from class java.awt.Color
brighter, createContext, darker, decode, equals, getAlpha, getBlue, getColor, getColor, getColor, getColorComponents, getColorComponents, getColorSpace, getComponents, getComponents, getGreen, getHSBColor, getRed, getRGB, getRGBColorComponents, getRGBComponents, getTransparency, hashCode, HSBtoRGB, RGBtoHSB
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.Paint
createContext
 
Methods inherited from interface java.awt.Transparency
getTransparency
 

Field Detail

DEFAULTSCALE

public static final float DEFAULTSCALE
Default color intensity scale factor (0.7).

See Also:
Constant Field Values

M_type

protected PaintType M_type
The type of this data.


M_creationString

protected java.lang.String M_creationString
Name used to create this instance

Constructor Detail

ColorData

public ColorData(ColorData data,
                 boolean deep)
Deep or shallow copy constructor. If it is shallow, then the new object shares an underlying type with the original object. If it is deep, then the type is dupped.

Parameters:
data - source data
deep - determine if the copying is deep or shallow
See Also:
getType(), Type.dup()

ColorData

public ColorData(PaintType type,
                 java.awt.Color color)
Creates a new color data object from the given color type and value.

Parameters:
type - color type
color - color value
See Also:
ColorData(PaintType,int)

ColorData

public ColorData(PaintType type,
                 java.awt.Color color,
                 float alpha,
                 boolean preserve)
Creates a new color data object from the given color type and color and alpha values.

Parameters:
type - color type
color - color value
alpha - opacity (zero: transparent, one: opaque)
preserve - should the previous alpha value of the color be merged with the given opacity value or not
See Also:
getRGBA(Color,float,boolean), ColorData(PaintType,int,boolean)

ColorData

public ColorData(PaintType type,
                 java.lang.String color)
          throws InvalidDataException
Creates a new color data object from the given color type and spesification string.

Parameters:
type - color type
color - color value
See Also:
ColorData(PaintType,String,ParsePosition)

ColorData

public ColorData(PaintType type,
                 java.lang.String format,
                 java.text.ParsePosition status)
          throws InvalidDataException
Creates a new color data object from the given color type and spesification string fragment.

Parameters:
type - color type
format - color spesicifaction string
status - parse status
See Also:
ColorData(PaintType,String), ColorType.createColor(String,ParsePosition)

ColorData

public ColorData(PaintType type,
                 int rgb)
Creates a new color data object from the given color type and integer RGB Java color value.

Parameters:
type - color type
rgb - color RGB value
See Also:
ColorData(PaintType,int,boolean)

ColorData

public ColorData(PaintType type,
                 int argb,
                 boolean hasAlpha)
Creates a new color data object from the given color type and integer value that optionally may have an alpha component.

Parameters:
type - color type
argb - color RGB value with optional alpha component
hasAlpha - flag for telling if the color value contains an alpha component
See Also:
ColorData(PaintType,int)

ColorData

public ColorData(PaintType type,
                 int red,
                 int green,
                 int blue)
Creates a new color data object from the given color type and RGB components.

Parameters:
type - color type
red - red color component value
green - green color component value
blue - blue color component value
See Also:
ColorData(PaintType,int,int,int,int)

ColorData

public ColorData(PaintType type,
                 int red,
                 int green,
                 int blue,
                 int alpha)
Creates a new color data object from the given color type and RGB and alpha components.

Parameters:
type - color type
red - red color component value
green - green color component value
blue - blue color component value
alpha - alpha color component value
See Also:
ColorData(PaintType,int,int,int,int)
Method Detail

brighterPaint

public java.awt.Paint brighterPaint()
Creates a brighter version of this paint. Unlike (at least some) standard Java implementations, this method preserves the color alpha component value.

Specified by:
brighterPaint in interface BetterPaint
Returns:
a new ColorData object, containing a brighter version of this color
See Also:
darkerPaint(), brighterPaint(int), ColorData

darkerPaint

public java.awt.Paint darkerPaint()
Creates a darker version of this paint. Unlike (at least some) standard Java implementations, this method preserves the color alpha component value.

Specified by:
darkerPaint in interface BetterPaint
Returns:
a new ColorData object, containing a darker version of this color
See Also:
brighterPaint(), darkerPaint(int), ColorData

next

public BetterPaint next()
Gets the next paint in this paint series.

This default implementation will return this instance. Subclasses may alter the behaviour and return other paint objects.

Specified by:
next in interface BetterPaint
Returns:
a reference to this object
See Also:
PaintsData

compareTo

public int compareTo(java.lang.Object o)
Compares this color with other color objects.

This object will be greater than any object that is not an instance of Color. For Color objects, comparison is done with the help of CompareGraphics.compareTo(Color,Color) method.

Specified by:
compareTo in interface java.lang.Comparable
See Also:
CompareGraphics.compareTo(Color,Color)

getType

public Type getType()
             throws InvalidDataException
Gets the color data type.

Specified by:
getType in interface Data
Returns:
type of this color data instance
Throws:
InvalidDataException - if type retrieval fails

isNull

public boolean isNull()
Tests if this data object value is undetermined.

Specified by:
isNull in interface SerializableData
See Also:
Type.isNull(com.davisor.data.SerializableData)

abs

public DataValue abs()
              throws InvalidDataException
Returns absolute data value of current value.

Specified by:
abs in interface DataValue
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if absolute value can not be computed

add

public DataValue add(DataValue value)
              throws InvalidDataException
Adds data values.

Color values do not support this operation, but they always throw an InvalidDataException instead.

Specified by:
add in interface DataValue
Parameters:
value - a data value to be added to this data value
Returns:
Returns an added data value.
Throws:
InvalidDataException - if the data values cannot be added

ceil

public DataValue ceil(DataValue precision)
Quantizes the current value up to an even value within given precision. The returned value is more than, or equal with the current value, but not more than the current value added by given precision.

Color values do not support this operation, but they always throw an Error instead.

Specified by:
ceil in interface DataValue
Parameters:
precision - the precision within quantization occurs
Returns:
a new data object, sharing the current type of this object
See Also:
floor(com.davisor.data.DataValue)

deinterpolate

public double deinterpolate(DataValue min,
                            DataValue max)
                     throws InvalidDataException
De-interpolates the current data value to a scalar value, in respect, with the given data value range. This gives scalar values between [0,1], when the current data value is within the range, negative scalar values when the data value falls below the range and scalar values greater than 1.0, otherwise.

Color values do not support this operation, but they always throw an InvalidDataException instead.

Specified by:
deinterpolate in interface DataValue
Parameters:
min - a data value representing a range's lower limit
max - a data value representing a range's higher limit
Returns:
de-interpolated scalar value
Throws:
InvalidDataException - if the de-interpolation fails
See Also:
DataValue.interpolate(double, com.davisor.data.DataValue), DeinterpolationException, TupleData.deinterpolate(com.davisor.data.DataValue, com.davisor.data.DataValue)

div

public DataValue div(DataValue value)
              throws InvalidDataException
Divides current data value with given value.

Color values do not support this operation, but they always throw an InvalidDataException instead.

Specified by:
div in interface DataValue
Parameters:
value - the divider
Returns:
result of division.
Throws:
InvalidDataException - if current data value cannot be divided

floor

public DataValue floor(DataValue precision)
Quantizes the current value down to an even value within given precision. The returned value is less than, or equal with the current value, but not less than the current value substracted by given precision.

Color values do not support this operation, but they always throw an Error instead.

Specified by:
floor in interface DataValue
Parameters:
precision - the precision within quantization occurs
Returns:
a new data object, sharing the current type of this object
See Also:
ceil(com.davisor.data.DataValue)

getFormat

public java.lang.String getFormat()
Gets the type specification optimal for this particular value. The specification returned may differ from the current type specification. If so, setting the current type specification equal with the value returned by this method, will ensure optimal value formatting and parsing for values of the same magnitude as this value.

The color data optimal specification string is always the current specification string for the current type.

Specified by:
getFormat in interface DataValue
See Also:
Data.getType(), Type.getFormat(), Type.setFormat(java.lang.String)

interpolate

public DataValue interpolate(double value,
                             DataValue max)
                      throws InvalidDataException
Interpolates the given scalar value to a data value, in respect, with the range defined by this value and the given maximum value. Scalar values between [0,1] result in data values between the current value and the given maximum value. Negative scalar values result in values smaller than this value, and scalar values higher than 1.0 return data values above the given maximum.

Color values do not support this operation, but they always throw an InvalidDataException instead.

Specified by:
interpolate in interface DataValue
Parameters:
value - the scalar value to be interpolated
max - a data value representing a range's higher limit
Returns:
interpolated data value
Throws:
InvalidDataException - if the interpolation fails
See Also:
DataValue.deinterpolate(com.davisor.data.DataValue, com.davisor.data.DataValue)

max

public DataValue max(DataValue value)
              throws InvalidDataException
Finds the largest data value.

Specified by:
max in interface DataValue
Parameters:
value - a data value to be compared with this data value
Returns:
the largest of the compared data values
Throws:
InvalidDataException - if the data value cannot be compared

min

public DataValue min(DataValue value)
              throws InvalidDataException
Finds the smallest data value.

Specified by:
min in interface DataValue
Parameters:
value - a data value to be compared with this data value
Returns:
the smallest of the compared data values
Throws:
InvalidDataException - if the data value cannot be compared

mul

public DataValue mul(DataValue value)
              throws InvalidDataException
Multiplies current data value with given value.

Color values do not support this operation, but they always throw an InvalidDataException instead.

Specified by:
mul in interface DataValue
Parameters:
value - the multiplier
Returns:
result of multiplication.
Throws:
InvalidDataException - if current data value cannot be multiplied

negate

public DataValue negate()
                 throws InvalidDataException
Returns the negation of current value.

Specified by:
negate in interface DataValue
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if absolute value can not be computed

setValue

public void setValue(java.lang.String value,
                     java.text.ParsePosition status)
              throws InvalidDataException
Attempts to set the color value, but since colors are immutable objects, this will always fail.

Specified by:
setValue in interface DataValue
Parameters:
value - new data value (may be null)
status - parse status (may be null)
Throws:
InvalidDataException - always.
See Also:
CountType.countValue(String,ParsePosition)

sub

public DataValue sub(DataValue value)
              throws InvalidDataException
Substracts a data value.

Color values do not support this operation, but they always throw an InvalidDataException>/CODE> instead.

Specified by:
sub in interface DataValue
Parameters:
value - a data value to be substracted from this data value
Returns:
a substracted data value
Throws:
InvalidDataException - if the value cannot be substracted

toStatement

public void toStatement(java.sql.PreparedStatement statement,
                        int parameter)
                 throws java.sql.SQLException
Converts color data to a prepared statement input parameter. The color will be encoded as an integer RGB value.

Specified by:
toStatement in interface DataValue
Parameters:
statement - statement to be updated
parameter - index of the statement parameter to be updated
Throws:
java.sql.SQLException - if a SQL error occurs

toXML

public java.lang.String toXML()
Gets the XML string representation of this value.

Specified by:
toXML in interface DataValue
Returns:
complete string representing the data.
See Also:
toString()

unit

public DataValue unit(DataValue targetValue,
                      double maxSteps)
Gets the largest suitable unit step to represent a value change between this value and the given target value.

Color values do not support this operation, but they always throw an Error instead.

Specified by:
unit in interface DataValue
Parameters:
targetValue - target value to be reached (may be null)
maxSteps - maximum number of steps (zero leaves the choise open)
Returns:
step value, as described above

dup

public com.davisor.core.Dupable dup()
Makes a deep copy of this object.

Specified by:
dup in interface com.davisor.core.Dupable

toString

public java.lang.String toString()
Gets the paint spesification string of this color.

Specified by:
toString in interface DataValue
See Also:
PaintParameters.PaintParameters(Paint)

getCreationString

public java.lang.String getCreationString()
Implements PaintValue interface method

Specified by:
getCreationString in interface PaintValue

setCreationString

public void setCreationString(java.lang.String creationString)
Implements PaintValue interface method

Specified by:
setCreationString in interface PaintValue

brighterPaint

public static int brighterPaint(int value)
Computes a brighter RGB component value.

See Also:
brighterPaint(), brighterPaint(Color), brighterPaint(int,float), brighterPaint(Color,float), darkerPaint(int)

brighterPaint

public static java.awt.Color brighterPaint(java.awt.Color color)
Creates a brighter version of the given color. Unlike (at least some) standard Java implementations, this method preserves the color alpha component value.

Parameters:
color - source color
Returns:
a new Color object, containing a brighter version of the given color
See Also:
brighterPaint(), brighterPaint(int), brighterPaint(int,float), brighterPaint(Color,float), darkerPaint(Color)

brighterPaint

public static int brighterPaint(int value,
                                float scale)
Computes a brighter RGB component value. The brighter value will be computed by multiplying the original value on a 1/scale. The brighter value will be at least one step brighter than the original value, regardless of the given scale factor. However, the returned value will also be cropped to a value between [0,255], inclusive.

Parameters:
value - RGB component value [0,255]
scale - scale factor [0,1]
See Also:
brighterPaint(), brighterPaint(int), brighterPaint(Color), brighterPaint(Color,float), darkerPaint(int,float)

brighterPaint

public static java.awt.Color brighterPaint(java.awt.Color color,
                                           float scale)
Creates a brighter version of the given color. Unlike (at least some) standard Java implementations, this method preserves the color alpha component value.

Parameters:
color - source color
scale - scale factor
Returns:
a new Color object, containing a brighter version of the given color
See Also:
brighterPaint(), brighterPaint(int), brighterPaint(Color), brighterPaint(int,float), darkerPaint(Color,float)

darkerPaint

public static int darkerPaint(int value)
Computes a darker RGB component value. Values greater than 364 are lowered to 255. Values between 4 and 364 are multiplied with 0.7. Values between 1 and 3 are decremented by one. Smaller values are set to zero values.

See Also:
brighterPaint(int), darkerPaint(), darkerPaint(Color), darkerPaint(int,float), darkerPaint(Color,float)

darkerPaint

public static java.awt.Color darkerPaint(java.awt.Color color)
Creates a darker version of the given color. Unlike (at least some) standard Java implementations, this method preserves the color alpha component value.

Parameters:
color - source color
Returns:
a new Color object, containing a darker version of given color
See Also:
brighterPaint(Color), darkerPaint(), darkerPaint(int), darkerPaint(int,float), darkerPaint(Color,float)

darkerPaint

public static int darkerPaint(int value,
                              float scale)
Computes a darker RGB component value. The darker value will be computed by multiplying the original value with a scale. The darker value will be at least one step darker than the original value, regardless of the given scale factor. However, the returned value will also be cropped to a value between [0,255], inclusive.

Parameters:
value - RGB component value [0,255]
scale - scale factor [0,1]
See Also:
brighterPaint(int,float), darkerPaint(), darkerPaint(int), darkerPaint(Color), darkerPaint(Color,float)

darkerPaint

public static java.awt.Color darkerPaint(java.awt.Color color,
                                         float scale)
Creates a darker version of the given color. Unlike (at least some) standard Java implementations, this method preserves the color alpha component value.

Parameters:
color - source color
scale - scale factor
Returns:
a new Color object, containing a brighter version of the given color
See Also:
brighterPaint(Color,float), darkerPaint(), darkerPaint(int), darkerPaint(Color), darkerPaint(int,float)

getRGBA

public static int getRGBA(java.awt.Color color,
                          float alpha,
                          boolean preserve)
Merges a color RGB value with the given normalized alpha value.

Parameters:
color - the color from which the color RGB value is taken (may be null)
alpha - the normalized alpha value (0.0-1.0)
preserve - should the previous alpha value of the color be merged with the given normalized alpha value or not
See Also:
setAlpha(java.awt.Color, float)

setAlpha

public static java.awt.Color setAlpha(java.awt.Color color,
                                      float alpha)
Sets color transparency for the given color. If the color transparency matches the given target transparency, then the original color is returned unmodified. If the color transparency does not match the given target transparency, a new color with the requested transparency and the original color RGB components is created and returned. null color will default to black.

Parameters:
color - the color from which the color RGB value is taken (may be null)
alpha - the normalized alpha value (0.0-1.0)
See Also:
getRGBA(java.awt.Color, float, boolean)

cropColorScale

protected static float cropColorScale(float scale)
Crops the given scale to the range of [0,1].



Copyright © 2001-2004 Davisor Oy. All Rights Reserved.