我想通过对它应用0.3的alpha值来“变亮”某种颜色。这在白色背景上显示。
我想确定与白色背景上显示的半透明颜色相对应的不透明颜色。原因是我将其用作视图控制器的背景色,并且当该背景为半透明时,过渡效果很差。
我没有尝试过任何东西,因为除了可能拍摄快照并获得其颜色外,没有一个合理的方法来解决这个问题,但这似乎有点过头了。找不到任何信息,搜索结果到处都是“如何使背景半透明”等
编辑:将imataptool的答案部分放在一起(并移植到Swift),这是我想出的:
extension UIColor {
static func opaqueColorByDisplayingTransparentColorOnBackground(transparentColor: UIColor, backgroundColor: UIColor) -> UIColor {
let bgView = UIView(frame: CGRectMake(0, 0, 1, 1))
bgView.backgroundColor = backgroundColor
let overlayView = UIView(frame: CGRectMake(0, 0, 1, 1))
overlayView.backgroundColor = transparentColor
bgView.addSubview(overlayView)
let image = UIView.imageWithView(bgView)
let provider = CGImageGetDataProvider(image.CGImage)
let pixelData = CGDataProviderCopyData(provider)
let data = CFDataGetBytePtr(pixelData)
return UIColor(
red: CGFloat(data[0]) / 255.0,
green: CGFloat(data[1]) / 255.0,
blue: CGFloat(data[2]) / 255.0,
alpha: 1
)
}
}
extension UIView {
// src http://stackoverflow.com/a/32042439/930450
class func imageWithView(view: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
}
编辑1:这并不是说这是实现特定目标的最佳方法,但是它确实解决了获得具有特定不透明度的两种颜色混合的RGB的问题。您没有提供足够的信息来说明我到底在做什么,以便给出更具体的答案。但是,如果此解决方案以令人满意的性能满足您的需求,那么请运行该解决方案。
编辑2:有关在iOS 7上将UIView渲染为位图的新(ish)方法,请参阅此问题与解答(因此,如果您支持iOS 6,则可以忽略此方法,但这不太可能)。本文的要旨是您现在可以使用UIView方法 -drawViewHierarchyInRect:(CGRect)afterScreenUpdates:(BOOL)
我不能说是否存在一种数学方法来计算确切的答案,但是一种解决方法是:
做1是微不足道的。你可以做2
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, [[UIScreen mainScreen] scale]);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
现在img
包含具有所需颜色的图像。现在,您只需要弄清楚它是由什么颜色组成的。我相信您可以使用此答案中提供的代码来做到这一点。为了方便起见,我将其复制到此处。
CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);
int pixelInfo = ((image.size.width * y) + x ) * 4; // The image is png
UInt8 red = data[pixelInfo]; // If you need this info, enable it
UInt8 green = data[(pixelInfo + 1)]; // If you need this info, enable it
UInt8 blue = data[pixelInfo + 2]; // If you need this info, enable itgame
CFRelease(pixelData);
red
,green
和blue
现在包含在任何你指出你的观点挑颜色的RGB值。您不妨只是去中间。同样,为了提高此过程的性能,您可以选择仅对所查看视图的一小部分成像(可能只有一个像素)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句