1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-06-04 23:54:10 +00:00

Allow passing RGB8 hex values to term.setColour

This commit is contained in:
Lignum 2017-05-07 01:16:48 +02:00
parent 1cc403191f
commit 287c6f172c
No known key found for this signature in database
GPG Key ID: 0889206F5A8A700D
4 changed files with 57 additions and 26 deletions

View File

@ -131,6 +131,8 @@ public class TermAPI implements ILuaAPI
int index = 15 - (int)( Math.log( (Double)e.getKey() ) / lg2 ); int index = 15 - (int)( Math.log( (Double)e.getKey() ) / lg2 );
try try
{
if (e.getValue() instanceof HashMap)
{ {
@SuppressWarnings({ "unchecked" }) // There isn't really a nice way around this :( @SuppressWarnings({ "unchecked" }) // There isn't really a nice way around this :(
HashMap<Object, Object> colour = (HashMap<Object, Object>) e.getValue(); HashMap<Object, Object> colour = (HashMap<Object, Object>) e.getValue();
@ -143,9 +145,20 @@ public class TermAPI implements ILuaAPI
( (Double)colour.get( 3.0 ) ).floatValue() ( (Double)colour.get( 3.0 ) ).floatValue()
); );
} }
else if (e.getValue() instanceof Double)
{
float[] rgb = Palette.decodeRGB8( ((Double)e.getValue()).intValue() );
setColour(
terminal,
index,
rgb[0], rgb[1], rgb[2]
);
}
}
catch(ClassCastException cce) catch(ClassCastException cce)
{ {
throw new LuaException( "Malformed colour table" ); throw new LuaException( "Malformed colour table " + cce.getMessage() );
} }
} }
} }
@ -335,21 +348,29 @@ public class TermAPI implements ILuaAPI
@SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :( @SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :(
HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0]; HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0];
setColour( m_terminal, colourTbl ); setColour( m_terminal, colourTbl );
return null;
} }
else if (args.length >= 4 && args[0] instanceof Double && args[1] instanceof Double && args[2] instanceof Double && args[3] instanceof Double)
if(args.length == 2 && args[0] instanceof Double && args[1] instanceof Double)
{
int colour = 15 - parseColour( args, m_environment.isColour() );
int hex = ((Double)args[1]).intValue();
float[] rgb = Palette.decodeRGB8( hex );
setColour( m_terminal, colour, rgb[0], rgb[1], rgb[2] );
return null;
}
if(args.length >= 4 && args[0] instanceof Double && args[1] instanceof Double && args[2] instanceof Double && args[3] instanceof Double)
{ {
int colour = 15 - parseColour( args, m_environment.isColour() ); int colour = 15 - parseColour( args, m_environment.isColour() );
float r = ((Double)args[1]).floatValue(); float r = ((Double)args[1]).floatValue();
float g = ((Double)args[2]).floatValue(); float g = ((Double)args[2]).floatValue();
float b = ((Double)args[3]).floatValue(); float b = ((Double)args[3]).floatValue();
setColour( m_terminal, colour, r, g, b ); setColour( m_terminal, colour, r, g, b );
} return null;
else
{
throw new LuaException( "Expected table or number, number, number, number" );
} }
return null; throw new LuaException( "Expected table or number, number or number, number, number, number" );
} }
case 21: case 21:
case 22: case 22:

View File

@ -243,21 +243,29 @@ public class MonitorPeripheral implements IPeripheral
@SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :( @SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :(
HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0]; HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0];
dan200.computercraft.core.apis.TermAPI.setColour( terminal, colourTbl ); dan200.computercraft.core.apis.TermAPI.setColour( terminal, colourTbl );
return null;
} }
else if (args.length >= 4 && args[0] instanceof Double && args[1] instanceof Double && args[2] instanceof Double && args[3] instanceof Double)
if(args.length == 2 && args[0] instanceof Double && args[1] instanceof Double)
{
int colour = 15 - dan200.computercraft.core.apis.TermAPI.parseColour( args, true );
int hex = ((Double)args[1]).intValue();
float[] rgb = Palette.decodeRGB8( hex );
dan200.computercraft.core.apis.TermAPI.setColour( terminal, colour, rgb[0], rgb[1], rgb[2] );
return null;
}
if (args.length >= 4 && args[0] instanceof Double && args[1] instanceof Double && args[2] instanceof Double && args[3] instanceof Double)
{ {
int colour = 15 - dan200.computercraft.core.apis.TermAPI.parseColour( args, true ); int colour = 15 - dan200.computercraft.core.apis.TermAPI.parseColour( args, true );
float r = ((Double)args[1]).floatValue(); float r = ((Double)args[1]).floatValue();
float g = ((Double)args[2]).floatValue(); float g = ((Double)args[2]).floatValue();
float b = ((Double)args[3]).floatValue(); float b = ((Double)args[3]).floatValue();
dan200.computercraft.core.apis.TermAPI.setColour( terminal, colour, r, g, b ); dan200.computercraft.core.apis.TermAPI.setColour( terminal, colour, r, g, b );
} return null;
else
{
throw new LuaException( "Expected table or number, number, number, number" );
} }
return null; throw new LuaException( "Expected table or number, number, number, number" );
} }
case 22: case 22:
case 23: case 23:

View File

@ -53,7 +53,7 @@ public class Palette
} }
} }
private static int encodeRGB8( float[] rgb ) public static int encodeRGB8( float[] rgb )
{ {
int r = (int)( rgb[0] * 255 ) & 0xFF; int r = (int)( rgb[0] * 255 ) & 0xFF;
int g = (int)( rgb[1] * 255 ) & 0xFF; int g = (int)( rgb[1] * 255 ) & 0xFF;
@ -62,7 +62,7 @@ public class Palette
return ( r << 16 ) | ( g << 8 ) | b; return ( r << 16 ) | ( g << 8 ) | b;
} }
private static float[] decodeRGB8( int rgb ) public static float[] decodeRGB8( int rgb )
{ {
return new float[] return new float[]
{ {

View File

@ -72,7 +72,7 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
for i=0,15 do for i=0,15 do
local c = 2 ^ i local c = 2 ^ i
tPalette[c] = table.pack( parent.getColour( c ) ) tPalette[c] = { parent.getColour( c ) }
end end
end end
@ -291,7 +291,9 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
for k,v in pairs(colour) do for k,v in pairs(colour) do
tPalette[k] = v tPalette[k] = v
end end
else elseif type(colour) == "number" and type(r) == "number" and g == nil and b == nil then
tPalette[ colour ] = { colours.rgb8( r ) }
elseif type(colour) == "number" and type(r) == "number" and type(g) == "number" and type(b) == "number" then
local tCol = tPalette[ colour ] local tCol = tPalette[ colour ]
tCol[1] = r tCol[1] = r
tCol[2] = g tCol[2] = g