Class Palette

java.lang.Object
net.mtrop.doom.graphics.Palette
All Implemented Interfaces:
BinaryObject

public class Palette extends Object implements BinaryObject
The palette that makes up the Doom Engine's color palette. The colors are all opaque. This contains an indexed set of 256 colors. Doom's PLAYPAL lump contains several of these. TODO: Revisit "nearest match" with better algorithm.
Author:
Matthew Tropiano
  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.mtrop.doom.object.BinaryObject

    BinaryObject.InlineScanner<BO extends BinaryObject>, BinaryObject.Reflect, BinaryObject.Scanner<BO extends BinaryObject>, BinaryObject.Shared, BinaryObject.Transformer<BO extends BinaryObject>
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Number of bytes per color in a Doom palette.
    protected byte[][]
    The palette of colors.
    static final int
    A single palette's length in bytes.
    static final int
    The number of total colors in a standard Doom palette.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new palette of black, opaque colors.
  • Method Summary

    Modifier and Type
    Method
    Description
    addColor(int index, double scalar, int argb)
    Sets the color of a specific index in the Palette by additively blending it with another color.
    addColor(int index, double scalar, int red, int green, int blue)
    Sets the color of a specific index in the Palette by additively blending it with another color.
    addColor(int startIndex, int endIndex, double scalar, int argb)
    Sets the color of a range of indices in the Palette by additively blending it with another color.
    addColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
    Sets the color of a range of indices in the Palette by additively blending it with another color.
    Makes a copy of this palette.
    getColor(int index)
    Returns the Color of a specific index in the palette.
    int
    getColorARGB(int index)
    Returns the Color of a specific index in the palette as a 32-bit ARGB integer.
    int
    Returns the index of the color nearest to a color in the palette.
    int
    getNearestColorIndex(int red, int green, int blue)
    Returns the index of the color nearest to a color in the palette.
    mixColor(int index, double scalar, int argb)
    Sets the color of a specific index in the Palette by blending it with another color.
    mixColor(int index, double scalar, int red, int green, int blue)
    Sets the color of a specific index in the Palette by blending it with another color.
    mixColor(int startIndex, int endIndex, double scalar, int argb)
    Sets the color of a range of indices in the Palette by blending it with another color.
    mixColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
    Sets the color of a range of indices in the Palette by blending it with another color.
    multiplyColor(int index, double scalar, int argb)
    Sets the color of a specific index in the Palette by multiplicatively blending it with another color.
    multiplyColor(int index, double scalar, int red, int green, int blue)
    Sets the color of a specific index in the Palette by multiplicatively blending it with another color.
    multiplyColor(int startIndex, int endIndex, double scalar, int argb)
    Sets the color of a range of indices in the Palette by multiplicatively blending it with another color.
    multiplyColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
    Sets the color of a range of indices in the Palette by multiplicatively blending it with another color.
    void
    Reads from an InputStream and sets this object's fields.
    saturateColor(int index, double scalar)
    Sets the color of a specific index in the Palette by saturating/desaturating it.
    saturateColor(int startIndex, int endIndex, double scalar)
    Sets the color of a range of indices in the Palette by saturating/desaturating it.
    setColor(int index, int argb)
    Sets the color of a specific index in the Palette.
    setColor(int startIndex, int endIndex, int argb)
    Sets the color of a range of indices in the Palette.
    setColor(int index, int red, int green, int blue)
    Sets the color of a specific index in the Palette.
    setColor(int startIndex, int endIndex, int red, int green, int blue)
    Sets the color of a range of indices in the Palette.
    setColorGradient(int startIndex, int endIndex, int argb0, int argb1)
    Sets the color of a range of indices in the Palette by creating a linear color gradient.
    setColorGradient(int startIndex, int endIndex, int red0, int green0, int blue0, int red1, int green1, int blue1)
    Sets the color of a range of indices in the Palette by creating a linear color gradient.
    protected void
    setColorNoSort(int index, int red, int green, int blue)
    Sets the color of a specific index in the Palette and doesn't trigger a re-sort.
    protected void
    Sort indexes into color.
    subtractColor(int index, double scalar, int argb)
    Sets the color of a specific index in the Palette by subtractively blending it with another color.
    subtractColor(int index, double scalar, int red, int green, int blue)
    Sets the color of a specific index in the Palette by subtractively blending it with another color.
    subtractColor(int startIndex, int endIndex, double scalar, int argb)
    Sets the color of a range of indices in the Palette by subtractively blending it with another color.
    subtractColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
    Sets the color of a range of indices in the Palette by subtractively blending it with another color.
     
    void
    Writes this object to an OutputStream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface net.mtrop.doom.object.BinaryObject

    fromBytes, readFile, toBytes, writeFile, writeFile
  • Field Details

    • NUM_COLORS

      public static final int NUM_COLORS
      The number of total colors in a standard Doom palette.
      See Also:
    • BYTES_PER_COLOR

      public static final int BYTES_PER_COLOR
      Number of bytes per color in a Doom palette.
      See Also:
    • LENGTH

      public static final int LENGTH
      A single palette's length in bytes.
      See Also:
    • colorPalette

      protected byte[][] colorPalette
      The palette of colors.
  • Constructor Details

    • Palette

      public Palette()
      Creates a new palette of black, opaque colors.
  • Method Details

    • copy

      public Palette copy()
      Makes a copy of this palette.
      Returns:
      a new Palette that is a copy of this one.
      Since:
      2.2.0
    • getColor

      public Color getColor(int index)
      Returns the Color of a specific index in the palette.
      Parameters:
      index - the index number of the color.
      Returns:
      the color as a java.awt.Color.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
    • getColorARGB

      public int getColorARGB(int index)
      Returns the Color of a specific index in the palette as a 32-bit ARGB integer. Alpha is always 255 (opaque).
      Parameters:
      index - the index number of the color.
      Returns:
      the color as an ARGB integer.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
    • getNearestColorIndex

      public int getNearestColorIndex(int argb)
      Returns the index of the color nearest to a color in the palette.
      Parameters:
      argb - the ARGB color.
      Returns:
      the closest index.
    • getNearestColorIndex

      public int getNearestColorIndex(int red, int green, int blue)
      Returns the index of the color nearest to a color in the palette.
      Parameters:
      red - the red component amount (0 to 255).
      green - the green component amount (0 to 255).
      blue - the blue component amount (0 to 255).
      Returns:
      the closest index.
    • setColor

      public Palette setColor(int index, int argb)
      Sets the color of a specific index in the Palette.
      Parameters:
      index - the index number of the color to change.
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0, this returns itself.
    • setColor

      public Palette setColor(int index, int red, int green, int blue)
      Sets the color of a specific index in the Palette.
      Parameters:
      index - the index number of the color to change.
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0, this returns itself.
    • mixColor

      public Palette mixColor(int index, double scalar, int argb)
      Sets the color of a specific index in the Palette by blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar intensity of the blend (0 to 1, 0 is none, 1 is replace).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • mixColor

      public Palette mixColor(int index, double scalar, int red, int green, int blue)
      Sets the color of a specific index in the Palette by blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar intensity of the blend (0 to 1, 0 is none, 1 is replace).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • addColor

      public Palette addColor(int index, double scalar, int argb)
      Sets the color of a specific index in the Palette by additively blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar amount of each component to add (0 to 1).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • addColor

      public Palette addColor(int index, double scalar, int red, int green, int blue)
      Sets the color of a specific index in the Palette by additively blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar amount of each component to add (0 to 1).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • subtractColor

      public Palette subtractColor(int startIndex, int endIndex, double scalar, int argb)
      Sets the color of a range of indices in the Palette by subtractively blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar amount of each component to subtract (0 to 1).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • subtractColor

      public Palette subtractColor(int index, double scalar, int red, int green, int blue)
      Sets the color of a specific index in the Palette by subtractively blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar amount of each component to subtract (0 to 1).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • multiplyColor

      public Palette multiplyColor(int index, double scalar, int argb)
      Sets the color of a specific index in the Palette by multiplicatively blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar intensity of the blend (0 to 1, 1 is full blend).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • multiplyColor

      public Palette multiplyColor(int index, double scalar, int red, int green, int blue)
      Sets the color of a specific index in the Palette by multiplicatively blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar intensity of the blend (0 to 1, 1 is full blend).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • setColor

      public Palette setColor(int startIndex, int endIndex, int argb)
      Sets the color of a range of indices in the Palette.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • setColor

      public Palette setColor(int startIndex, int endIndex, int red, int green, int blue)
      Sets the color of a range of indices in the Palette.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • mixColor

      public Palette mixColor(int startIndex, int endIndex, double scalar, int argb)
      Sets the color of a range of indices in the Palette by blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar intensity of the blend (0 to 1, 0 is none, 1 is replace).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • mixColor

      public Palette mixColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
      Sets the color of a range of indices in the Palette by blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar intensity of the blend (0 to 1, 0 is none, 1 is replace).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • addColor

      public Palette addColor(int startIndex, int endIndex, double scalar, int argb)
      Sets the color of a range of indices in the Palette by additively blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar amount of each component to add (0 to 1).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • addColor

      public Palette addColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
      Sets the color of a range of indices in the Palette by additively blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar amount of each component to add (0 to 1).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • subtractColor

      public Palette subtractColor(int index, double scalar, int argb)
      Sets the color of a specific index in the Palette by subtractively blending it with another color.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar amount of each component to subtract (0 to 1).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • subtractColor

      public Palette subtractColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
      Sets the color of a range of indices in the Palette by subtractively blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar amount of each component to subtract (0 to 1).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • multiplyColor

      public Palette multiplyColor(int startIndex, int endIndex, double scalar, int argb)
      Sets the color of a range of indices in the Palette by multiplicatively blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar intensity of the blend (0 to 1, 1 is full blend).
      argb - the ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • multiplyColor

      public Palette multiplyColor(int startIndex, int endIndex, double scalar, int red, int green, int blue)
      Sets the color of a range of indices in the Palette by multiplicatively blending it with another color.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar intensity of the blend (0 to 1, 1 is full blend).
      red - the red component amount (0 to 255, clamped).
      green - the green component amount (0 to 255, clamped).
      blue - the blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if startIndex or endIndex is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • saturateColor

      public Palette saturateColor(int index, double scalar)
      Sets the color of a specific index in the Palette by saturating/desaturating it.
      Parameters:
      index - the index number of the color to change.
      scalar - the scalar intensity of the final saturation (0 to 1, 0 is desaturated, 1 is no change, higher values saturate).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • saturateColor

      public Palette saturateColor(int startIndex, int endIndex, double scalar)
      Sets the color of a range of indices in the Palette by saturating/desaturating it.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      scalar - the scalar intensity of the final saturation (0 to 1, 0 is desaturated, 1 is no change, higher values saturate).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • setColorGradient

      public Palette setColorGradient(int startIndex, int endIndex, int argb0, int argb1)
      Sets the color of a range of indices in the Palette by creating a linear color gradient.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      argb0 - the first ARGB color.
      argb1 - the second ARGB color.
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • setColorGradient

      public Palette setColorGradient(int startIndex, int endIndex, int red0, int green0, int blue0, int red1, int green1, int blue1)
      Sets the color of a range of indices in the Palette by creating a linear color gradient.
      Parameters:
      startIndex - the starting index number of the color to change (inclusive).
      endIndex - the ending index number of the color to change (inclusive).
      red0 - the first color's red component amount (0 to 255, clamped).
      green0 - the first color's green component amount (0 to 255, clamped).
      blue0 - the first color's blue component amount (0 to 255, clamped).
      red1 - the second color's red component amount (0 to 255, clamped).
      green1 - the second color's green component amount (0 to 255, clamped).
      blue1 - the second color's blue component amount (0 to 255, clamped).
      Returns:
      itself, for chaining calls.
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
      Since:
      2.2.0
    • setColorNoSort

      protected void setColorNoSort(int index, int red, int green, int blue)
      Sets the color of a specific index in the Palette and doesn't trigger a re-sort.
      Parameters:
      index - the index number of the color.
      red - the red component amount (0 to 255).
      green - the green component amount (0 to 255).
      blue - the blue component amount (0 to 255).
      Throws:
      ArrayIndexOutOfBoundsException - if index is greater than or equal to NUM_COLORS or less than 0.
    • sortIndices

      protected void sortIndices()
      Sort indexes into color.
    • readBytes

      public void readBytes(InputStream in) throws IOException
      Description copied from interface: BinaryObject
      Reads from an InputStream and sets this object's fields. Only reads the amount of bytes that it takes to read a single instance of the object. Note that not every object may have a consistent length!
      Specified by:
      readBytes in interface BinaryObject
      Parameters:
      in - the InputStream to read from.
      Throws:
      IOException - if a read error occurs.
    • writeBytes

      public void writeBytes(OutputStream out) throws IOException
      Description copied from interface: BinaryObject
      Writes this object to an OutputStream.
      Specified by:
      writeBytes in interface BinaryObject
      Parameters:
      out - the OutputStream to write to.
      Throws:
      IOException - if a write error occurs.
    • toString

      public String toString()
      Overrides:
      toString in class Object