假设我有一个UIColor
UIColor *color = [UIColor redColor];
现在我想修改颜色的饱和度/色相/亮度,该怎么办呢?我已经阅读了文档,但我还是很困惑。
我想修改我所创建的UIColor([UIColor redColor]),而不是用一些偏好设置初始化一个新的颜色。我该如何修改它并保留原始的颜色?我知道colorWithHue:saturation:brightness:alpha:
方法,但我需要更新现有颜色的属性,保持红色颜色。
假设我有一个UIColor
UIColor *color = [UIColor redColor];
现在我想修改颜色的饱和度/色相/亮度,该怎么办呢?我已经阅读了文档,但我还是很困惑。
我想修改我所创建的UIColor([UIColor redColor]),而不是用一些偏好设置初始化一个新的颜色。我该如何修改它并保留原始的颜色?我知道colorWithHue:saturation:brightness:alpha:
方法,但我需要更新现有颜色的属性,保持红色颜色。
getHue:saturation:brightness:alpha:
,然后调整数值,并使用 +[UIColor colorWithHue:saturation:brightness:alpha:]
创建一个新的颜色并使用已调整的组件。CGFloat hue, saturation, brightness, alpha ;
BOOL ok = [ <color> getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha ] ;
if ( !ok ) {
// handle error
}
// ... adjust components..
UIColor * newColor = [ UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha ] ;
这里是一个有用的 UIColor
扩展,适用于 Swift:
extension UIColor {
func modified(withAdditionalHue hue: CGFloat, additionalSaturation: CGFloat, additionalBrightness: CGFloat) -> UIColor {
var currentHue: CGFloat = 0.0
var currentSaturation: CGFloat = 0.0
var currentBrigthness: CGFloat = 0.0
var currentAlpha: CGFloat = 0.0
if self.getHue(¤tHue, saturation: ¤tSaturation, brightness: ¤tBrigthness, alpha: ¤tAlpha){
return UIColor(hue: currentHue + hue,
saturation: currentSaturation + additionalSaturation,
brightness: currentBrigthness + additionalBrightness,
alpha: currentAlpha)
} else {
return self
}
}
}
这里有一点需要注意,你的UIColor应该在扩展RGB空间中。根据你的颜色最初是如何创建的,如果只是RGB,那么这个函数可能会返回false。
其次,我对@ambientlight的答案进行了改进,使API更加流畅。你可以调整一个或所有属性。
extension UIColor {
public func adjust(hueBy hue: CGFloat = 0, saturationBy saturation: CGFloat = 0, brightnessBy brightness: CGFloat = 0) -> UIColor {
var currentHue: CGFloat = 0.0
var currentSaturation: CGFloat = 0.0
var currentBrigthness: CGFloat = 0.0
var currentAlpha: CGFloat = 0.0
if getHue(¤tHue, saturation: ¤tSaturation, brightness: ¤tBrigthness, alpha: ¤tAlpha) {
return UIColor(hue: currentHue + hue,
saturation: currentSaturation + saturation,
brightness: currentBrigthness + brightness,
alpha: currentAlpha)
} else {
return self
}
}
}
很遗憾,默认情况下,更改 UIColor 的任何 hsba 或 rgba 值都是相当麻烦的。但使用 HandyUIKit(通过 Carthage 安装)会使您的生活变得容易得多:HandyUIKit
import HandyUIKit
// each line creates a new UIColor object with the new value
color.change(.hue, to: 0.1)
color.change(.brightness, to: 0.2)
color.change(.saturation, to: 0.3)
color.change(.alpha, to: 0.4)
// chaining them returns a single new object with all values changed
color.change(.hue, to: 0.5)
.change(.brightness, to: 0.6)
.change(.saturation, to: 0.7)
还有应用相对变化的选项:
// create a new UIColor object with hue & brightness increased by 0.2
color.change(.hue, by: 0.2)
.change(.brightness, by: 0.2)
你可以通过设置以下数值来使用这个方法
UIColor *customColor = [UIColor colorWithHue: x.xx saturation: x.xx brightness: x.xx alpha: 1.0];
有一个类函数 colorWithHue:saturation:brightness:alpha:
当你使用 [UIColor redColor]
初始化时,你可以先使用 getHue:saturation:brightness:alpha:
来获取颜色的属性,然后再进行任何更改。
CGSize imageSize = [ba size];
CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);
// Create a context containing the image.
UIGraphicsBeginImageContext(imageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
[sourceimage drawInRect:imageExtent];
// Draw the hue on top of the image.
CGContextSetBlendMode(context, kCGBlendModeHue);
[[UIColor colorWithHue:yourvalue saturation:1.0 brightness:1 alpha:1.0] set];
UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
[imagePath fill];
CGImageRef imageref=CGBitmapContextCreateImage(context);
UIImage *result =[UIImage imageWithCGImage:imageref];
CGImageRelease(imageref);
在前两行中,它描述了图像大小和图像矩形。 CGContextRef
用于在核心图形中创建上下文。之后的第5行是您要应用色调和图像矩形的图像。之后是重要的混合模式。之后是UI colorWithHue
,其中传递了色相、饱和度、亮度和alpha的值。为了获得正确的效果,请给出1.0的alpha值。最后,您应该设置 uicolor.create
bezerpath 或者直接给出 cgcontextsetfill(context)
。最后创建 imageref
并将该图像放入 UIImage
中。最后,在choreographics中,始终释放 CGImageRelease
以避免内存问题。
你可以通过简单修改来实现这个功能
[UIColor colorWithHue:YourHueValue saturation:YourSaturationValue brightness:YourBrightnessValueValue alpha:1.00];
alpha 表示视图的不透明度,取值范围为 0.0 到 1.0
UIColor 无法改变饱和度/色调/亮度,您可以使用 CoreImage。
以下是一些示例代码:
CIFilter *filter = [CIFilter filterWithName:@"CIColorControls"];
[filter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputContrast"];
[filter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputSaturation"];
[filter setValue:[NSNumber numberWithFloat:0.0] forKey:@"inputBrightness"];
[filter setValue:_A_CIImage_ forKey:kCIInputImageKey];
CIImage *_outputImage = filter.outputImage;
CIContext context = [CIContext contextWithOptions:nil];
CGImageRef outputImageRef = [context createCGImage: _outputImage fromRect:[_outputImage extent]];
[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00];