Class Color

  • All Implemented Interfaces:
    Serializable

    public class Color
    extends Object
    implements Serializable

    A simple class representing a color in a platform-independent way.

    In the most basic form, colors are defined using RGB components. In more complex scenarios a color can be represented by a string - for instance, if it is determined by a complex style sheet definition. The isLogicColor() method can be used to distinguish between these kinds of objects. If it returns true, the color is defined based on a string, and thus concrete RGB values are not available.

    Color objects are created using static factory methods. They are immutable and thus can be shared between different threads.

    Version:
    $Id: Color.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    See Also:
    Serialized Form
    • Field Detail

      • COMPONENT_UNDEFINED

        public static final int COMPONENT_UNDEFINED
        Constant for an undefined color component. This value is returned by the access methods for the single color components (getRed(), getGreen(), or getBlue() to indicate that this component is undefined - which is the case for logic colors.
        Since:
        1.3
        See Also:
        Constant Field Values
      • UNDEFINED

        public static final Color UNDEFINED
        Constant for an undefined color. This constant defines a special instance which does not have RGB components nor a logic color definition. It can be used to represent an undefined color.
        Since:
        1.3
    • Method Detail

      • isLogicColor

        public boolean isLogicColor()
        Returns a flag whether this Color instance is based on a logic color definition. This means that it was constructed from a text-based definition. In this case, the values of the RGB components are undefined.
        Returns:
        true if this Color instance is based on a logic definition, false otherwise
        Since:
        1.3
      • getBlue

        public int getBlue()
        Returns the blue component of this color. This value is only defined if isLogicColor() returns false.
        Returns:
        the blue component
      • getGreen

        public int getGreen()
        Returns the green component of this color. This value is only defined if isLogicColor() returns false.
        Returns:
        the green component
      • getRed

        public int getRed()
        Returns the red component of this color. This value is only defined if isLogicColor() returns false.
        Returns:
        the red component
      • getColorDefinition

        public String getColorDefinition()
        Returns the logic color definition this Color instance is based on. This value is only defined of isLogicColor() returns true.
        Returns:
        the logic color definition
        Since:
        1.3
      • toString

        public String toString()
        Returns a String representation of this object.
        Overrides:
        toString in class Object
        Returns:
        a String for this object
      • equals

        public boolean equals​(Object obj)
        Tests if a passed in object equals this object. Two Color objects are considered equal if and only if all color components are equal.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to
        Returns:
        a flag whether the objects are equal
      • hashCode

        public int hashCode()
        Determines a hash code for this object.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code
      • newInstance

        @Deprecated
        public static Color newInstance​(int r,
                                        int g,
                                        int b)
        Deprecated.
        Use newRGBInstance() instead.
        Creates a new instance of Color and initializes the single components. This method will check whether the passed in values are valid; if one is out of range, an exception will be thrown.
        Parameters:
        r - the r component
        g - the g component
        b - the b component
        Returns:
        the new Color instance
        Throws:
        IllegalArgumentException - if a component is invalid
      • newRGBInstance

        public static Color newRGBInstance​(int r,
                                           int g,
                                           int b)
        Creates a new instance of Color and initializes it with the given components for the red, green, and blue part. This method checks whether the passed in values are valid; if one argument is out of range, an exception is thrown.
        Parameters:
        r - the r component
        g - the g component
        b - the b component
        Returns:
        the new Color instance
        Throws:
        IllegalArgumentException - if a component is invalid
        Since:
        1.3
      • newLogicInstance

        public static Color newLogicInstance​(String coldef)
        Creates a new instance of Color based on a logic, text-based color definition. The passed in string is just stored and not interpreted in any form. It must not be null.
        Parameters:
        coldef - the logic color definition
        Returns:
        the new Color instance
        Throws:
        IllegalArgumentException - if the color definition is undefined
        Since:
        1.3