/** Adds the given color to the palette * * @param {*} newColor the colour to add * @return the list item containing the added colour */ const ColorModule = (() => { const currentPalette = []; const coloursList = document.getElementById("palette-list"); console.info("Initialized Color Module.."); document.getElementById('jscolor-hex-input').addEventListener('change',colorChanged, false); document.getElementById('jscolor-hex-input').addEventListener('input', colorChanged, false); document.getElementById('add-color-button').addEventListener('click', addColorButtonEvent, false); new Sortable(document.getElementById("colors-menu"), { animation:100, filter: ".noshrink", draggable: ".draggable-colour", onEnd: makeIsDraggingFalse }); // Changes all of one color to another after being changed from color picker function colorChanged(colorHexElement) { // Get old and new colors from the element const hexElement = colorHexElement.target; const hexElementValue = hexElement.value; const newColor = hexToRgb(hexElementValue); const oldColor = hexElement.oldColor; //if the color is not a valid hex color, exit this function and do nothing const newColorHex = hexElementValue.toLowerCase(); if (/^[0-9a-f]{6}$/i.test(newColorHex) == false) return currentPalette.splice(currentPalette.indexOf("#" + newColor), 1); newColor.a = 255; //save undo state new HistoryStateEditColor(hexElementValue.toLowerCase(), rgbToHex(oldColor)); //get the currently selected color const currentlyEditedColor = document.getElementsByClassName('jscolor-active')[0]; const duplicateColorWarning = document.getElementById('duplicate-color-warning'); //check if selected color already matches another color colors = document.getElementsByClassName('color-button'); //loop through all colors in palette for (var i = 0; i < colors.length; i++) { //if generated color matches this color if (newColorHex == colors[i].jscolor.toString()) { //if the color isnt the one that has the picker currently open if (!colors[i].parentElement.classList.contains('jscolor-active')) { //console.log('%cColor is duplicate', colorCheckingStyle); //show the duplicate color warning duplicateColorWarning.style.visibility = 'visible'; //shake warning icon duplicateColorWarning.classList.remove('shake'); void duplicateColorWarning.offsetWidth; duplicateColorWarning.classList.add('shake'); //exit function without updating color return; } } } //if the color being edited has a duplicate color warning, remove it duplicateColorWarning.style.visibility = 'hidden'; currentlyEditedColor.firstChild.jscolor.fromString(newColorHex); replaceAllOfColor(oldColor, newColor); //set new old color to changed color hexElement.oldColor = newColor; currentPalette.push('#' + newColorHex); //if this is the current color, update the drawing color if (hexElement.colorElement.parentElement.classList.contains('selected')) { for (let i=1; i { //hide edit button button.parentElement.lastChild.classList.add('hidden'); //show jscolor picker, if basic mode is enabled if (pixelEditorMode == 'Basic') button.parentElement.firstChild.jscolor.show(); else showDialogue("palette-block", false); }); return listItem; } return { currentPalette, addColor, AddToSimplePalette } })(); console.log("Color module: " + ColorModule);