Get RGB color of a specific screen location

hakan at hakan at
Mon Jan 7 18:04:41 CET 2019

Well, he could, if he didn’t wanted an average value of some more pixels, and, calculating an average via screenMouseLoc is really slow. Exporting a rect to an image is fairly quick though

Found the following in a stack:

# Calculates the average color value for an image
# pID should be the long id of the image we want to get the average from
function imageAverageValue pID
   put the imageData of pID into tData
   put the number of bytes of tData / 4 into tNumPixels
   # Sum all pixel values
   repeat with i = 0 to tNumPixels - 1
      add byteToNum(byte i*4 + 2 of tData) to tResult["red"]
      add byteToNum(byte i*4 + 3 of tData) to tResult["green"]
      add byteToNum(byte i*4 + 4 of tData) to tResult["blue"]
   end repeat
   # Calculate mean value
   repeat for each item anItem in "red,green,blue"
      put round(tResult[anItem] / tNumPixels) into tResult[anItem]
   end repeat
   return tResult["red"], tResult["green"], tResult["blue"]
end imageAverageValue

So to get the mouse average color in a square ± 2px from the mouseLoc we could do:

function getMouseAverageColor pSize
   if pSize is empty then put 2 into pSize
   put globalLoc(the mouseLoc) into tLoc
   if there is no image "averageColor" then create invisible image "averageColor"
   put the long id of image "averageColor" into tID
   export snapshot from rectangle item 1 of tLoc - pSize, item 2 of tLoc - pSize,  \
         item 1 of tLoc + pSize, item 2 of tLoc + pSize to image "averageColor"
   return imageAverageValue(tID)
end getMouseAvaregeColor

Happy coding!

On 7 Jan 2019, 15:35 +0100, dunbarxx via use-livecode <use-livecode at>, wrote:
> A kluge, but couldn't you:
> on mouseUp
> set the screenMouseLoc to "200,200"
> answer the mouseColor
> end mouseUp
> I just threw this together, so you might want to restore the original
> mouseLoc, and locking the screen might be useful.
> Craig Newman
> --
> Sent from:
> _______________________________________________
> use-livecode mailing list
> use-livecode at
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:

More information about the use-livecode mailing list