1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-10-23 18:07:39 +00:00

Use ARGB32 to store palette colours

Previously we used an RGBA byte array. However, this comes with some
overhead (extra memory reads, bounds checks).

Minecraft 1.21+ uses ARGB32 colours for rendering (well, in the public
code — internaly it converts to ABGR), so it makes sense to match that
here.

We also add some helper functions for dealing with ARGB32 colours. These
can be removed in 1.21, as Minecraft will have these builtin.
This commit is contained in:
Jonathan Coates
2024-09-11 09:00:06 +01:00
parent 370e5f92a0
commit ba36c69583
8 changed files with 94 additions and 78 deletions

View File

@@ -12,15 +12,13 @@ public class Palette {
private final boolean colour;
private final double[][] colours = new double[PALETTE_SIZE][3];
private final byte[][] byteColours = new byte[PALETTE_SIZE][4];
private final int[] byteColours = new int[PALETTE_SIZE];
public static final Palette DEFAULT = new Palette(true);
public Palette(boolean colour) {
this.colour = colour;
resetColours();
for (var i = 0; i < PALETTE_SIZE; i++) byteColours[i][3] = (byte) 255;
}
public void setColour(int i, double r, double g, double b) {
@@ -30,15 +28,17 @@ public class Palette {
colours[i][2] = b;
if (colour) {
byteColours[i][0] = (byte) (int) (r * 255);
byteColours[i][1] = (byte) (int) (g * 255);
byteColours[i][2] = (byte) (int) (b * 255);
byteColours[i] = packColour((int) (r * 255), (int) (g * 255), (int) (b * 255));
} else {
var grey = (byte) (int) ((r + g + b) / 3 * 255);
byteColours[i][0] = byteColours[i][1] = byteColours[i][2] = grey;
var grey = (int) ((r + g + b) / 3 * 255);
byteColours[i] = packColour(grey, grey, grey);
}
}
private static int packColour(int r, int g, int b) {
return 255 << 24 | (r & 255) << 16 | (g & 255) << 8 | b & 255;
}
public void setColour(int i, Colour colour) {
setColour(i, colour.getR(), colour.getG(), colour.getB());
}
@@ -48,26 +48,20 @@ public class Palette {
}
/**
* Get the colour as a set of RGB values suitable for rendering. Colours are automatically converted to greyscale
* Get the colour as a set of ARGB values suitable for rendering. Colours are automatically converted to greyscale
* when using a black and white palette.
* <p>
* This returns a byte array, suitable for being used directly by our terminal vertex format.
* This returns a packed 32-bit ARGB colour.
*
* @param i The colour index.
* @return The number as a tuple of bytes.
* @return The actual RGB colour.
*/
public byte[] getRenderColours(int i) {
public int getRenderColours(int i) {
return byteColours[i];
}
public void resetColour(int i) {
if (i >= 0 && i < PALETTE_SIZE) setColour(i, Colour.VALUES[i]);
}
public void resetColours() {
for (var i = 0; i < Colour.VALUES.length; i++) {
resetColour(i);
}
for (var i = 0; i < Colour.VALUES.length; i++) setColour(i, Colour.VALUES[i]);
}
public static int encodeRGB8(double[] rgb) {

View File

@@ -4,8 +4,6 @@
package dan200.computercraft.core.util;
import javax.annotation.Nullable;
public enum Colour {
BLACK(0x111111),
RED(0xcc4c4c),
@@ -30,15 +28,6 @@ public enum Colour {
return Colour.VALUES[colour];
}
@Nullable
public static Colour fromHex(int colour) {
for (var entry : VALUES) {
if (entry.getHex() == colour) return entry;
}
return null;
}
private final int hex;
private final float red, green, blue;