Class RgbUtils

java.lang.Object
imagingbook.common.color.RgbUtils

public abstract class RgbUtils extends Object
This class defines static methods for manipulating and converting RGB colors.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double[]
    ITU BR.601 weights for RGB to Y (luma) conversion.
    static final double[]
    ITU BR.709 weights for RGB to Y (luma) conversion.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    decodeIntToRgb(int argb, int[] RGB)
    Converts the given integer-encoded 8-bit RGB color to a 3-element int[] with components red, green and blue.
    static int[]
    denormalize(float[] rgb)
    Converts float RGB values (with components assumed to be in [0,1]) to integer values in [0,255].
    static int[]
    denormalizeD(double[] rgb)
    Converts double RGB values (with components assumed to be in [0,1]) to integer values in [0,255].
    static int
    encodeRgbToInt(int[] RGB)
    Encodes the given RGB component values into a single 32-bit int value in ARGB format (with transparency A set to zero).
    static int
    encodeRgbToInt(int r, int g, int b)
    Encodes the given r, g, b component values into a single 32-bit int value in ARGB format (with transparency A set to zero).
    static double[]
     
    static Color
    interpolate(Color[] palette, double t)
    Interpolates linearly between the colors in the specified color palette.
    static Color
    interpolate(Color ca, Color cb, double t)
    Interpolates linearly between two specified colors.
    static int[]
    intToRgb(int argb)
    Converts the given integer-encoded 8-bit RGB color to a 3-element int[] with components red, green and blue.
    static float[]
    normalize(int[] RGB)
    Converts integer RGB values (with components assumed to be in [0,255]) to float values in [0,1].
    static double[]
    normalizeD(int[] RGB)
     
    static int
    rgbToInt(int[] RGB)
    Converts the given 3-element int[] with components red, green and blue to an integer-encoded 8-bit RGB color.

    Methods inherited from class java.lang.Object

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

    • ITU601RgbWeights

      public static final double[] ITU601RgbWeights
      ITU BR.601 weights for RGB to Y (luma) conversion.
    • ITU709RgbWeights

      public static final double[] ITU709RgbWeights
      ITU BR.709 weights for RGB to Y (luma) conversion.
  • Method Details

    • getDefaultWeights

      public static double[] getDefaultWeights()
    • intToRgb

      public static int[] intToRgb(int argb)
      Converts the given integer-encoded 8-bit RGB color to a 3-element int[] with components red, green and blue.
      Parameters:
      argb - integer-encoded 8-bit RGB color in ARGB format
      Returns:
      int[] with R, G, B components in [0,255]
    • rgbToInt

      public static int rgbToInt(int[] RGB)
      Converts the given 3-element int[] with components red, green and blue to an integer-encoded 8-bit RGB color.
      Parameters:
      RGB - int[] with R, G, B components in [0,255]
      Returns:
      integer-encoded 8-bit RGB color in ARGB format
      See Also:
    • decodeIntToRgb

      public static void decodeIntToRgb(int argb, int[] RGB)
      Converts the given integer-encoded 8-bit RGB color to a 3-element int[] with components red, green and blue. Fills the specified int[], nothing is returned.
      Parameters:
      argb - integer-encoded 8-bit RGB color in ARGB format
      RGB - int[] with R, G, B components
    • encodeRgbToInt

      public static int encodeRgbToInt(int[] RGB)
      Encodes the given RGB component values into a single 32-bit int value in ARGB format (with transparency A set to zero).
      Parameters:
      RGB - int[] with R, G, B components
      Returns:
      integer-encoded 8-bit RGB color in ARGB format
    • encodeRgbToInt

      public static int encodeRgbToInt(int r, int g, int b)
      Encodes the given r, g, b component values into a single 32-bit int value in ARGB format (with transparency A set to zero).
      Parameters:
      r - red component value
      g - breen component value
      b - blue component value
      Returns:
      integer-encoded 8-bit RGB color in ARGB format
    • normalize

      public static float[] normalize(int[] RGB)
      Converts integer RGB values (with components assumed to be in [0,255]) to float values in [0,1].
      Parameters:
      RGB - a sequence of R,G,B values or int[]
      Returns:
      the RGB values normalized to [0,1]
    • normalizeD

      public static double[] normalizeD(int[] RGB)
    • denormalize

      public static int[] denormalize(float[] rgb)
      Converts float RGB values (with components assumed to be in [0,1]) to integer values in [0,255].
      Parameters:
      rgb - RGB float values in [0,1]
      Returns:
      RGB integer values in [0,255]
    • denormalizeD

      public static int[] denormalizeD(double[] rgb)
      Converts double RGB values (with components assumed to be in [0,1]) to integer values in [0,255].
      Parameters:
      rgb - RGB float values in [0,1]
      Returns:
      RGB integer values in [0,255]
    • interpolate

      public static Color interpolate(Color ca, Color cb, double t)
      Interpolates linearly between two specified colors.
      Parameters:
      ca - first color (to be interpolated from)
      cb - second color (to be interpolated to)
      t - interpolation coefficient, must be in [0,1]
      Returns:
      the interpolated color
    • interpolate

      public static Color interpolate(Color[] palette, double t)
      Interpolates linearly between the colors in the specified color palette. The interpolation coefficient must be in [0,1]. If 0, the first palette color is returned, if 1 the last color.
      Parameters:
      palette - an array of colors (at least 2)
      t - interpolation coefficient, must be in [0,1]
      Returns:
      the interpolated color