get("#348") -- rgb functions -- returns value between 0 and 1 function averageBrightness(img, x1, y1, x2, y2) local sum, count = 0, 0 local w, h, g = img.width, img.height, img.getInt for y=y1, y2-1 do if y >= 0 and y < h then for x=x1, x2-1 do if x >= 0 and x < w then count = count+1 sum = sum+bright(rgb(g(x, y))) end end end end return count == 0 and nil or sum/count end