[Script] useTool eyedropper not working, but pencil does

Hello,
first thank you for this beautiful tool!

I have a problem using the eyedropper via useTool.

I have a function like this:

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

Which I call like:

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:

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

draw( 20,26 )

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

Thank you!

1 Like

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 myself. Someone can correct me if I’m wrong.

Maybe try a workaround like this:

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

1 Like

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:

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)
3 Likes