【问题标题】:Get hue value from a UIColor?从 UIColor 获取色调值?
【发布时间】:2014-06-10 16:11:19
【问题描述】:

我正在使用下面的代码来改变 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. 或者只是以某种方式使用 UIColor ?
  3. 或者有没有每种颜色的具体数字的列表?

文档说:

提前致谢

【问题讨论】:

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


    【解决方案1】:

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

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

    希望这符合您的需求。

    【讨论】:

    • 我实际上在搜索时遇到了这些问题,但我没有解决我的问题
    • 我已经更改了整个代码并使用CIFiler 中的CIColorMonochrome 代替CIHueAdjust 来更改图像颜色,但是您的回答很棒,并且肯定会对其他人有所帮助,谢谢!跨度>
    猜你喜欢
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2021-04-06
    • 2019-03-30
    • 1970-01-01
    • 2017-05-03
    相关资源
    最近更新 更多