This commit is contained in:
unsettledgames 2021-07-25 23:26:32 +02:00
parent 3361048f24
commit 1bf5d4e433
5 changed files with 23 additions and 29 deletions

View File

@ -346,6 +346,7 @@ const ColorModule = (() => {
* @param {*} paletteColors The colours of the palette
*/
function createColorPalette(paletteColors) {
console.log("creating palette");
//remove current palette
while (colorsMenu.childElementCount > 1)
colorsMenu.children[0].remove();
@ -389,6 +390,9 @@ const ColorModule = (() => {
*/
function createPaletteFromLayers() {
let colors = {};
let nColors = 0;
//create array out of colors object
let colorPaletteArray = [];
for (let i=0; i<layers.length; i++) {
if (layers[i].menuEntry != null) {
@ -398,13 +402,14 @@ const ColorModule = (() => {
for (let j=0; j<dataLength; j += 4) {
if (!isPixelEmpty(imageData[j])) {
let color = imageData[j]+','+imageData[j + 1]+','+imageData[j + 2];
if (!colors[color] && imageData[j + 3] != 0) {
if (!colors[color]) {
colorPaletteArray.push('#' + new Color("rgb", imageData[j], imageData[j + 1], imageData[j + 2]).hex);
colors[color] = new Color("rgb", imageData[j], imageData[j + 1], imageData[j + 2]).rgb;
nColors++;
//don't allow more than 256 colors to be added
if (Object.keys(colors).length >= settings.maxColorsOnImportedImage) {
alert('The image loaded seems to have more than '+settings.maxColorsOnImportedImage+' colors.');
if (nColors >= Settings.getCurrSettings().maxColorsOnImportedImage) {
alert('The image loaded seems to have more than '+Settings.getCurrSettings().maxColorsOnImportedImage+' colors.');
break;
}
}
@ -413,18 +418,10 @@ const ColorModule = (() => {
}
}
//create array out of colors object
let colorPaletteArray = [];
for (let color in colors) {
if (colors.hasOwnProperty(color)) {
colorPaletteArray.push('#'+Color.rgbToHex(colors[color]));
}
}
console.log("palette");
//create palette from colors array
createColorPalette(colorPaletteArray);
console.log("Done 2");
}
function updateCurrentColor(color, refLayer) {

View File

@ -38,8 +38,8 @@ const History = (() => {
undoStates.push(state);
//limit the number of states to settings.numberOfHistoryStates
if (undoStates.length > settings.numberOfHistoryStates) {
undoStates = undoStates.splice(-settings.numberOfHistoryStates, settings.numberOfHistoryStates);
if (undoStates.length > Settings.getCurrSettings().numberOfHistoryStates) {
undoStates = undoStates.splice(-Settings.getCurrSettings().numberOfHistoryStates, Settings.getCurrSettings().numberOfHistoryStates);
}
//there is now definitely at least 1 undo state, so the button shouldnt be disabled

View File

@ -57,7 +57,12 @@ const Settings = (() => {
Dialogue.closeDialogue();
}
return {
function getCurrSettings() {
return settings;
}
return {
getCurrSettings
}
})();

View File

@ -6,16 +6,8 @@
* @param {*} pixel
*/
function isPixelEmpty(pixel) {
if (pixel == null || pixel === undefined) {
return false;
}
// If the alpha channel is 0, the current pixel is empty
if (pixel[3] == 0) {
return true;
}
return false;
return pixel[3] == 0;
}
// REFACTOR: move to eyedropper onMouseUp event?

View File

@ -66,7 +66,7 @@ function fillPixelGrid() {
pixelGrid.canvas.width = originalSize[0] * Math.round(lineDistance);
pixelGrid.canvas.height = originalSize[1] * Math.round(lineDistance);
context.strokeStyle = settings.pixelGridColour;
context.strokeStyle = Settings.getCurrSettings().pixelGridColour;
// OPTIMIZABLE, could probably be a bit more elegant
// Draw horizontal lines