Programmatically determine the average greyscale

Sannyasin Brahmanathaswami brahma at
Thu Feb 18 18:05:47 EST 2016

Use case:

I want to import images as patterns for buttons. This always works as they are centered and made the correct size will fill the button area, regardless of the margins (setting the icon takes the margins position)

Buttons are big squares 205 X 205 on a mobile interface (design adopted from FlipBoard)

By tweaking the margins you can get the name of the button nicely positioned in the top left corner


But if I use white type on top of the image and the sky appears there in the top left area of the image is a light color, then we have a problem with contrast/readability.

Is there a way to get the "average" grayscale value of a small area of a photo?

So in the photo that is 205 X 205... can we determine the average grayscale level of an area that is, relative to the top left of the image:

20,20,40,40  which would be a small square 20 by 20 pixels.

if the average grayscale level were above 75%, then I could programatically change the foreground (text) color of the button to black  "on the fly"

I think the answer is "use the charToNum(of char x of the alphaData of image 1)

So to check on the alpha data of a pixel that was 30 pixels down and 30 pixels over on a 205 pixel square.. then you  do something like

put  ( (the width of this image) X 29 ) + into tOnePixelCheck

But I don't think alphaData applies to just a light color... even if the color is light... but can i query the color of a single pixel?  I don't see a way to refer to this color in the dictionary?


More information about the use-livecode mailing list