Class Color


public class Color extends Object
Immutable class representing a computer displayable color. Colors are represented using the hexadecimal, rgb, and cmyk methods.

Class-defined colors match CSS usage.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    The amount of Blue in the color (a value between 0 and 255) Using RGB representation.
    static Color
    Constant representation of CSS-compatible Black.
    static Color
    Constant representation of CSS-compatible Blue.
    static Color
    Constant representation of CSS-compatible Brown.
    double
    The amount of Cyan in the color (a value between 0 and 255) using CMYK representation.
    int
    The amount of Green in the color (a value between 0 and 255) Using RGB representation.
    static Color
    Constant representation of CSS-compatible Green.
    int
    The hex-code representation of the color.
    double
    The CMYK Key value of the color.
    double
    The amount of Magenta in the color (a value between 0 and 255) using CMYK representation.
    static Color
    Constant representation of CSS-compatible Orange.
    static Color
    Constant representation of CSS-compatible Purple.
    int
    The amount of Red in the color (a value between 0 and 255).
    static Color
    Constant representation of CSS-compatible Red.
    static Color
    Constant representation of CSS-compatible White.
    double
    The amount of Yello in the color (a value between 0 and 255) using CMYK representation.
    static Color
    Constant representation of CSS-compatible Yellow.
  • Constructor Summary

    Constructors
    Constructor
    Description
    No args constructor.
    Color​(double c, double m, double y, double k)
    Constructs a color of a given CMYK value.
    Color​(int r, int g, int b)
    Constructs a color of given r, g, and b values.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Color
    mix​(Color x, Color y)
    Gives the color midway between two colors as its "mixture." Mixed color is the average of the rgb values of the two colors.
    static Color
    mix​(Color x, Color y, double ratio)
    Gives the color between x and y with a given weight/ratio applied to x.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • r

      public final int r
      The amount of Red in the color (a value between 0 and 255). Using RGB representation.
    • g

      public final int g
      The amount of Green in the color (a value between 0 and 255) Using RGB representation.
    • b

      public final int b
      The amount of Blue in the color (a value between 0 and 255) Using RGB representation.
    • c

      public final double c
      The amount of Cyan in the color (a value between 0 and 255) using CMYK representation.
    • m

      public final double m
      The amount of Magenta in the color (a value between 0 and 255) using CMYK representation.
    • y

      public final double y
      The amount of Yello in the color (a value between 0 and 255) using CMYK representation.
    • k

      public final double k
      The CMYK Key value of the color.
    • hex

      public final int hex
      The hex-code representation of the color.
    • RED

      public static final Color RED
      Constant representation of CSS-compatible Red.
    • GREEN

      public static final Color GREEN
      Constant representation of CSS-compatible Green.
    • BLUE

      public static final Color BLUE
      Constant representation of CSS-compatible Blue.
    • BROWN

      public static final Color BROWN
      Constant representation of CSS-compatible Brown.
    • ORANGE

      public static final Color ORANGE
      Constant representation of CSS-compatible Orange.
    • PURPLE

      public static final Color PURPLE
      Constant representation of CSS-compatible Purple.
    • YELLOW

      public static final Color YELLOW
      Constant representation of CSS-compatible Yellow.
    • BLACK

      public static final Color BLACK
      Constant representation of CSS-compatible Black.
    • WHITE

      public static final Color WHITE
      Constant representation of CSS-compatible White.
  • Constructor Details

    • Color

      public Color()
      No args constructor. Defaults to black.
    • Color

      public Color(int r, int g, int b) throws IllegalArgumentException
      Constructs a color of given r, g, and b values.
      Parameters:
      r - the quantity of red in the color (range from 0 to 255)
      g - the quantity of green in the color (range from 0 to 255)
      b - the quantity of blue in the color (range from 0 to 255)
      Throws:
      IllegalArgumentException
    • Color

      public Color(double c, double m, double y, double k)
      Constructs a color of a given CMYK value.
      Parameters:
      c - amount of Cyan in the color (range from 0 to 1)
      m - amount of Magenta in the color (range from 0 to 1)
      y - amount of Yello in the color (range from 0 to 1)
      k - amount of black (key value) in the color (range from 0 to 1)
  • Method Details

    • mix

      public static Color mix(Color x, Color y)
      Gives the color midway between two colors as its "mixture." Mixed color is the average of the rgb values of the two colors.
      Parameters:
      x - a color to mix
      y - a second color to mix with x
      Returns:
      a color with the average rgb values of x and y.
    • mix

      public static Color mix(Color x, Color y, double ratio)
      Gives the color between x and y with a given weight/ratio applied to x. Higher ratio values imply greater weight towards and lower values a higher weight towards y.
      Parameters:
      x - a color to mix
      y - a second color to mix with x
      ratio - the proportion of x in the final mixture
      Returns:
      a Color equivalent to the mixture of x and y with the given weight applied to x.