基于背景图像的文本颜色

问题描述:

我的视图有一个带有文本标签叠加的背景图片
基于背景图像确定文本颜色的最佳/好动态方法是什么,因此它可以读取(目前我只对确定感兴趣如果文字颜色应该是暗的还是浅的)

My view has a background image with a text label overlay What's the best/good dynamic way to determine text color based on the background image so it can be readable (For now, I'm only interested on determining if text color should be dark or light)

谢谢:)

使用提到的链接找到平均颜色

Find the average color with the link woz mentioned

然后设置文字样式

CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
[averageColor getRed:&red green:&green blue:&blue alpha:&alpha];

int threshold = 105;
int bgDelta = ((red * 0.299) + (green * 0.587) + (blue * 0.114));

UIColor *textColor = (255 - bgDelta < threshold) ? [UIColor blackColor] : [UIColor whiteColor];

这样的事情。

你可以也可以使用上面的链接从图像中获取 UIColor 并使用亚光的类别,让UIColor变浅或变暗。

You could also use the link above to get the UIColor from the image and use matt's category for UIColor to get light or dark.