Package net.mtrop.doom.graphics
Class Palette
java.lang.Object
net.mtrop.doom.graphics.Palette
- All Implemented Interfaces:
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
Modifier and TypeFieldDescriptionstatic 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
-
Method Summary
Modifier and TypeMethodDescriptionaddColor
(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.copy()
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
getNearestColorIndex
(int argb) Returns the index of the color nearest to a color in the palette.int
getNearestColorIndex
(int argb, boolean exclude255) 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.int
getNearestColorIndex
(int red, int green, int blue, boolean exclude255) 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
readBytes
(InputStream in) Reads from anInputStream
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.toString()
void
writeBytes
(OutputStream out) Writes this object to anOutputStream
.
-
Field Details
-
NUM_COLORS
public static final int NUM_COLORSThe number of total colors in a standard Doom palette.- See Also:
-
BYTES_PER_COLOR
public static final int BYTES_PER_COLORNumber of bytes per color in a Doom palette.- See Also:
-
LENGTH
public static final int LENGTHA single palette's length in bytes.- See Also:
-
colorPalette
protected byte[][] colorPaletteThe palette of colors.
-
-
Constructor Details
-
Palette
public Palette()Creates a new palette of black, opaque colors.
-
-
Method Details
-
copy
Makes a copy of this palette.- Returns:
- a new Palette that is a copy of this one.
- Since:
- 2.2.0
-
getColor
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 argb, boolean exclude255) Returns the index of the color nearest to a color in the palette.- Parameters:
argb
- the ARGB color.exclude255
- if true, exclude the 255th color in the palette as a candidate (for patches).- Returns:
- the closest index.
- Since:
- 2.16.0
-
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.
-
getNearestColorIndex
public int getNearestColorIndex(int red, int green, int blue, boolean exclude255) 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).exclude255
- if true, exclude the 255th color in the palette as a candidate (for patches).- Returns:
- the closest index.
- Since:
- 2.16.0
-
setColor
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Description copied from interface:BinaryObject
Reads from anInputStream
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 interfaceBinaryObject
- Parameters:
in
- theInputStream
to read from.- Throws:
IOException
- if a read error occurs.
-
writeBytes
Description copied from interface:BinaryObject
Writes this object to anOutputStream
.- Specified by:
writeBytes
in interfaceBinaryObject
- Parameters:
out
- theOutputStream
to write to.- Throws:
IOException
- if a write error occurs.
-
toString
-