05-13-26

Color Click and Drag Script

This Lua script for hardware input detects a specified color on screen and automatically clicks. Holding Mouse5 smoothly drags the mouse towards the detected color, with configurable smoothing and spe

rebindlua scriptcolor detectionmouse clickmouse draghardware inputscript

Question

Create a script that detects a color and sends a click. Also, when I hold Mouse5, it should drag the mouse over (with smoothing) to the detected color.

Rebind AI

I'll create a color detection click script with mouse dragging and smoothing when holding Mouse5.

Before I write this, let me clarify a few things to make sure it works perfectly for you:

  1. Color detection region: Should it scan the center of the screen, or would you like to define a specific area (e.g., a box around the center)?
  2. Click type: Left click, right click, or configurable?
  3. Drag smoothing: When holding Mouse5 and dragging, should it move toward the detected color, or do you have a specific drag pattern in mind?
  4. Detection frequency: How often should it scan for the color — every frame, or less frequently to save CPU?

For now, I'll write a script that:

  • Scans the screen center for a target color
  • Clicks when detected
  • When Mouse5 is held, smoothly drags the mouse toward the color position
  • Includes UI config for color, tolerance, sensitivity, and smoothing
lua
-- rebind: min_sdk=0.1.0
-- rebind: name=Color Click + Drag
-- rebind: description=Click on detected color, drag with Mouse5 smoothing

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable" }),
    
    target_color = UI.Text("FF0000", {
        label = "Target Color (hex)",
        placeholder = "RRGGBB",
        maxLength = 6,
        group = "Detection",
    }),
    
    tolerance = UI.Slider(20, {
        min = 0,
        max = 100,
        label = "Color Tolerance",
        group = "Detection",
    }),
    
    scan_radius = UI.Slider(100, {
        min = 10,
        max = 500,
        label = "Scan Radius (px)",
        suffix = "px",
        group = "Detection",
    }),
    
    smoothing = UI.Slider(30, {
        min = 0,
        max = 80,
        label = "Drag Smoothing",
        suffix = "%",
        group = "Dragging",
    }),
    
    drag_speed = UI.Slider(5, {
        min = 1,
        max = 20,
        label = "Drag Speed",
        group = "Dragging",
    }),
    
    click_on_detect = UI.Toggle(true, {
        label = "Auto-Click on Detect",
        group = "Clicking",
    }),
})

local function hexToRgb(hex)
    if not hex or #hex ~= 6 then return 0, 0, 0 end
    return tonumber(hex:sub(1, 2), 16), tonumber(hex:sub(3, 4), 16), tonumber(hex:sub(5, 6), 16)
end

local function colorDistance(hex1, hex2)
    local r1, g1, b1 = hexToRgb(hex1)
    local r2, g2, b2 = hexToRgb(hex2)
    return math.sqrt((r2 - r1) ^ 2 + (g2 - g1) ^ 2 + (b2 - b1) ^ 2)
end

local function sampleRegion(cx, cy, radius)
    local maxDist = 0
    local closestX, closestY = cx, cy
    local targetRgb = cfg.target_color
    
    -- Sample a grid of points within the radius
    local step = math.max(1, math.floor(radius / 10))
    for x = cx - radius, cx + radius, step do
        for y = cy - radius, cy + radius, step do
            local dist = math.sqrt((x - cx) ^ 2 + (y - cy) ^ 2)
            if dist <= radius then
                local pixel = Screen.GetPixelColor(x, y)
                local colorDist = colorDistance(pixel, targetRgb)
                if colorDist < maxDist or maxDist == 0 then
                    maxDist = colorDist
                    closestX = x
                    closestY = y
                end
            end
        end
    end
    
    if maxDist <= cfg.tolerance then
        return closestX, closestY, true
    end
    return closestX, closestY, false
end

local dragTask = nil
local lastPos = { x = 0, y = 0 }

function OnDown(key)
    if key == "Mouse5" and cfg.enabled then
        if dragTask then dragTask:Cancel() end
        
        dragTask = Run(function()
            lastPos = Input.GetMousePos()
            
            while Input.IsDown("Mouse5") do
                local sw, sh = System.Screen()
                local cx, cy = math.floor(sw / 2), math.floor(sh / 2)
                
                local targetX, targetY, found = sampleRegion(cx, cy, cfg.scan_radius)
                
                if found then
                    local curPos = Input.GetMousePos()
                    local dx = targetX - curPos.x
                    local dy = targetY - curPos.y
                    local dist = math.sqrt(dx * dx + dy * dy)
                    
                    if dist > 1 then
                        local smoothFactor = cfg.smoothing / 100
                        local moveAmount = (1 - smoothFactor) * cfg.drag_speed
                        
                        local moveX = (dx / dist) * moveAmount
                        local moveY = (dy / dist) * moveAmount
                        
                        HID.Move(moveX, moveY)
                    end
                end
                
                Sleep(16) -- ~60fps
            end
        end)
        
        return false
    end
    
    return true
end

function OnUp(key)
    if key == "Mouse5" then
        if dragTask then
            dragTask:Cancel()
            dragTask = nil
        end
        return false
    end
    return true
end

function OnTick()
    if not cfg.enabled or not cfg.click_on_detect then return end
    
    local sw, sh = System.Screen()
    local cx, cy = math.floor(sw / 2), math.floor(sh / 2)
    
    local targetX, targetY, found = sampleRegion(cx, cy, cfg.scan_radius)
    
    if found then
        HID.Press("Mouse1", 20)
    end
end

function OnBlur()
    if dragTask then
        dragTask:Cancel()
        dragTask = nil
    end
end

function OnStop()
    if dragTask then
        dragTask:Cancel()
        dragTask = nil
    end
end

How it works:

  1. Color Detection — Scans a region around the screen center in a grid pattern, comparing each pixel to your target color (hex format). If any pixel is within the tolerance threshold, it's considered a match.

  2. Auto-Click — When a color is detected and click_on_detect is enabled, it sends a left click every frame. Disable this if you only want dragging.

  3. Mouse5 Drag — While holding Mouse5, the script continuously scans for the color and smoothly drags the mouse toward it. The Smoothing slider controls how much the movement is dampened (higher = more lag, smoother curves). Drag Speed controls how fast it moves.

  4. OnTick — Runs every frame and checks for the color, clicking when found.

Config options:

  • Target Color: Enter the hex color code (e.g., FF0000 for red, 00FF00 for green)
  • Tolerance: How close a pixel's color must be to match (0–100, higher = more forgiving)
  • Scan Radius: The area around screen center to search (in pixels)
  • Smoothing: How much to smooth the drag movement (0 = instant, 80 = very smooth)
  • Drag Speed: How fast to move toward the target when dragging
  • Auto-Click on Detect: Whether to automatically click when color is found

Learn more