From 81eb4f4b66e46a04767616b80957f90fb7e84641 Mon Sep 17 00:00:00 2001 From: Leamsi Escribano Date: Mon, 14 Sep 2020 10:48:15 -0400 Subject: [PATCH] Fixed issue #9 when creating a new drawing --- js/_createColorPalette.js | 6 +++--- js/_newPixel.js | 23 ++++++++++++----------- js/_presets.js | 10 ++++++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/js/_createColorPalette.js b/js/_createColorPalette.js index da51707..8f7352b 100644 --- a/js/_createColorPalette.js +++ b/js/_createColorPalette.js @@ -1,5 +1,5 @@ -function createColorPalette(selectedPalette, fillBackground, deletePreviousPalette = true) { +function createColorPalette(paletteColors, fillBackground, deletePreviousPalette = true) { //remove current palette if (deletePreviousPalette) { colors = document.getElementsByClassName('color-button'); @@ -11,8 +11,8 @@ function createColorPalette(selectedPalette, fillBackground, deletePreviousPalet var lightestColor = '#000000'; var darkestColor = '#ffffff'; - for (var i = 0; i < selectedPalette.length; i++) { - var newColor = selectedPalette[i]; + for (var i = 0; i < paletteColors.length; i++) { + var newColor = paletteColors[i]; var newColorElement = addColor(newColor); var newColorHex = hexToRgb(newColor); diff --git a/js/_newPixel.js b/js/_newPixel.js index 1473738..595ab77 100644 --- a/js/_newPixel.js +++ b/js/_newPixel.js @@ -73,29 +73,30 @@ function newPixel (width, height, editorMode, fileContent = null) { colors[0].parentElement.remove(); } + // set the default color variables + let fg = hslToRgb(Math.floor(Math.random()*255), 230,70); + let bg = hslToRgb(Math.floor(Math.random()*255), 230,170); + + let defaultForegroundColor = rgbToHex(fg.r,fg.g,fg.b); + let defaultBackgroundColor = rgbToHex(bg.r,bg.g,bg.b); + //add colors from selected palette var selectedPalette = getText('palette-button'); if (selectedPalette != 'Choose a palette...' && fileContent == null) { - //if this palette isnt the one specified in the url, then reset the url - if (!palettes[selectedPalette].specified) + if (!presets[selectedPalette].specified) history.pushState(null, null, '/pixel-editor/app'); + presets[selectedPalette].colors.push(defaultForegroundColor); + presets[selectedPalette].colors.push(defaultBackgroundColor); + //fill the palette with specified palette - createColorPalette(palettes[selectedPalette].colors,true); + createColorPalette(presets[selectedPalette].colors,true); } else if (fileContent == null) { //this wasn't a specified palette, so reset the url history.pushState(null, null, '/pixel-editor/app'); - //generate default colors - var fg = hslToRgb(Math.floor(Math.random()*255), 230,70); - var bg = hslToRgb(Math.floor(Math.random()*255), 230,170); - - //convert colors to hex - var defaultForegroundColor = rgbToHex(fg.r,fg.g,fg.b); - var defaultBackgroundColor = rgbToHex(bg.r,bg.g,bg.b); - //add colors to palette addColor(defaultForegroundColor).classList.add('selected'); addColor(defaultBackgroundColor); diff --git a/js/_presets.js b/js/_presets.js index bc4e02d..c36e563 100644 --- a/js/_presets.js +++ b/js/_presets.js @@ -3,17 +3,23 @@ var presets = { 'Gameboy Color': { width: 240, height: 203, - palette: 'Gameboy Color' + palette: 'Gameboy Color', + specified: false, + colors: [] }, 'PICO-8': { width: 128, height: 128, palette: 'PICO-8', + specified: false, + colors: [] }, 'Commodore 64': { width: 40, height: 80, - palette: 'Commodore 64' + palette: 'Commodore 64', + specified: false, + colors: [] } };