2022-01-08 06:45:35 +03:00
|
|
|
class EyeDropperTool extends Tool {
|
2021-11-01 13:33:18 +03:00
|
|
|
eyedropperPreview = document.getElementById("eyedropper-preview");
|
|
|
|
selectedColor = {r:0, g:0, b:0};
|
|
|
|
|
|
|
|
constructor(name, options, switchFunction) {
|
|
|
|
super(name, options);
|
|
|
|
|
|
|
|
Events.on('click', this.mainButton, switchFunction, this);
|
2022-01-30 02:09:43 +03:00
|
|
|
|
|
|
|
this.resetTutorial();
|
|
|
|
this.addTutorialTitle("Eyedropper tool");
|
|
|
|
this.addTutorialKey("E", " to select the lasso selection tool");
|
|
|
|
this.addTutorialKey("Left drag", " to preview the picked colour");
|
|
|
|
this.addTutorialKey("Aòt + left drag", " to preview the picked colour");
|
|
|
|
this.addTutorialKey("Left click", " to select a colour");
|
|
|
|
this.addTutorialKey("Alt + click", " to select a colour");
|
2022-02-25 17:49:30 +03:00
|
|
|
this.addTutorialImg("/eyedropper-tutorial.gif");
|
2021-11-01 13:33:18 +03:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:31:09 +03:00
|
|
|
onStart(mousePos, target) {
|
2021-11-01 13:33:18 +03:00
|
|
|
super.onStart(mousePos);
|
|
|
|
|
2021-11-01 14:31:09 +03:00
|
|
|
if (target.className != 'drawingCanvas')
|
|
|
|
return;
|
|
|
|
|
2021-11-01 13:33:18 +03:00
|
|
|
this.eyedropperPreview.style.display = 'block';
|
|
|
|
this.onDrag(mousePos);
|
|
|
|
}
|
|
|
|
|
|
|
|
onDrag(mousePos) {
|
|
|
|
super.onDrag(mousePos);
|
|
|
|
|
|
|
|
this.selectedColor = this.pickColor(mousePos);
|
|
|
|
|
|
|
|
this.eyedropperPreview.style.borderColor = '#' + Color.rgbToHex(this.selectedColor);
|
2021-12-06 19:37:43 +03:00
|
|
|
this.eyedropperPreview.style.left = mousePos[0] + currFile.currentLayer.canvas.offsetLeft - 30 + 'px';
|
|
|
|
this.eyedropperPreview.style.top = mousePos[1] + currFile.currentLayer.canvas.offsetTop - 30 + 'px';
|
2021-11-01 13:33:18 +03:00
|
|
|
|
|
|
|
const colorLightness = Math.max(this.selectedColor.r,this.selectedColor.g,this.selectedColor.b);
|
|
|
|
|
|
|
|
//for the darkest 50% of colors, change the eyedropper preview to dark mode
|
|
|
|
if (colorLightness>127) this.eyedropperPreview.classList.remove('dark');
|
|
|
|
else this.eyedropperPreview.classList.add('dark');
|
|
|
|
|
|
|
|
this.changeColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
onEnd(mousePos) {
|
|
|
|
super.onEnd(mousePos);
|
|
|
|
this.eyedropperPreview.style.display = 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
changeColor() {
|
|
|
|
let colorHex = Color.rgbToHex(this.selectedColor);
|
|
|
|
ColorModule.updateCurrentColor('#' + Color.rgbToHex(this.selectedColor));
|
|
|
|
|
|
|
|
let colors = document.getElementsByClassName('color-button');
|
|
|
|
for (let i = 0; i < colors.length; i++) {
|
|
|
|
//if picked color matches this color
|
|
|
|
if (colorHex == colors[i].jscolor.toString()) {
|
|
|
|
//remove current color selection
|
|
|
|
document.querySelector("#colors-menu li.selected")?.classList.remove("selected");
|
|
|
|
|
|
|
|
//set current color
|
2021-12-06 19:37:43 +03:00
|
|
|
for (let i=2; i<currFile.layers.length; i++) {
|
|
|
|
currFile.layers[i].context.fillStyle = '#' + colorHex;
|
2021-11-01 13:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//make color selected
|
|
|
|
colors[i].parentElement.classList.add('selected');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the eyedropped colour (the colour of the pixel pointed by the cursor when the user is using the eyedropper).
|
|
|
|
* It takes the colour of the canvas with the biggest z-index, basically the one the user can see, since it doesn't
|
|
|
|
* make much sense to sample a colour which is hidden behind a different layer
|
|
|
|
*
|
|
|
|
* @param {*} cursorLocation The position of the cursor
|
|
|
|
*/
|
|
|
|
pickColor(cursorLocation) {
|
|
|
|
// Making sure max will take some kind of value
|
|
|
|
let max = -1;
|
|
|
|
// Using tmpColour to sample the sprite
|
|
|
|
let tmpColour;
|
|
|
|
// Returned colour
|
|
|
|
let selectedColor;
|
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
for (let i=1; i<currFile.layers.length-3; i++) {
|
2021-11-01 13:33:18 +03:00
|
|
|
// Getting the colour of the pixel in the cursorLocation
|
2021-12-06 19:37:43 +03:00
|
|
|
tmpColour = currFile.layers[i].context.getImageData(Math.floor(cursorLocation[0]/currFile.zoom),Math.floor(cursorLocation[1]/currFile.zoom),1,1).data;
|
2021-11-01 13:33:18 +03:00
|
|
|
|
|
|
|
// If it's not empty, I check if it's on the top of the previous colour
|
2021-12-06 19:37:43 +03:00
|
|
|
if (currFile.layers[i].canvas.style.zIndex > max || Util.isPixelEmpty(selectedColor) || selectedColor === undefined) {
|
|
|
|
max = currFile.layers[i].canvas.style.zIndex;
|
2021-11-01 13:33:18 +03:00
|
|
|
|
|
|
|
if (!Util.isPixelEmpty(tmpColour)) {
|
|
|
|
selectedColor = tmpColour;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the final colour was empty, I return black
|
|
|
|
if (Util.isPixelEmpty(tmpColour) && selectedColor === undefined) {
|
|
|
|
selectedColor = [0, 0, 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return {r:selectedColor[0], g:selectedColor[1], b:selectedColor[2]};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSelect() {
|
|
|
|
super.onSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
onDeselect() {
|
|
|
|
super.onDeselect();
|
|
|
|
this.eyedropperPreview.style.display = 'none';
|
|
|
|
}
|
|
|
|
}
|