Class ColorTools

java.lang.Object
qupath.lib.common.ColorTools

public final class ColorTools extends Object
Static functions to help work with RGB(A) colors using packed ints.
Author:
Pete Bankhead
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Integer
    Packed int representing black.
    static final Integer
    Packed int representing blue.
    static final Integer
    Packed int representing cyan.
    static final Integer
    Packed int representing green.
    static final Integer
    Packed int representing magenta.
    static final Integer
    Mask for use when extracting the alpha component from a packed ARGB int value.
    static final Integer
    Mask for use when extracting the blue component from a packed (A)RGB int value.
    static final Integer
    Mask for use when extracting the green component from a packed (A)RGB int value.
    static final Integer
    Mask for use when extracting the red component from a packed (A)RGB int value.
    static final Integer
    Packed int representing red.
    static final Integer
    Packed int representing white.
    static final Integer
    Packed int representing yellow.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    alpha(int argb)
    Extract the 8-bit alpha value from a packed ARGB value.
    static int
    blue(int rgb)
    Extract the 8-bit blue value from a packed RGB value.
    static int
    clip255(double val)
    Convert a double value to an int, flooring and clipping to the range 0-255.
    static int
    do8BitRangeCheck(double v)
    Clip an input value to be an integer in the range 0-255 (with rounding down).
    static int
    Clip an input value to be an integer in the range 0-255 (with rounding down).
    static int
    Clip an input value to be an integer in the range 0-255.
    static int
    green(int rgb)
    Extract the 8-bit green value from a packed RGB value.
    static int
    makeRGB(int r, int g, int b)
    static int
    makeRGBA(int r, int g, int b, int a)
    Deprecated.
    The naming and order of arguments is misleading.
    static int
    makeScaledRGB(int rgb, double scale)
    Scale the RGB channels for a color by a fixed amount.
    static int
    packARGB(int a, int r, int g, int b)
    Make a packed ARGB value from specified input values.
    static int
    packClippedARGB(int a, int r, int g, int b)
    Make a packed ARGB value from specified input values, clipping to the range 0-255.
    static int
    packClippedRGB(int r, int g, int b)
    Make a packed RGB value from specified input values, clipping to the range 0-255.
    static int
    packRGB(int r, int g, int b)
    Make a packed RGB value from specified input values.
    static int
    red(int rgb)
    Extract the 8-bit red value from a packed RGB value.
    static int[]
    unpackARGB(int rgb)
    Unpack a packed ARGB int into red, green and blue values, returning the result as a 4-element int array.
    static int[]
    unpackARGB(int rgb, int[] array)
    Unpack a packed ARGB int into red, green and blue values, into a 4-element int array.
    static int[]
    unpackRGB(int rgb)
    Unpack a packed (A)RGB int into red, green and blue values, returning the result as a 3-element int array.
    static int[]
    unpackRGB(int rgb, int[] array)
    Unpack a packed (A)RGB int into red, green and blue values, into a 3-element int array.

    Methods inherited from class java.lang.Object

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

    • WHITE

      public static final Integer WHITE
      Packed int representing white.
    • BLACK

      public static final Integer BLACK
      Packed int representing black.
    • RED

      public static final Integer RED
      Packed int representing red.
    • GREEN

      public static final Integer GREEN
      Packed int representing green.
    • BLUE

      public static final Integer BLUE
      Packed int representing blue.
    • MAGENTA

      public static final Integer MAGENTA
      Packed int representing magenta.
    • CYAN

      public static final Integer CYAN
      Packed int representing cyan.
    • YELLOW

      public static final Integer YELLOW
      Packed int representing yellow.
    • MASK_ALPHA

      public static final Integer MASK_ALPHA
      Mask for use when extracting the alpha component from a packed ARGB int value.
    • MASK_RED

      public static final Integer MASK_RED
      Mask for use when extracting the red component from a packed (A)RGB int value.
    • MASK_GREEN

      public static final Integer MASK_GREEN
      Mask for use when extracting the green component from a packed (A)RGB int value.
    • MASK_BLUE

      public static final Integer MASK_BLUE
      Mask for use when extracting the blue component from a packed (A)RGB int value.
  • Method Details

    • makeRGB

      @Deprecated public static int makeRGB(int r, int g, int b)
      Make a packed RGB value from specified input values.

      Input r, g and b should be in the range 0-255 - but no checking is applied. Rather, the input values are simply shifted as they are.

      The alpha value is 255.

      Parameters:
      r -
      g -
      b -
      Returns:
    • packRGB

      public static int packRGB(int r, int g, int b)
      Make a packed RGB value from specified input values. This is equivalent to an ARGB value with alpha set to 255, following Java Color.

      Input r, g, and b should be in the range 0-255; only the lower 8 bits are used.

      Parameters:
      r -
      g -
      b -
      Returns:
      packed ARGB value
      See Also:
    • unpackRGB

      public static int[] unpackRGB(int rgb)
      Unpack a packed (A)RGB int into red, green and blue values, returning the result as a 3-element int array.
      Parameters:
      rgb -
      Returns:
      Since:
      v0.4.0
    • unpackRGB

      public static int[] unpackRGB(int rgb, int[] array)
      Unpack a packed (A)RGB int into red, green and blue values, into a 3-element int array.
      Parameters:
      rgb -
      array - optional preallocated input array
      Returns:
      the red, green and blue values in array if provided and long enough, otherwise a new int array storing the values
      Since:
      v0.4.0
    • unpackARGB

      public static int[] unpackARGB(int rgb)
      Unpack a packed ARGB int into red, green and blue values, returning the result as a 4-element int array.
      Parameters:
      rgb -
      Returns:
      Since:
      v0.4.0
    • unpackARGB

      public static int[] unpackARGB(int rgb, int[] array)
      Unpack a packed ARGB int into red, green and blue values, into a 4-element int array.
      Parameters:
      rgb -
      array - optional preallocated input array
      Returns:
      the alpha, red, green and blue values in array if provided and long enough, otherwise a new int array storing the values
      Since:
      v0.4.0
    • packClippedRGB

      public static int packClippedRGB(int r, int g, int b)
      Make a packed RGB value from specified input values, clipping to the range 0-255. This is equivalent to an ARGB value with alpha set to 255, following Java Color.

      Input r, g, and b should be in the range 0-255, but if they are not they are clipped to the closest valid value.

      Parameters:
      r -
      g -
      b -
      Returns:
      packed ARGB value // * @see #packRGB(int, int, int)
    • makeRGBA

      @Deprecated public static int makeRGBA(int r, int g, int b, int a)
      Deprecated.
      The naming and order of arguments is misleading. The output is a packed ARGB value, but arguments are provided in the order red, green, blue, alpha.
      Make a packed ARGB value from specified input values.

      Input r, g, b and a should be in the range 0-255 - but no checking is applied. Rather, the input values are simply shifted as they are.

      Parameters:
      r -
      g -
      b -
      a -
      Returns:
      See Also:
    • packARGB

      public static int packARGB(int a, int r, int g, int b)
      Make a packed ARGB value from specified input values.

      Input a, r, g, and b should be in the range 0-255; only the lower 8 bits are used.

      Warning! Note the order of the input values. This differs from the (deprecated) method makeRGBA(int, int, int, int)

      Parameters:
      a -
      r -
      g -
      b -
      Returns:
      packed ARGB value
      See Also:
    • packClippedARGB

      public static int packClippedARGB(int a, int r, int g, int b)
      Make a packed ARGB value from specified input values, clipping to the range 0-255.

      Input a, r, g, and b should be in the range 0-255, but if they are not they are clipped to the closest valid value.

      Warning! Note the order of the input values. This differs from the (deprecated) method makeRGBA(int, int, int, int)

      Parameters:
      a -
      r -
      g -
      b -
      Returns:
      packed ARGB value
      See Also:
    • do8BitRangeCheck

      public static int do8BitRangeCheck(int v)
      Clip an input value to be an integer in the range 0-255.
      Parameters:
      v -
      Returns:
    • do8BitRangeCheck

      public static int do8BitRangeCheck(float v)
      Clip an input value to be an integer in the range 0-255 (with rounding down).
      Parameters:
      v -
      Returns:
    • do8BitRangeCheck

      public static int do8BitRangeCheck(double v)
      Clip an input value to be an integer in the range 0-255 (with rounding down).
      Parameters:
      v -
      Returns:
    • alpha

      public static int alpha(int argb)
      Extract the 8-bit alpha value from a packed ARGB value.
      Parameters:
      argb -
      Returns:
    • red

      public static int red(int rgb)
      Extract the 8-bit red value from a packed RGB value.
      Parameters:
      rgb -
      Returns:
    • green

      public static int green(int rgb)
      Extract the 8-bit green value from a packed RGB value.
      Parameters:
      rgb -
      Returns:
    • blue

      public static int blue(int rgb)
      Extract the 8-bit blue value from a packed RGB value.
      Parameters:
      rgb -
      Returns:
    • makeScaledRGB

      public static int makeScaledRGB(int rgb, double scale)
      Scale the RGB channels for a color by a fixed amount.

      This is useful for brightening/darkening an input color.

      Parameters:
      rgb -
      scale -
      Returns:
    • clip255

      public static int clip255(double val)
      Convert a double value to an int, flooring and clipping to the range 0-255.
      Parameters:
      val -
      Returns: