2019-03-27 02:20:54 +03:00
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
/** Creates the colour palette
|
|
|
|
*
|
|
|
|
* @param {*} paletteColors The colours of the palette
|
|
|
|
* @param {*} deletePreviousPalette Tells if the app should delete the previous palette or not
|
|
|
|
* (used when opening a file, for example)
|
|
|
|
*/
|
|
|
|
function createColorPalette(paletteColors, deletePreviousPalette = true) {
|
2020-04-04 10:41:56 +03:00
|
|
|
//remove current palette
|
2020-06-28 17:57:19 +03:00
|
|
|
if (deletePreviousPalette) {
|
|
|
|
colors = document.getElementsByClassName('color-button');
|
|
|
|
while (colors.length > 0) {
|
|
|
|
colors[0].parentElement.remove();
|
|
|
|
}
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var lightestColor = '#000000';
|
|
|
|
var darkestColor = '#ffffff';
|
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
// Adding all the colours in the array
|
2020-09-17 20:24:23 +03:00
|
|
|
for (var i = 0; i < paletteColors.length; i++) {
|
|
|
|
var newColor = paletteColors[i];
|
2020-04-04 10:41:56 +03:00
|
|
|
var newColorElement = addColor(newColor);
|
|
|
|
|
|
|
|
var newColorHex = hexToRgb(newColor);
|
|
|
|
|
|
|
|
var lightestColorHex = hexToRgb(lightestColor);
|
|
|
|
if (newColorHex.r + newColorHex.g + newColorHex.b > lightestColorHex.r + lightestColorHex.g + lightestColorHex.b)
|
|
|
|
lightestColor = newColor;
|
|
|
|
|
|
|
|
var darkestColorHex = hexToRgb(darkestColor);
|
|
|
|
if (newColorHex.r + newColorHex.g + newColorHex.b < darkestColorHex.r + darkestColorHex.g + darkestColorHex.b) {
|
|
|
|
|
|
|
|
//remove current color selection
|
|
|
|
var selectedColor = document.querySelector('#colors-menu li.selected');
|
|
|
|
if (selectedColor) selectedColor.classList.remove('selected');
|
|
|
|
|
|
|
|
//set as current color
|
|
|
|
newColorElement.classList.add('selected');
|
|
|
|
|
|
|
|
darkestColor = newColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:29:35 +03:00
|
|
|
//prepend # if not present
|
|
|
|
if (!darkestColor.includes('#')) darkestColor = '#' + darkestColor;
|
|
|
|
|
2020-04-04 10:41:56 +03:00
|
|
|
//set as current color
|
|
|
|
currentLayer.context.fillStyle = darkestColor;
|
|
|
|
}
|
2020-06-28 17:57:19 +03:00
|
|
|
|
2020-12-31 15:05:51 +03:00
|
|
|
/** Creates the palette with the colours used in all the layers
|
|
|
|
*
|
|
|
|
*/
|
2020-06-28 17:57:19 +03:00
|
|
|
function createPaletteFromLayers() {
|
|
|
|
let colors = {};
|
|
|
|
|
|
|
|
for (let i=0; i<layers.length; i++) {
|
|
|
|
if (layers[i].menuEntry != null) {
|
|
|
|
let imageData = layers[i].context.getImageData(0, 0, layers[i].canvasSize[0], layers[i].canvasSize[1]).data;
|
|
|
|
let dataLength = imageData.length;
|
|
|
|
|
|
|
|
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]) {
|
|
|
|
colors[color] = {r:imageData[j],g:imageData[j + 1],b:imageData[j + 2]};
|
|
|
|
|
|
|
|
//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.');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//create array out of colors object
|
|
|
|
let colorPaletteArray = [];
|
|
|
|
for (let color in colors) {
|
|
|
|
if (colors.hasOwnProperty(color)) {
|
|
|
|
colorPaletteArray.push('#'+rgbToHex(colors[color]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//create palette form colors array
|
|
|
|
createColorPalette(colorPaletteArray, false);
|
|
|
|
}
|