从UIColor获取色调值?

时间:2014-06-10 16:11:19

标签: ios objective-c cocoa-touch uiimage nsnumber

我使用以下代码更改UIImage的色调

UIImage *image = [UIImage imageNamed:@"Image.png"];

// Create a Core Image version of the image.
CIImage *sourceCore = [CIImage imageWithCGImage:[image CGImage]];

// Apply a CIHueAdjust filter
CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
[hueAdjust setDefaults];
[hueAdjust setValue: sourceCore forKey: @"inputImage"];
[hueAdjust setValue: [NSNumber numberWithFloat: 1.0f] forKey: @"inputAngle"];
CIImage *resultCore = [hueAdjust valueForKey: @"outputImage"];

// Convert the filter output back into a UIImage.
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef resultRef = [context createCGImage:resultCore fromRect:[resultCore extent]];

UIImage *result = [UIImage imageWithCGImage:resultRef];
CGImageRelease(resultRef);

代码工作正常,我只想找到一种方法来获取特定颜色的inputAngle的正确NSNumber值。

我也许可以:

  1. 转换[UIColor colorWithRed:0.89 green:0.718 blue:0.102 alpha:1.0]
  2. 获取价值
  3. 或者只是以某种方式使用UIColor?
  4. 或者是否有任何列表中包含每种颜色的具体数字?
  5. 文档说:

    CIHueAdjust

    提前致谢

1 个答案:

答案 0 :(得分:2)

这篇文章可能会回答你的问题:

Is there function to convert UIColor to Hue Saturation Brightness?

角度应该是你到达的色调值。

在这里您可以找到有关如何理解角度的一些信息:

iOS: Values for CIFilter (Hue) from Photoshop

修改

以下是一些基于您的示例代码:

首先让我们定义你想要过滤的颜色(对于你的inputAngle)

UIColor *myColor = [UIColor redColor]; // The color you want to filter

然后我们确定该颜色的色调值(即实际的inputAngle)

CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
[myColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];

这是你的代码(未更改)

UIImage *image = [UIImage imageNamed:@"Image.png"];

// Create a Core Image version of the image.
CIImage *sourceCore = [CIImage imageWithCGImage:[image CGImage]];

// Apply a CIHueAdjust filter
CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
[hueAdjust setDefaults];
[hueAdjust setValue: sourceCore forKey: @"inputImage"];

这里我们使用所选颜色的确定色调值来应用滤镜

[hueAdjust setValue: [NSNumber numberWithFloat: hue] forKey: @"inputAngle"];

这是你的代码(未更改)

CIImage *resultCore = [hueAdjust valueForKey: @"outputImage"];

// Convert the filter output back into a UIImage.
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef resultRef = [context createCGImage:resultCore fromRect:[resultCore extent]];

UIImage *result = [UIImage imageWithCGImage:resultRef];
CGImageRelease(resultRef);

希望这符合您的需求。