Fixed issue #9 when creating a new drawing

This commit is contained in:
Leamsi Escribano 2020-09-14 10:48:15 -04:00
parent 62fc655fd2
commit 81eb4f4b66
3 changed files with 23 additions and 16 deletions

View File

@ -1,5 +1,5 @@
function createColorPalette(selectedPalette, fillBackground, deletePreviousPalette = true) { function createColorPalette(paletteColors, fillBackground, deletePreviousPalette = true) {
//remove current palette //remove current palette
if (deletePreviousPalette) { if (deletePreviousPalette) {
colors = document.getElementsByClassName('color-button'); colors = document.getElementsByClassName('color-button');
@ -11,8 +11,8 @@ function createColorPalette(selectedPalette, fillBackground, deletePreviousPalet
var lightestColor = '#000000'; var lightestColor = '#000000';
var darkestColor = '#ffffff'; var darkestColor = '#ffffff';
for (var i = 0; i < selectedPalette.length; i++) { for (var i = 0; i < paletteColors.length; i++) {
var newColor = selectedPalette[i]; var newColor = paletteColors[i];
var newColorElement = addColor(newColor); var newColorElement = addColor(newColor);
var newColorHex = hexToRgb(newColor); var newColorHex = hexToRgb(newColor);

View File

@ -73,29 +73,30 @@ function newPixel (width, height, editorMode, fileContent = null) {
colors[0].parentElement.remove(); 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 //add colors from selected palette
var selectedPalette = getText('palette-button'); var selectedPalette = getText('palette-button');
if (selectedPalette != 'Choose a palette...' && fileContent == null) { if (selectedPalette != 'Choose a palette...' && fileContent == null) {
//if this palette isnt the one specified in the url, then reset the url //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'); history.pushState(null, null, '/pixel-editor/app');
presets[selectedPalette].colors.push(defaultForegroundColor);
presets[selectedPalette].colors.push(defaultBackgroundColor);
//fill the palette with specified palette //fill the palette with specified palette
createColorPalette(palettes[selectedPalette].colors,true); createColorPalette(presets[selectedPalette].colors,true);
} }
else if (fileContent == null) { else if (fileContent == null) {
//this wasn't a specified palette, so reset the url //this wasn't a specified palette, so reset the url
history.pushState(null, null, '/pixel-editor/app'); 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 //add colors to palette
addColor(defaultForegroundColor).classList.add('selected'); addColor(defaultForegroundColor).classList.add('selected');
addColor(defaultBackgroundColor); addColor(defaultBackgroundColor);

View File

@ -3,17 +3,23 @@ var presets = {
'Gameboy Color': { 'Gameboy Color': {
width: 240, width: 240,
height: 203, height: 203,
palette: 'Gameboy Color' palette: 'Gameboy Color',
specified: false,
colors: []
}, },
'PICO-8': { 'PICO-8': {
width: 128, width: 128,
height: 128, height: 128,
palette: 'PICO-8', palette: 'PICO-8',
specified: false,
colors: []
}, },
'Commodore 64': { 'Commodore 64': {
width: 40, width: 40,
height: 80, height: 80,
palette: 'Commodore 64' palette: 'Commodore 64',
specified: false,
colors: []
} }
}; };