如何修改UIColor的色调,亮度和饱和度?

时间:2013-03-15 09:10:03

标签: iphone ios xcode uikit uicolor

假设我有一个UIColor

UIColor *color = [UIColor redColor];

现在我想修改饱和度/色调/亮度,我该怎么做? 我确实阅读了文档,但我仍然很困惑

我想修改我制作的UIColor([UIColor redColor])不会使用某些首选项启动新颜色。如何修改它保留原件。我知道colorWithHue:saturation:brightness:alpha:方法,我需要更新现有颜色的属性,保持红色。

10 个答案:

答案 0 :(得分:20)

您可以在颜色上调用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 ] ;

答案 1 :(得分:5)

以下是您可能会觉得有用的快速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(&currentHue, saturation: &currentSaturation, brightness: &currentBrigthness, alpha: &currentAlpha){
            return UIColor(hue: currentHue + hue,
                           saturation: currentSaturation + additionalSaturation,
                           brightness: currentBrigthness + additionalBrightness,
                           alpha: currentAlpha)
        } else {
            return self
        }
    }
}

答案 2 :(得分:2)

不幸的是,默认更改UIColor 的任何hsbargba非常麻烦。使用 HandyUIKit (通过Carthage安装)可让您的生活更轻松

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以获取更多详细信息。

我希望它有所帮助!

答案 3 :(得分:1)

此处尚未提及的一个重要说明是,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(&currentHue, saturation: &currentSaturation, brightness: &currentBrigthness, alpha: &currentAlpha) {
            return UIColor(hue: currentHue + hue,
                       saturation: currentSaturation + saturation,
                       brightness: currentBrigthness + brightness,
                       alpha: currentAlpha)
        } else {
            return self
        }
    }
}

答案 4 :(得分:0)

您可以通过设置以下值来使用此方法

UIColor *customColor = [UIColor colorWithHue: x.xx saturation: x.xx brightness: x.xx alpha: 1.0];

答案 5 :(得分:0)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIColor_Class/Reference/Reference.html

有一个colorWithHue:saturation:brightness:alpha:

的类函数

当您使用getHue:saturation:brightness:alpha:初始化时,您可以先使用[UIColor redColor]进行任何更改

答案 6 :(得分: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]];

如果您对iOS有任何疑问,可以转到http://weibo.com/iOSHomePage 和私信给我。我完全有能力回应。

答案 7 :(得分:0)

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);

在前两行中,它描述了图像大小和图像rect。 CGContextRef用于在核心图形中创建上下文。之后,第5行是您要应用hue和rect图像的图像。在那种混合模式之后这很重要。在那个UI colorWithHue之后,其中传递了色调,饱和度,亮度和alpha的值。为了获得正确的效果,给出1.0的alpha值。最后你应该设置uicolor.create bezerpath,或者你可以直接给cgcontextsetfill(context)。最后创建imageref并将该图片放入UIImage。最后,与编排一样,发布CGImageRelease以对冲内存问题。

答案 8 :(得分:-1)

您可以通过

简单地修改它
[UIColor colorWithHue:YourHueValue saturation:YourSaturationValue brightness:YourBrightnessValueValue alpha:1.00];

alpha表示视图的不透明度,范围为0.0 - 1.0

答案 9 :(得分:-4)

[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00];