2019-01-01 01:10:18 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
2017-05-05 13:24:29 +00:00
|
|
|
package dan200.computercraft.shared.util;
|
|
|
|
|
2017-05-05 15:21:53 +00:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
2017-05-05 13:24:29 +00:00
|
|
|
public class Palette
|
|
|
|
{
|
|
|
|
private static final int PALETTE_SIZE = 16;
|
2017-05-28 12:08:15 +00:00
|
|
|
private final double[][] colours = new double[PALETTE_SIZE][3];
|
2017-05-05 13:24:29 +00:00
|
|
|
|
2017-05-07 10:51:06 +00:00
|
|
|
public static final Palette DEFAULT = new Palette();
|
|
|
|
|
2017-05-05 13:24:29 +00:00
|
|
|
public Palette()
|
|
|
|
{
|
|
|
|
// Get the default palette
|
|
|
|
resetColours();
|
|
|
|
}
|
|
|
|
|
2018-12-23 17:46:58 +00:00
|
|
|
public void setColour( int i, double r, double g, double b )
|
2017-05-05 13:24:29 +00:00
|
|
|
{
|
|
|
|
if( i >= 0 && i < colours.length )
|
|
|
|
{
|
2017-05-05 16:22:40 +00:00
|
|
|
colours[i][0] = r;
|
|
|
|
colours[i][1] = g;
|
|
|
|
colours[i][2] = b;
|
2017-05-05 13:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 17:46:58 +00:00
|
|
|
public void setColour( int i, Colour colour )
|
2017-05-05 13:24:29 +00:00
|
|
|
{
|
|
|
|
setColour( i, colour.getR(), colour.getG(), colour.getB() );
|
|
|
|
}
|
|
|
|
|
2017-05-28 12:08:15 +00:00
|
|
|
public double[] getColour( int i )
|
2017-05-05 13:24:29 +00:00
|
|
|
{
|
|
|
|
if( i >= 0 && i < colours.length )
|
|
|
|
{
|
2017-05-05 16:22:40 +00:00
|
|
|
return colours[i];
|
2017-05-05 13:24:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:52:21 +00:00
|
|
|
public void resetColour( int i )
|
|
|
|
{
|
2017-05-05 16:22:40 +00:00
|
|
|
if( i >= 0 && i < colours.length )
|
2017-05-05 13:24:29 +00:00
|
|
|
{
|
2017-05-05 16:22:40 +00:00
|
|
|
setColour( i, Colour.values()[i] );
|
2017-05-05 13:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-05 13:52:21 +00:00
|
|
|
|
|
|
|
public void resetColours()
|
|
|
|
{
|
2018-12-23 17:46:58 +00:00
|
|
|
for( int i = 0; i < Colour.values().length; i++ )
|
2017-05-05 13:52:21 +00:00
|
|
|
{
|
|
|
|
resetColour( i );
|
|
|
|
}
|
|
|
|
}
|
2017-05-05 15:21:53 +00:00
|
|
|
|
2017-05-28 12:08:15 +00:00
|
|
|
public static int encodeRGB8( double[] rgb )
|
2017-05-05 16:47:43 +00:00
|
|
|
{
|
2018-12-23 17:46:58 +00:00
|
|
|
int r = (int) (rgb[0] * 255) & 0xFF;
|
|
|
|
int g = (int) (rgb[1] * 255) & 0xFF;
|
|
|
|
int b = (int) (rgb[2] * 255) & 0xFF;
|
2017-05-05 16:47:43 +00:00
|
|
|
|
2018-12-23 17:46:58 +00:00
|
|
|
return (r << 16) | (g << 8) | b;
|
2017-05-05 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 12:08:15 +00:00
|
|
|
public static double[] decodeRGB8( int rgb )
|
2017-05-05 16:47:43 +00:00
|
|
|
{
|
2019-06-07 23:28:03 +00:00
|
|
|
return new double[] {
|
|
|
|
((rgb >> 16) & 0xFF) / 255.0f,
|
|
|
|
((rgb >> 8) & 0xFF) / 255.0f,
|
|
|
|
(rgb & 0xFF) / 255.0f,
|
|
|
|
};
|
2017-05-05 16:47:43 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 15:21:53 +00:00
|
|
|
public NBTTagCompound writeToNBT( NBTTagCompound nbt )
|
|
|
|
{
|
2017-05-05 16:47:43 +00:00
|
|
|
int[] rgb8 = new int[colours.length];
|
|
|
|
|
2018-12-23 17:46:58 +00:00
|
|
|
for( int i = 0; i < colours.length; i++ )
|
2017-05-05 15:21:53 +00:00
|
|
|
{
|
2017-05-05 16:47:43 +00:00
|
|
|
rgb8[i] = encodeRGB8( colours[i] );
|
2017-05-05 15:21:53 +00:00
|
|
|
}
|
2017-05-05 16:47:43 +00:00
|
|
|
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
nbt.putIntArray( "term_palette", rgb8 );
|
2017-05-05 15:21:53 +00:00
|
|
|
return nbt;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readFromNBT( NBTTagCompound nbt )
|
|
|
|
{
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
if( !nbt.contains( "term_palette" ) ) return;
|
2017-05-05 16:47:43 +00:00
|
|
|
int[] rgb8 = nbt.getIntArray( "term_palette" );
|
|
|
|
|
2017-05-19 10:47:27 +00:00
|
|
|
if( rgb8.length != colours.length ) return;
|
|
|
|
|
2018-12-23 17:46:58 +00:00
|
|
|
for( int i = 0; i < colours.length; i++ )
|
2017-05-05 15:21:53 +00:00
|
|
|
{
|
2017-05-05 16:47:43 +00:00
|
|
|
colours[i] = decodeRGB8( rgb8[i] );
|
2017-05-05 15:21:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-05 13:24:29 +00:00
|
|
|
}
|