# \[Script\] useTool eyedropper not working, but pencil does

**URL:** https://community.aseprite.org/t/script-usetool-eyedropper-not-working-but-pencil-does/13112
**Category:** Scripts & Extensions
**Created:** [February 15, 2022, 1:59pm UTC](https://community.aseprite.org/t/script-usetool-eyedropper-not-working-but-pencil-does/13112 "2022-02-15T13:59:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![DimaUndSo](https://community.aseprite.org/user_avatar/community.aseprite.org/dimaundso/32/10976_2.png) [@DimaUndSo](https://community.aseprite.org/u/DimaUndSo)
#### Post date: [February 15, 2022, 1:59pm UTC](https://community.aseprite.org/t/script-usetool-eyedropper-not-working-but-pencil-does/13112/1 "2022-02-15T13:59:06Z")

</div>

Hello,  
first thank you for this beautiful tool!

I have a problem using the eyedropper via useTool.

I have a function like this:

```auto
	local function eyedropper( x,y )
		app.useTool{
			tool="eyedropper",
			points={ Point(x,y) },
			button=MouseButton.LEFT
		}
	end

```

Which I call like:

```auto
eyedropper( 20,28 )

```

20,28 is the pixel I want to get the color from.  
But after I run this, i still don’t have the color. What am I doing wrong?

I do the same with pencil and it works well:

```auto
	local function draw( x,y )
		app.useTool{
			tool="pencil",
			points={ Point(x,y) },
			button=MouseButton.LEFT
		}
	end

```

```auto
draw( 20,26 )

```

Why does it not work with the eyedropper tool?  
I just started scripting so I guess I missed something?

Thank you!

---

<div class="post-metadata">

### Author: ![behreandtjeremy](https://community.aseprite.org/user_avatar/community.aseprite.org/behreandtjeremy/32/6606_2.png) [@behreandtjeremy](https://community.aseprite.org/u/behreandtjeremy)
#### Post date: [February 15, 2022, 4:23pm UTC](https://community.aseprite.org/t/script-usetool-eyedropper-not-working-but-pencil-does/13112/2 "2022-02-15T16:23:49Z")

</div>

Hi @DimaUndSo,

Please, there is no need to shout (in your post title).

afaik some tools just don’t work via scripting. I’ve run into [similar problems](https://community.aseprite.org/t/how-to-draw-cubic-bezier-curve-via-lua-script/8439) myself. Someone can correct me if I’m wrong.

Maybe try a workaround like this:

```lua
local x = 35
local y = 28

local activeSprite = app.activeSprite
local activeFrame = app.activeFrame
if activeSprite and activeFrame then
    local xVal = math.min(math.max(x, 0), activeSprite.width - 1)
    local yVal = math.min(math.max(y, 0), activeSprite.height - 1)

    -- This supports a composite from RGB color mode only.
    -- To support indexed and grayscale, you'd need
    -- to take extra steps.
    local flatImg = Image(1, 1)
    flatImg:drawSprite(activeSprite, activeFrame, -Point(xVal, yVal))
    local hex = flatImg:getPixel(0, 0)
    local clr = Color(hex)
    app.fgColor = clr
end

```

The script above assumes you want the composite of the whole sprite. It also assumes RGB color mode only, not grayscale or indexed.

If you want to sample a specific layer, you have to consider a bunch of other factors: that cels have an offset position, that both cels and layers have an opacity, that layers have a blend mode, that layers can be groups and that layers can be visible or invisible (locally or due to being a child of a parent group).

Best to you,  
Jeremy

---

<div class="post-metadata">

### Author: ![DimaUndSo](https://community.aseprite.org/user_avatar/community.aseprite.org/dimaundso/32/10976_2.png) [@DimaUndSo](https://community.aseprite.org/u/DimaUndSo)
#### Post date: [February 15, 2022, 6:56pm UTC](https://community.aseprite.org/t/script-usetool-eyedropper-not-working-but-pencil-does/13112/3 "2022-02-15T18:56:10Z")

</div>

Hey Jeremy, thanks for your reply.  
I tried your workaround but it didn’t worked for me, I think I got a red color instead of a grey I was looking for, but I guess I just did something wrong.

But I kept trying things and have now build my colorpicker as I needed!  
This here works for me:

```auto
local function eyedropper( x,y )
	local cel = app.activeCel
	local image = app.activeImage
	-- convert given canvas x,y coordinates to image coordinates
	local boundsX = cel.bounds.x
	local boundsY = cel.bounds.y
	local imageX = x - boundsX
	local imageY = y - boundsY
	-- get color (integer value)
	local intColor = image:getPixel(imageX,imageY)

	-- get rgba values from the integer value
	local r = app.pixelColor.rgbaR( intColor )
	local g = app.pixelColor.rgbaG( intColor )
	local b = app.pixelColor.rgbaB( intColor )
	local a = app.pixelColor.rgbaA( intColor )
	-- set rgba color value
	local color = Color{ r=r,g=g,b=b,a=a }
	-- set as selected foreground color
	app.fgColor = color
	-- return
	return color
end

local function draw( x,y, color )
	app.useTool{
		tool="pencil",
		color = color,
		points={ Point(x,y) },
		button=MouseButton.LEFT
	}
end

-- pick color
eyedropper( 1,1 )
-- draw pixel
draw(2,1)

```
