diff --git a/bun.lock b/bun.lock index dd91f62..e5d8397 100644 --- a/bun.lock +++ b/bun.lock @@ -5,7 +5,7 @@ "": { "name": "litecanvas", "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", @@ -77,7 +77,7 @@ "@esbuild/win32-x64": ["@esbuild/win32-x64@0.28.1", "", { "os": "win32", "cpu": "x64" }, "sha512-bm4Mowrv+GXMlpWX++EcXw/iLyd1o3+bJkC2DkWXYVvgZCqD/bSj9ctZeAMC3cIxgjRVR2Dufaiu4YPxr5gW1A=="], - "@happy-dom/global-registrator": ["@happy-dom/global-registrator@20.10.3", "", { "dependencies": { "@types/node": ">=20.0.0", "happy-dom": "^20.10.3" } }, "sha512-rw10ox5gAdKT5UScrrhLRE8y9t2xzvRx2lUNwbXlPogJixYGciElqywuLlcmX+Rgcif0sF2wWUwqUEob1BKZTA=="], + "@happy-dom/global-registrator": ["@happy-dom/global-registrator@20.10.4", "", { "dependencies": { "@types/node": ">=20.0.0", "happy-dom": "^20.10.4" } }, "sha512-d1iWnYgb20MuHYyXVcjkEdFkUy5+myq8RyLAFK9Sy4PVNy/cU5xrM5JM/MMMR3KDZQ6YNVj9puy96lcfEaKh8Q=="], "@isaacs/cliui": ["@isaacs/cliui@8.0.2", "", { "dependencies": { "string-width": "^5.1.2", "string-width-cjs": "npm:string-width@^4.2.0", "strip-ansi": "^7.0.1", "strip-ansi-cjs": "npm:strip-ansi@^6.0.1", "wrap-ansi": "^8.1.0", "wrap-ansi-cjs": "npm:wrap-ansi@^7.0.0" } }, ""], @@ -305,7 +305,7 @@ "gzip-size": ["gzip-size@7.0.0", "", { "dependencies": { "duplexer": "^0.1.2" } }, ""], - "happy-dom": ["happy-dom@20.10.3", "", { "dependencies": { "@types/node": ">=20.0.0", "@types/whatwg-mimetype": "^3.0.2", "@types/ws": "^8.18.1", "buffer-image-size": "^0.6.4", "entities": "^7.0.1", "whatwg-mimetype": "^3.0.0", "ws": "^8.21.0" } }, "sha512-Hjdiy8RziuCcn5z04QI/rlsNuQoG8P0xxjgvsSMpi89cvIXIOcucQtiHS1yHSShxoBcSCeYqAskINmTiy/mlfw=="], + "happy-dom": ["happy-dom@20.10.4", "", { "dependencies": { "@types/node": ">=20.0.0", "@types/whatwg-mimetype": "^3.0.2", "@types/ws": "^8.18.1", "buffer-image-size": "^0.6.4", "entities": "^7.0.1", "whatwg-mimetype": "^3.0.0", "ws": "^8.21.0" } }, "sha512-bKrsQnFNpcjZG0UPsH7UMN7Oyp3AB42LXk2GuiQmu7l4QFxH7lsw5T1eWEtE2+vbIFrTC45sbNSB2pkB8MTfKA=="], "has-flag": ["has-flag@4.0.0", "", {}, "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ=="], diff --git a/package.json b/package.json index d415087..bd7095b 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -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", diff --git a/src/index.js b/src/index.js index 183c09f..c64d492 100644 --- a/src/index.js +++ b/src/index.js @@ -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[]} */ @@ -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 @@ -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) }, /** @@ -1313,7 +1321,7 @@ export default function litecanvas(settings = {}) { _eventListeners, // 5 - _colorPalette, + _currentPalette, // 6 _defaultSound, @@ -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) { diff --git a/src/version.js b/src/version.js index 3110a65..777639b 100644 --- a/src/version.js +++ b/src/version.js @@ -1,2 +1,2 @@ // Generated by genversion. -export const version = '0.301.1' +export const version = '0.302.0' diff --git a/tests/pal.js b/tests/pal.js index 5830e80..9843e7d 100644 --- a/tests/pal.js +++ b/tests/pal.js @@ -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) @@ -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]) }) })