iPhone: How to Determine Average Light/Dark of an Area of an UIImage

Posted by TechZen on Stack Overflow See other posts from Stack Overflow or by TechZen
Published on 2010-02-23T21:21:19Z Indexed on 2010/04/08 10:03 UTC
Read the original article Hit count: 216

Filed under:
|
|
|

I need to place labels with a transparent background over a variable-content UIImage. Readability will vary significantly depending on the relationship between the color of the label's text and the color/luminosity of the area of the image displayed under the label. Since the image will be constantly changing, the color of the label's text needs to change in sync.

I have found several techniques for determining the color, perceived luminosity etc of a single pixel. However, I need to rather quickly (while a view loads) determine the rough perceived color/luminosity of an area of the UIImage under the frame of the UILabel. I presume I will also need to measure the alpha because the same color/luminosity looks different at different alpha values.

Is there a way to calculate such a value for an area? Will I be reduced to simply summing pixels? If it comes to that, is there an algorithm to accomplish this?

I've thought of two possible approaches:

  1. Perform some "folding" operations i.e. combining pixels from one half of the area to the other half. Then repeat until I get a single value. Would this be practical? How would you logically combine pixels to average their perceived color/luminosity?
  2. Sample a statistically significant number of pixels in the area and then combine them (somehow) to get a rough measure.

I think this problem comes up a lot these days with people being so found of customizing backgrounds. Seems like something that would be worth my time to bang out a category or class to handle this and then share it around.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uiimage