Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "litecanvas",
"version": "0.301.1",
"version": "0.302.0",
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
"license": "MIT",
"author": "Luiz Bills <luizbills@pm.me>",
Expand Down Expand Up @@ -32,7 +32,7 @@
"creative coding"
],
"devDependencies": {
"@happy-dom/global-registrator": "^20.10.3",
"@happy-dom/global-registrator": "^20.10.4",
"@size-limit/preset-small-lib": "^12.1.0",
"@swc/core": "^1.15.41",
"ava": "^8.0.1",
Expand Down
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export default function litecanvas(settings = {}) {
/** @type {number} */
_rngSeed = Date.now(),
/** @type {string[]} */
_colorPalette = defaultPalette,
_currentPalette = defaultPalette,
/** @type {string[]} */
_lastPalette = defaultPalette,
/** @type {number[]} */
_colorPaletteState = [],
/** @type {number[]} */
Expand Down Expand Up @@ -1183,7 +1185,7 @@ export default function litecanvas(settings = {}) {
},

/**
* Set new palette colors or restore the default palette.
* Set new palette colors or restore previous palette.
*
* @param {string[]} [colors] an array of colors
* @param {number} [textColor] the default text color this palette
Expand All @@ -1198,11 +1200,17 @@ export default function litecanvas(settings = {}) {
'pal() 2nd argument must be a non-negative number'
)

_colorPalette = colors || defaultPalette
if (null == colors) {
_currentPalette = _lastPalette
} else {
_lastPalette = _currentPalette
_currentPalette = colors
}

_colorPaletteState = []
_defaultTextColor = textColor

instance.emit('pal', _colorPalette, _defaultTextColor)
instance.emit('pal', _currentPalette, _defaultTextColor)
},

/**
Expand Down Expand Up @@ -1313,7 +1321,7 @@ export default function litecanvas(settings = {}) {
_eventListeners,

// 5
_colorPalette,
_currentPalette,

// 6
_defaultSound,
Expand Down Expand Up @@ -1864,7 +1872,7 @@ export default function litecanvas(settings = {}) {
*/
function getColor(index) {
const i = _colorPaletteState[index] ?? index
return _colorPalette[~~i % _colorPalette.length]
return _currentPalette[~~i % _currentPalette.length]
}

if (settings.global) {
Expand Down
2 changes: 1 addition & 1 deletion src/version.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions tests/pal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.after(() => {
local.quit()
})

test('change the color palette', async (t) => {
test('change the current color palette', async (t) => {
await onLitecanvas(local, 'draw', () => {
const customPalette = ['#123', '#456']
local.pal(customPalette)
Expand All @@ -33,12 +33,20 @@ test('change the color palette', async (t) => {
})
})

test('reset the color palette', async (t) => {
test('restore the previous color palette', async (t) => {
await onLitecanvas(local, 'draw', () => {
local.pal()
const _firstPalette = ['#114', '#fee']
local.pal(_firstPalette)

const colors = local.stat(5)
const _secondPalette = ['#213', '#abc']
local.pal(_secondPalette)

const _currentPalette = local.stat(5)
t.not(_currentPalette[0], _firstPalette[0])

local.pal()
const _restoredPalette = local.stat(5)

t.is(colors[0], defaultPalette[0])
t.is(_restoredPalette[0], _firstPalette[0])
})
})
Loading