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

@ -132,20 +132,33 @@ public static void setColour( Terminal terminal, HashMap<Object, Object> colours
try
{
@SuppressWarnings({ "unchecked" }) // There isn't really a nice way around this :(
HashMap<Object, Object> colour = (HashMap<Object, Object>) e.getValue();
if (e.getValue() instanceof HashMap)
{
@SuppressWarnings({ "unchecked" }) // There isn't really a nice way around this :(
HashMap<Object, Object> colour = (HashMap<Object, Object>) e.getValue();
setColour(
terminal,
index,
( (Double)colour.get( 1.0 ) ).floatValue(),
( (Double)colour.get( 2.0 ) ).floatValue(),
( (Double)colour.get( 3.0 ) ).floatValue()
);
setColour(
terminal,
index,
( (Double)colour.get( 1.0 ) ).floatValue(),
( (Double)colour.get( 2.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)
{
throw new LuaException( "Malformed colour table" );
throw new LuaException( "Malformed colour table " + cce.getMessage() );
}
}
}
@ -335,21 +348,29 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
@SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :(
HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0];
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() );
float r = ((Double)args[1]).floatValue();
float g = ((Double)args[2]).floatValue();
float b = ((Double)args[3]).floatValue();
setColour( m_terminal, colour, r, g, b );
}
else
{
throw new LuaException( "Expected table or number, number, number, number" );
return null;
}
return null;
throw new LuaException( "Expected table or number, number or number, number, number, number" );
}
case 21:
case 22:

View File

@ -243,21 +243,29 @@ public Object[] callMethod( IComputerAccess computer, ILuaContext context, int m
@SuppressWarnings( { "unchecked" } ) // There isn't really a nice way around this :(
HashMap<Object, Object> colourTbl = (HashMap<Object, Object>)args[0];
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 );
float r = ((Double)args[1]).floatValue();
float g = ((Double)args[2]).floatValue();
float b = ((Double)args[3]).floatValue();
dan200.computercraft.core.apis.TermAPI.setColour( terminal, colour, r, g, b );
}
else
{
throw new LuaException( "Expected table or number, number, number, number" );
return null;
}
return null;
throw new LuaException( "Expected table or number, number, number, number" );
}
case 22:
case 23:

View File

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

View File

@ -72,7 +72,7 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
for i=0,15 do
local c = 2 ^ i
tPalette[c] = table.pack( parent.getColour( c ) )
tPalette[c] = { parent.getColour( c ) }
end
end
@ -291,7 +291,9 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
for k,v in pairs(colour) do
tPalette[k] = v
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 ]
tCol[1] = r
tCol[2] = g