Programmatically determine the average greyscale

J. Landman Gay jacque at
Thu Feb 18 22:01:53 EST 2016

On 2/18/2016 8:44 PM, Sannyasin Brahmanathaswami wrote:
> Thanks that could be useful... BTW where to you store this kind of snippet

In an old HC stack I made 20 years ago that I've converted to LC. It's 
not fit for public consumption but it has all my stuff in it so I keep 
using it.

> how do you get the color a specific pixel in an image?

I have three examples. The first one is mine, based on a suggestion from 
Scott Raney. The other two are more sophisticated because they use math...


on getPixelColor pPt -- pass local stack-based pt
   put the screenmouseloc into tOldLoc
   put (item 1 of pPt) + (the left of this stack) into tloc
   put (item 2 of pPt) + (the top of this stack) into item 2 of tloc
   set the screenmouseloc to tloc
   put the mousecolor into tColor
   set the screenmouseloc to tOldLoc
   return tColor
end getPixelColor

It happens so fast you never see the cursor move.


Unknown author:

function readPixelColor pVal
    put globalLoc(pVal) into pVal
    put item 1 of pVal + 1 into item 3 of pVal
    put item 2 of pVal + 1 into item 4 of pVal
    export snapshot from rect pVal to templateImage
    get the imageData of templateImage
    reset templateImage
    return byteToNum(char 2 of it) & "," & byteToNum(char 3 of it) & "," 
& byteToNum(char 4 of it)
end readPixelColor

Scott Rossi:

local theImageData

function getPixelColor pImage,pX,pY
    if theImageData is empty then put the imageData of pImage into
    put width of pImage into W
    put ((pY-1)*W)*4+pX*4 into lastChar
    put charToNum(char lastChar of theImageData) into B
    put charToNum(char lastChar-1 of theImageData) into G
    put charToNum(char lastChar-2 of theImageData) into R
    return R,G,B
end getPixelColor

Jacqueline Landman Gay         |     jacque at
HyperActive Software           |

More information about the use-livecode mailing list