# Fast Algorithm to Determine Average Brightness of an Image

```Use Case:

I want to place puzzle tiles on top of an image. We take a field, put a grc
behind it, take a snapshot, crop each word off at -1000, -1000 and then
scramble the tiles to make a puzzle… all this works really well. So I decided
to dress it up a bit more: we dynamically choose a color for the foreground of
the field in RGB range of 0-135 for all three colors → This gets us dark type…
then add 100 to all the colors and this gets us a color for the bkgnd grc which
is the same hue, but brighter (well, that depends on your preferred brand HSV,
HSL, HSB LAB color theory, it's not that simple… but, close enough for this
context)  tiles look great.. color is randomized for each puzzle with the
caveat of an occasional "ugly" (in relation to the nature photo in the
background)  but still… cool..```
```
OK I thought hmmm we could also use light type with dark background and I got
this working… too much fun..

Switch sUserColorRange
case "darkType"
set the itemdelimiter to "," # just to be safe
put randomInRange(0,135) into tRGB
put comma & randomInRange(0,135) after tRGB
put comma & randomInRange(0,135) after tRGB
set the backgroundcolor of grc "startHere" to tRGB
set the foregroundcolor of fld _Quote to tRGB
# same hue for background, but light
repeat for each item tHue in tRGB
put tHue+ (115) & comma after tTileBkgndRGB
end repeat
delete char -1 of tTileBkgndRGB
set the backgroundColor of grc quoteBkgnd to tTileBkgndRGB
break
case "lightType"
set the itemdelimiter to "," # just to be safe
put randomInRange(136,255) into tRGB
put comma & randomInRange(136,255) after tRGB
put comma & randomInRange(136,255) after tRGB
set the backgroundcolor of grc "startHere" to tRGB  ## oops need to fix this…
too light, disappears
set the foregroundcolor of fld _Quote to tRGB
# same hue for background, but light
repeat for each item tHue in tRGB
put tHue- (115) & comma after tTileBkgndRGB
end repeat
delete char -1 of tTileBkgndRGB
set the backgroundColor of grc quoteBkgnd to tTileBkgndRGB
end Switch

The incoming nature photo that is the backdrop for the puzzle is a random
selection. The above is predicated on a user choice/save-as-setting, but a
preferred algorithm would be:

Determine average overall brightness of the image. If below 130 on a scale of
1-255, then use light colored type on dark tile… if >130 then use dark type on
light tiles

So is there a fast, efficient way to analyze a 1200 X 800 px image to get this
average brightness value.

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com