iPhone:如何确定 UIImage 区域的平均明暗度
我需要在可变内容 UIImage 上放置具有透明背景的标签。可读性将根据标签文本的颜色与标签下显示的图像区域的颜色/亮度之间的关系而显着变化。由于图像会不断变化,因此标签文本的颜色需要同步变化。
我发现了几种确定 颜色,单个像素的感知亮度等。但是,我需要相当快地(在加载视图时)确定 UILabel 框架下 UIImage 区域的粗略感知颜色/亮度。我想我还需要测量 alpha,因为相同的颜色/亮度在不同的 alpha 值下看起来不同。
有没有办法计算一个面积的这样的值?我会被简化为简单地求和像素吗?如果涉及到这一点,有没有一种算法可以实现这一点?
我想到了两种可能的方法:
- 执行一些“折叠”操作,即将像素从一半区域合并到另一半区域。然后重复直到得到一个值。这实用吗?您将如何逻辑地组合像素来平均其感知的颜色/亮度?
- 在该区域中采样统计上显着数量的像素,然后将它们(以某种方式)组合起来以获得粗略的测量结果。
我认为这个问题现在经常出现,因为人们越来越喜欢自定义背景。似乎值得我花时间来创建一个类别或类来处理这个问题,然后分享它。
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:
- 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?
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地以一种在深色和浅色背景上都显示的方式概述文本怎么样?
这是在其他情况下的处理方式,其中文本必须显示在内容未知的背景上(例如,带有字幕的电影)。
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).