diff --git a/js/ColorModule.js b/js/ColorModule.js index 6fc2554..2b79241 100644 --- a/js/ColorModule.js +++ b/js/ColorModule.js @@ -444,6 +444,6 @@ const ColorModule = (() => { resetPalette, createColorPalette, createPaletteFromLayers, - updateCurrentColor + updateCurrentColor, } })(); \ No newline at end of file diff --git a/js/ToolManager.js b/js/ToolManager.js index f8353fe..2aff531 100644 --- a/js/ToolManager.js +++ b/js/ToolManager.js @@ -4,6 +4,7 @@ const ToolManager = (() => { rectangleTool = new RectangleTool("rectangle", {type: 'html'}, switchTool); lineTool = new LineTool("line", {type: 'html'}, switchTool); fillTool = new FillTool("fill", {type: 'cursor', pic: 'fill.png'}, switchTool); + eyedropperTool = new EyedropperTool("eyedropper", {type: 'cursor', pic: 'none'}, switchTool); currTool = brushTool; currTool.onSelect(); diff --git a/js/Util.js b/js/Util.js index 76a716c..78d6cd9 100644 --- a/js/Util.js +++ b/js/Util.js @@ -1,6 +1,23 @@ // Acts as a public static class class Util { + /** Tells if a pixel is empty (has alpha = 0) + * + * @param {*} pixel + */ + static 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; + } + /** Tells if element is a child of an element with class className * * @param {*} element diff --git a/js/_fill.js b/js/_fill.js deleted file mode 100644 index e69de29..0000000 diff --git a/js/_toolButtons.js b/js/_toolButtons.js index c66d4f7..fc49004 100644 --- a/js/_toolButtons.js +++ b/js/_toolButtons.js @@ -32,11 +32,6 @@ Events.on('click',"pan-button", function(){ tool.pan.switchTo(); }, false); -//eyedropper -Events.on('click',"eyedropper-button", function(){ - tool.eyedropper.switchTo(); -}, false); - //rectangular selection button Events.on('click', "rectselect-button", function(){ tool.rectselect.switchTo(); diff --git a/js/_variables.js b/js/_variables.js index 6fa2220..11af5fc 100644 --- a/js/_variables.js +++ b/js/_variables.js @@ -5,8 +5,8 @@ var lastMouseClickPos = [0,0]; // REFACTOR: Input IIFE via getter? <- probably e //common elements // REFACTOR: put brush and eyedropper preview in the respective tool implementations +// REFACTOR: this should be in ResizableTool var brushPreview = document.getElementById("brush-preview"); -var eyedropperPreview = document.getElementById("eyedropper-preview"); // REFACTOR: File class? var canvasView = document.getElementById("canvas-view"); diff --git a/js/pixel-editor.js b/js/pixel-editor.js index 538003d..e2952c3 100644 --- a/js/pixel-editor.js +++ b/js/pixel-editor.js @@ -1,7 +1,6 @@ /**utilities**/ //=include lib/cookies.js //=include _jscolor.js -//=include _pixelEditorUtility.js //=include _variables.js //=include lib/sortable.js //=include Util.js @@ -10,6 +9,7 @@ //=include Dialogue.js //=include History.js +//=include ColorModule.js //=include _drawLine.js //=include _tools.js //=include tools/*.js @@ -30,8 +30,6 @@ /**functions**/ //=include _changeZoom.js -//=include ColorModule.js -//=include _fill.js //=include _checkerboard.js //=include _copyPaste.js //=include _resizeCanvas.js @@ -50,7 +48,6 @@ /**event listeners**/ //=include Input.js -//=include _mouseEvents.js /**feature toggles**/ //=include _featureToggles.js diff --git a/js/tools/EyeDropperTool.js b/js/tools/EyeDropperTool.js index e69de29..6d5ede2 100644 --- a/js/tools/EyeDropperTool.js +++ b/js/tools/EyeDropperTool.js @@ -0,0 +1,108 @@ +class EyedropperTool extends Tool { + 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); + } + + onStart(mousePos) { + super.onStart(mousePos); + + 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); + this.eyedropperPreview.style.left = mousePos[0] + currentLayer.canvas.offsetLeft - 30 + 'px'; + this.eyedropperPreview.style.top = mousePos[1] + currentLayer.canvas.offsetTop - 30 + 'px'; + + 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 + for (let i=2; i