views:

148

answers:

1

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.

A: 

What about simply outlining your text in a way that it will show on both dark and light backgrounds?

This is how it is handled in other situations where text must be displayed over a background with unknown content (for example, films with subtitles).

rh
Could you expand on that? Do you mean adding a shadow or something? I'm not sure how to outline text. The text in the labels might also be variable so I can't just use an image. The text could be rather small so clarity is important.
TechZen
I'm not sure how it's done on an actual UILabel, but when you get down into the drawing calls, you can use CGContextSetTextDrawingMode (ctx, kCGTextFillStroke), then set your CGContextSetRGBStrokeColor and CGContextSetRGBFillColor to two fairly opposite colors, then CGContextShowTextAtPoint with the text to display.
rh
I'll try it. That would be simpler than what I am thinking about doing now.
TechZen
Good luck - let me know how it goes! :)
rh