2023-02-15 21:49:36 +00:00
-- Copyright (C) 2019-2023 Igara Studio S.A.
2018-08-22 17:56:07 +00:00
-- Copyright (C) 2018 David Capello
--
-- This file is released under the terms of the MIT license.
-- Read LICENSE.txt for more information.
2018-09-11 21:46:35 +00:00
do
local a = Sprite ( 32 , 64 )
assert ( a.width == 32 )
assert ( a.height == 64 )
assert ( a.colorMode == ColorMode.RGB ) -- RGB by default
2018-11-14 19:53:11 +00:00
assert ( a.bounds == Rectangle { x = 0 , y = 0 , width = 32 , height = 64 } )
2018-09-11 21:46:35 +00:00
-- Crop and resize
a.selection : select ( 2 , 3 , 4 , 5 )
a : crop ( )
assert ( a.width == 4 )
assert ( a.height == 5 )
2019-08-13 21:26:15 +00:00
assert ( a.cels [ 1 ] . image.width == 32 )
assert ( a.cels [ 1 ] . image.height == 64 )
2018-09-11 21:46:35 +00:00
a : resize ( 6 , 8 )
assert ( a.width == 6 )
assert ( a.height == 8 )
2024-06-03 14:29:15 +00:00
assert ( a.cels [ 1 ] . image.width == math.floor ( 32 * 6 / 4 ) ) -- Check that the image was resized (not only the canvas)
assert ( a.cels [ 1 ] . image.height == math.floor ( 64 * 8 / 5 ) )
2018-09-11 21:46:35 +00:00
a : crop { x =- 1 , y =- 1 , width = 20 , height = 30 }
assert ( a.width == 20 )
assert ( a.height == 30 )
2019-08-13 21:26:15 +00:00
-- Resize sprite setting width/height (just changing canvas size)
2018-09-11 21:46:35 +00:00
a.width = 8
a.height = 10
assert ( a.width == 8 )
assert ( a.height == 10 )
2019-04-18 03:16:54 +00:00
-- Test other Sprite() constructors
local b = Sprite ( 4 , 8 , ColorMode.INDEXED )
2018-09-11 21:46:35 +00:00
assert ( b.width == 4 )
2019-04-18 03:16:54 +00:00
assert ( b.height == 8 )
2018-09-11 21:46:35 +00:00
assert ( b.colorMode == ColorMode.INDEXED )
2019-04-18 03:16:54 +00:00
local c = Sprite { colorMode = ColorMode.INDEXED , width = 10 , height = 20 }
assert ( c.width == 10 )
assert ( c.height == 20 )
assert ( c.colorMode == ColorMode.INDEXED )
local d = Sprite { fromFile = " sprites/abcd.aseprite " }
assert ( # d.layers == 4 )
assert ( d.width == 32 )
assert ( d.height == 32 )
assert ( d.colorMode == ColorMode.INDEXED )
2018-09-11 21:46:35 +00:00
end
-- Transparent color
do
local a = Sprite ( 32 , 32 , ColorMode.INDEXED )
assert ( a.transparentColor == 0 )
a.transparentColor = 8
assert ( a.transparentColor == 8 )
end
-- Palette
do
local a = Sprite ( 32 , 32 , ColorMode.INDEXED )
assert ( # a.palettes == 1 )
assert ( # a.palettes [ 1 ] == 256 )
local p = Palette ( 3 )
p : setColor ( 0 , Color ( 255 , 0 , 0 ) )
p : setColor ( 1 , Color ( 0 , 255 , 0 ) )
p : setColor ( 2 , Color ( 0 , 0 , 255 ) )
a : setPalette ( p )
assert ( # a.palettes == 1 )
assert ( # a.palettes [ 1 ] == 3 )
assert ( a.palettes [ 1 ] : getColor ( 0 ) == Color ( 255 , 0 , 0 ) )
assert ( a.palettes [ 1 ] : getColor ( 1 ) == Color ( 0 , 255 , 0 ) )
assert ( a.palettes [ 1 ] : getColor ( 2 ) == Color ( 0 , 0 , 255 ) )
end
2019-04-17 18:04:25 +00:00
2019-04-18 03:16:54 +00:00
-- Duplicate & Flatten
2019-04-17 18:04:25 +00:00
do
local a = Sprite ( 32 , 32 )
a : newLayer ( )
a : newLayer ( )
assert ( # a.layers == 3 )
2019-04-18 03:16:54 +00:00
local b = Sprite ( a ) -- Clone a
a : flatten ( ) -- Flatten a
2019-04-17 18:04:25 +00:00
assert ( # a.layers == 1 )
2019-04-18 03:16:54 +00:00
assert ( # b.layers == 3 )
2019-04-17 18:04:25 +00:00
end
2019-09-11 22:19:10 +00:00
-- Resize non-active sprite
do
local a = Sprite ( 32 , 32 )
local b = Sprite ( 64 , 64 )
app.activeSprite = a
a : resize ( 10 , 10 )
b : resize ( 20 , 20 )
assert ( a.width == 10 )
assert ( a.height == 10 )
assert ( b.width == 20 )
assert ( b.height == 20 )
app.undo ( )
assert ( a.width == 32 )
assert ( a.height == 32 )
app.activeSprite = b
app.undo ( )
assert ( b.width == 64 )
assert ( b.height == 64 )
end
2019-11-01 18:06:11 +00:00
-- Grid bounds
do
local s = Sprite ( 32 , 32 )
assert ( s.gridBounds == Rectangle { 0 , 0 , 16 , 16 } )
s.gridBounds = Rectangle { 2 , 3 , 8 , 4 }
assert ( s.gridBounds == Rectangle { 2 , 3 , 8 , 4 } )
2021-04-09 19:19:58 +00:00
s : saveAs ( " _test_sprite_gridbounds.aseprite " )
2019-11-01 18:06:11 +00:00
2021-04-09 19:19:58 +00:00
local s2 = Sprite { fromFile = " _test_sprite_gridbounds.aseprite " }
assert ( s2.gridBounds == Rectangle { 2 , 3 , 8 , 4 } )
end
-- Pixel ratio
do
local s = Sprite ( 32 , 32 )
assert ( s.pixelRatio == Size { 1 , 1 } )
s.pixelRatio = Size { 3 , 2 }
assert ( s.pixelRatio == Size { 3 , 2 } )
s : saveAs ( " _test_sprite_pixelratio.aseprite " )
local s2 = Sprite { fromFile = " _test_sprite_pixelratio.aseprite " }
assert ( s2.pixelRatio == Size { 3 , 2 } )
2019-11-01 18:06:11 +00:00
end
2020-07-28 20:44:43 +00:00
-- Sprite{ fromFile, oneFrame }
do
local s = Sprite ( 32 , 32 )
s : newFrame ( )
s : saveAs ( " _test1.png " )
assert ( # s.frames == 2 )
s = Sprite { fromFile = " _test1.png " }
assert ( # s.frames == 2 )
s = Sprite { fromFile = " _test1.png " , oneFrame = true }
assert ( # s.frames == 1 )
s = Sprite { fromFile = " _test1.png " , oneFrame = false }
assert ( # s.frames == 2 )
end
2021-09-02 20:48:58 +00:00
-- Issues with sprites having pixel with indexes out of palette bounds:
-- Saving png failed (https://github.com/aseprite/aseprite/issues/2842)
do
local s = Sprite ( 2 , 2 , ColorMode.INDEXED )
assert ( # s.palettes == 1 )
s.transparentColor = 1
s.palettes [ 1 ] : resize ( 8 )
assert ( # s.palettes [ 1 ] == 8 )
s.cels [ 1 ] . image : clear ( 2 )
s.cels [ 1 ] . image : putPixel ( 0 , 0 , 7 )
s : saveAs ( " _test1_.png " ) -- OK
s.palettes [ 1 ] : resize ( 4 )
assert ( # s.palettes [ 1 ] == 4 )
local result = s : saveAs ( " _test2_palerr_.png " ) -- Used to fail
-- If result=false we got a "libpng: Wrote palette index exceeding num_palette"
assert ( result )
local s2 = app.open ( " _test2_palerr_.png " )
assert ( s2 ~= nil )
print ( s2.cels [ 1 ] . image : getPixel ( 0 , 0 ) )
assert ( s2.cels [ 1 ] . image : getPixel ( 0 , 0 ) == 1 )
end
-- Flatten visible layers uses indices outside the palette range as opaque colors instead of transparent color (https://github.com/aseprite/aseprite/issues/2912)
do
local s = Sprite ( 2 , 2 , ColorMode.INDEXED )
s.transparentColor = 0
s.palettes [ 1 ] : resize ( 4 )
s.cels [ 1 ] . image : clear ( 1 )
local l = s : newLayer ( )
s : newCel ( l )
s.cels [ 2 ] . image : putPixel ( 0 , 0 , 2 )
s.cels [ 2 ] . image : putPixel ( 1 , 0 , 7 )
s : flatten ( )
assert ( s.cels [ 1 ] . image : getPixel ( 0 , 0 ) == 2 )
assert ( s.cels [ 1 ] . image : getPixel ( 1 , 0 ) == 1 ) -- Get the color of the first layer
end
2022-05-23 20:23:16 +00:00
-- Compare sprite IDs
do
local a = Sprite ( 1 , 1 )
local b = Sprite ( 1 , 1 )
assert ( a == a )
assert ( a ~= b ) -- Compares IDs, not sprite size
end
2023-02-15 21:49:36 +00:00
-- Tile management plugin
do
local fn = " _test_sprite_tileManagementPlugin.aseprite "
local a = Sprite ( 1 , 1 )
assert ( a.tileManagementPlugin == nil )
a.tileManagementPlugin = " test "
app.undo ( )
assert ( a.tileManagementPlugin == nil )
app.redo ( )
assert ( a.tileManagementPlugin == " test " )
a : saveAs ( fn )
b = app.open ( fn )
assert ( b.tileManagementPlugin == " test " )
b.tileManagementPlugin = nil
b : saveAs ( fn )
c = app.open ( fn )
assert ( c.tileManagementPlugin == nil )
end