Class Color

java.lang.Object
edu.wpi.first.wpilibj.util.Color

public class Color extends Object
Represents colors.

Limited to 12 bits of precision.

  • Field Details

  • Constructor Details

    • Color

      public Color()
      Constructs a default color (black).
    • Color

      public Color(double red, double green, double blue)
      Constructs a Color from doubles.
      Parameters:
      red - Red value (0-1)
      green - Green value (0-1)
      blue - Blue value (0-1)
    • Color

      public Color(int red, int green, int blue)
      Constructs a Color from ints.
      Parameters:
      red - Red value (0-255)
      green - Green value (0-255)
      blue - Blue value (0-255)
    • Color

      public Color(Color8Bit color)
      Constructs a Color from a Color8Bit.
      Parameters:
      color - The color
    • Color

      public Color(String hexString)
      Constructs a Color from a hex string.
      Parameters:
      hexString - a string of the format #RRGGBB
      Throws:
      IllegalArgumentException - if the hex string is invalid.
  • Method Details

    • fromHSV

      public static Color fromHSV(int h, int s, int v)
      Creates a Color from HSV values.
      Parameters:
      h - The h value [0-180)
      s - The s value [0-255]
      v - The v value [0-255]
      Returns:
      The color
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toHexString

      public String toHexString()
      Return this color represented as a hex string.
      Returns:
      a string of the format #RRGGBB
    • hsvToRgb

      public static int hsvToRgb(int h, int s, int v)
      Converts HSV values to RGB values. The returned RGB color is packed into a 32-bit integer for memory performance reasons.
      Parameters:
      h - The h value [0-180)
      s - The s value [0-255]
      v - The v value [0-255]
      Returns:
      the packed RGB color
    • packRGB

      public static int packRGB(int r, int g, int b)
      Packs 3 RGB values into a single 32-bit integer. These values can be unpacked with unpackRGB(int, RGBChannel) to retrieve the values. This is helpful for avoiding memory allocations of new Color objects and its resulting garbage collector pressure.
      Parameters:
      r - the value of the first channel to pack
      g - the value of the second channel to pack
      b - the value of the third channel to pack
      Returns:
      the packed integer
    • unpackRGB

      public static int unpackRGB(int packedColor, Color.RGBChannel channel)
      Unpacks a single color channel from a packed 32-bit RGB integer.

      Note: Packed RGB colors are expected to be in byte order [empty][red][green][blue].

      Parameters:
      packedColor - the packed color to extract from
      channel - the color channel to unpack
      Returns:
      the value of the stored color channel
    • lerpRGB

      public static Color lerpRGB(Color a, Color b, double t)
      Performs a linear interpolation between two colors in the RGB colorspace.
      Parameters:
      a - the first color to interpolate from
      b - the second color to interpolate from
      t - the interpolation scale in [0, 1]
      Returns:
      the interpolated color
    • lerpRGB

      public static int lerpRGB(double r1, double g1, double b1, double r2, double g2, double b2, double t)
      Linearly interpolates between two RGB colors represented by the (r1, g1, b1) and (r2, g2, b2) triplets. For memory performance reasons, the output color is returned packed into a single 32-bit integer; use unpackRGB(int, RGBChannel) to extract the values for the individual red, green, and blue channels.
      Parameters:
      r1 - the red value of the first color, in [0, 1]
      g1 - the green value of the first color, in [0, 1]
      b1 - the blue value of the first color, in [0, 1]
      r2 - the red value of the second color, in [0, 1]
      g2 - the green value of the second color, in [0, 1]
      b2 - the blue value of the second color, in [0, 1]
      t - the interpolation value, in [0, 1]
      Returns:
      the interpolated color, packed in a 32-bit integer
    • lerpRGB

      public static int lerpRGB(int r1, int g1, int b1, int r2, int g2, int b2, double t)
      Linearly interpolates between two RGB colors represented by the (r1, g1, b1) and (r2, g2, b2) triplets. For memory performance reasons, the output color is returned packed into a single 32-bit integer; use unpackRGB(int, RGBChannel) to extract the values for the individual red, green, and blue channels.
      Parameters:
      r1 - the red value of the first color, in [0, 255]
      g1 - the green value of the first color, in [0, 255]
      b1 - the blue value of the first color, in [0, 255]
      r2 - the red value of the second color, in [0, 255]
      g2 - the green value of the second color, in [0, 255]
      b2 - the blue value of the second color, in [0, 255]
      t - the interpolation value, in [0, 1]
      Returns:
      the interpolated color, packed in a 32-bit integer