1

I am using the following code to change hue of 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);

The code works fine, i'm just trying to find a way to get the correct NSNumber values of inputAngle for the specific colors.

so can i maybe:

  1. Get the value by converting a [UIColor colorWithRed:0.89 green:0.718 blue:0.102 alpha:1.0]
  2. or maybe just use the UIColor in some way ?
  3. or is there any list with the specific numbers for each color ?

The docs says:

CIHueAdjust

Thanks in advance

Fouad
  • 399
  • 1
  • 7
  • 19

1 Answers1

3

This post might answer your question:

Is there function to convert UIColor to Hue Saturation Brightness?

The angle should be the hue value you get there.

Here you find some information on how the angle has to be understood:

iOS: Values for CIFilter (Hue) from Photoshop

EDIT

Here is some example code based on yours:

First let's define the color you want to filter (for your inputAngle)

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

Then we determine the hue value of that color (that's the actual inputAngle)

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

This is your code (unchanged)

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"];

Here we apply the filter using the determined hue value of the chosen color

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

This is your code (unchanged)

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

Hope this fits your needs.

Community
  • 1
  • 1
Shingoo
  • 826
  • 10
  • 26
  • I actually went through these questions while searching, but i didn't get to solve my issue – Fouad Jun 10 '14 at 16:52
  • I have changed my whole code and used `CIColorMonochrome` of `CIFiler` instead `CIHueAdjust` to change images color, but your answer is great, and pretty sure will help others, thank you! – Fouad Jun 13 '14 at 08:01