假设我有一个 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
扩展:
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 的任何或值非常麻烦。使用HandyUIKit(通过 Carthage 安装)让您的生活更轻松:hsba
rgba
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)
该库还在您的项目中添加了一些其他方便的 UI 功能——查看 GitHub 上的 README 以获取更多详细信息。
我希望它有帮助!
此处尚未提及的一个重要注意事项是您的 UIColor 应该位于扩展的 RGB 空间中。根据最初创建颜色的方式,如果它只是 RGB,则此函数可能会返回 false。
其次,我对@ambientlight 的回答做了一个变体,以使 API 更加流畅。您可以调整 1 个或所有属性。
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 *customColor = [UIColor colorWithHue: x.xx saturation: x.xx brightness: x.xx alpha: 1.0];
有一个类函数colorWithHue:saturation:brightness:alpha:
当然,您可以getHue:saturation:brightness:alpha:
在初始化时先使用,然后再进行任何更改[UIColor redColor]
你可以简单地修改它
[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]];
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
. 最后,一如既往地在编排中,释放CGImageRelease
以对冲记忆问题。
[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00];