xcode 如何修改 UIColor 的色调、亮度和饱和度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15428422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I modify a UIColor's hue, brightness and saturation?
提问by s6luwJ0A3I
Lets say I have a UIColor
假设我有一个 UIColor
UIColor *color = [UIColor redColor];
Now I want to modify the saturation/hue/brigthness, how do I do that? I did read the documentation but i'm still really confused
现在我想修改饱和度/色调/亮度,我该怎么做?我确实阅读了文档,但我仍然很困惑
I want to modify the UIColor I made ([UIColor redColor]) not initiate a new color with some preferences. How do I modify it retaining the original. I do know about thecolorWithHue:saturation:brightness:alpha:
method, I need to update an existing color's properties, keeping the red color.
我想修改我制作的 UIColor ([UIColor redColor]),而不是使用某些首选项启动新颜色。我如何修改它保留原来的。我知道该colorWithHue:saturation:brightness:alpha:
方法,我需要更新现有颜色的属性,保持红色。
回答by nielsbot
You can call getHue:saturation:brightness:alpha:
on your color, then adjust the values, then create a new color with your adjusted components using +[UIColor 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 ] ;
回答by ambientlight
Here is swift UIColor
extension you might find useful:
这是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
}
}
}
回答by Jeehut
Unfortunately it's quite a hassleto change any of the hsba
or rgba
values of a UIColor by default. Using HandyUIKit(install it via Carthage) makes your life a lot easier:
不幸的是,默认情况下更改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)
There are also options to apply relative changes:
还有一些选项可以应用相对更改:
// create a new UIColor object with hue & brightness increased by 0.2
color.change(.hue, by: 0.2)
.change(.brightness, by: 0.2)
The library also adds some other handy UI featuresinto your project – checkout its README on GitHub for more details.
该库还在您的项目中添加了一些其他方便的 UI 功能——查看 GitHub 上的自述文件以了解更多详细信息。
I hope it helps!
我希望它有帮助!
回答by bitwit
One important note not yet mentioned here is that your UIColor should be in extended RGB space. Depending on how your color is originally created this function may return false if it's just RGB.
此处尚未提及的一项重要说明是,您的 UIColor 应位于扩展的 RGB 空间中。根据您的颜色最初是如何创建的,如果它只是 RGB,这个函数可能会返回 false。
Secondly, I made a variant on @ambientlight's answer to make the API just a little more slick. You can adjust 1 or all properties.
其次,我对@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
}
}
}
回答by Rajan Balana
You can use this method by setting the following values
您可以通过设置以下值来使用此方法
UIColor *customColor = [UIColor colorWithHue: x.xx saturation: x.xx brightness: x.xx alpha: 1.0];
UIColor *customColor = [UIColor colorWithHue: x.xx saturation: x.xx brightness: x.xx alpha: 1.0];
回答by Arvind Rajpurohit
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);
In the first two lines it describes image size and image rect. CGContextRef
is used to create a context in core graphics. After that the 5th line is the image you want to apply hue and rect of image. After that blend mode which is important. After that UI colorWithHue
in which values of hue, saturation, brightness and alpha are passed. To get a proper effect give 1.0 value of alpha. And at the end you should set uicolor.create
bezerpath or you can directly give cgcontextsetfill(context)
. At the end create imageref
and put that image in UIImage
. At the end, as always in choreographics, release CGImageRelease
to hedge from memory issues.
在前两行中,它描述了图像大小和图像矩形。CGContextRef
用于在核心图形中创建上下文。之后第 5 行是您要应用图像的色调和矩形的图像。之后的混合模式很重要。在该 UI 之后,colorWithHue
其中传递了色调、饱和度、亮度和 alpha 的值。要获得适当的效果,请提供 1.0 的 alpha 值。最后你应该设置uicolor.create
bezerpath 或者你可以直接给cgcontextsetfill(context)
. 最后创建imageref
并将该图像放入UIImage
. 最后,就像在编舞中一样,释放CGImageRelease
以对冲记忆问题。
回答by Black Maggie
There is a class function with colorWithHue:saturation:brightness:alpha:
有一个类函数 colorWithHue:saturation:brightness:alpha:
Of course you can first use getHue:saturation:brightness:alpha:
before making any changes when you init with [UIColor redColor]
当然,您可以getHue:saturation:brightness:alpha:
在初始化时先使用,然后再进行任何更改[UIColor redColor]
回答by iOS Developer
You can simply modify this by
您可以简单地修改它
[UIColor colorWithHue:YourHueValue saturation:YourSaturationValue brightness:YourBrightnessValueValue alpha:1.00];
alpha denotes the opaqueness of view and it ranges from 0.0 - 1.0
alpha 表示视图的不透明度,范围为 0.0 - 1.0
回答by zhoujinhao
UIColor can't change the saturation/hue/brigthness,you can you CoreImage.
UIColor 不能改变饱和度/色调/亮度,你可以改变 CoreImage。
Some sample code below:
下面的一些示例代码:
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]];
回答by Rahul
[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00];