Is There Function to Convert Uicolor to Hue Saturation Brightness

Is there function to convert UIColor to Hue Saturation Brightness?

Use the UIColor method:
getHue:saturation:brightness:alpha:

From the Apple docs:

"Returns the components that make up the color in the HSB color space."

- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha

Example:

UIColor *testColor = [UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];

CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
BOOL success = [testColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"success: %i hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", success, hue, saturation, brightness, alpha);

NSLog output:

success: 1 hue: 0.10, saturation: 0.79, brightness: 0.53, alpha: 1.00

Here is a corrected version of the method provided by @WhiteTiger:

// Test values
CGFloat red = 0.53;
CGFloat green = 0.37;
CGFloat blue = 0.11;

CGFloat hue = 0;
CGFloat saturation = 0;
CGFloat brightness = 0;

CGFloat minRGB = MIN(red, MIN(green,blue));
CGFloat maxRGB = MAX(red, MAX(green,blue));

if (minRGB==maxRGB) {
hue = 0;
saturation = 0;
brightness = minRGB;
} else {
CGFloat d = (red==minRGB) ? green-blue : ((blue==minRGB) ? red-green : blue-red);
CGFloat h = (red==minRGB) ? 3 : ((blue==minRGB) ? 1 : 5);
hue = (h - d/(maxRGB - minRGB)) / 6.0;
saturation = (maxRGB - minRGB)/maxRGB;
brightness = maxRGB;
}
NSLog(@"hue: %0.2f, saturation: %0.2f, value: %0.2f", hue, saturation, brightness);

NSLog output:

hue: 0.10, saturation: 0.79, value: 0.53

How can I modify a UIColor's hue, brightness and saturation?

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:]

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

UIColor getHue:saturation:brightness:alpha: returns NO

It appears that the UIColor getHue:saturation:brightness: method doesn't work if the color's color space is sRGB but it does work if the color's color space is Extended sRGB.

So the solution is to update the selected Color Space for each of your colors in your color set asset.

This can be demonstrated in a Swift Playground as follows. This creates a color using the sRGB color space.

if let cs = CGColorSpace(name: CGColorSpace.sRGB) {
if let cc = CGColor(colorSpace: cs, components: [0.5, 0.7, 0.3, 1.0]) {
let color = UIColor(cgColor: cc)
print(color)
var h: CGFloat = 0
var s: CGFloat = 0
var b: CGFloat = 0
if color.getHue(&h, saturation: &s, brightness: &b, alpha: nil) {
print(h, s, b)
} else {
print("Failed with color space \(cs)")
}
}
}

This gives the output:

kCGColorSpaceModelRGB 0.5 0.7 0.3 1

Failed with color space (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1)

Updating the above code to use the CGColorSpace.extendedSRGB color space gives the following results:

UIExtendedSRGBColorSpace 0.5 0.7 0.3 1

0.25 0.571428571428571 0.7

Get hue value from a UIColor?

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.

UIColor with Hue

HSV, HSL, HSI and HSB are not interchangeable. To make matters worse, I believe that in some cases, there are different implementations of each representation that of course yield different results with converted to/from RGB, and, worse still, sometimes people think they are using HSL when in fact they are using HSV or some other such combo. So your best bet is to just keep trying until it works.

In your specific case, you need to find a way to convert from RGB to HSB (or HS*), and hope that your converter and the method of UIColor that you're calling do things the same way.

Also, 105/360 equals 0 in integer division, which is what you are using. Try using double values, e.g. 105.0/360.0 to get a double result.

UIColor saturation brightness values different from the regular saturation luminosity values of a color

A few things are going on here.

First, HSB == HSV, as discussed in this post.

Next, the HSB(V) color space and HSL color space are two different beasts. The following image can help visualize it & shows the values you are getting:
Sample Image

As to how to convert them:

Sample Image

What is the best/shortest way to convert a UIColor to hex (web color) in Swift?

Xcode 8.2 • Swift 3.0.2

Getting Red, Green, Blue and Alpha components from a UIColor:

extension UIColor {
var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
if getRed(&r, green: &g, blue: &b, alpha: &a) {
return (r,g,b,a)
}
return (0, 0, 0, 0)
}

// hue, saturation, brightness and alpha components from UIColor**
var hsba: (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat) {
var hue: CGFloat = 0, saturation: CGFloat = 0, brightness: CGFloat = 0, alpha: CGFloat = 0
if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) {
return (hue, saturation, brightness, alpha)
}
return (0,0,0,0)
}

var htmlRGB: String {
let rgbaCache = rgba
return String(format: "#%02x%02x%02x", Int(round(rgbaCache.red * 255)), Int(round(rgbaCache.green * 255)), Int(round(rgbaCache.blue * 255)))
}

var htmlRGBA: String {
let rgbaCache = rgba
return String(format: "#%02x%02x%02x%02x", Int(round(rgbaCache.red * 255)), Int(round(rgbaCache.green * 255)), Int(round(rgbaCache.blue * 255)), Int(round(rgbaCache.alpha * 255)) )
}
}

Testing it:

HTML Colors

let myWhiteWebColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).htmlRGBA      // #ffffffff
let myGreyWebColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1).htmlRGBA // #7f7f7fff
let myBlackWebColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).htmlRGBA // #000000ff
let myRedWebColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).htmlRGBA // #ff0000ff
let myGreenWebColor = UIColor(red: 0, green: 1, blue: 0, alpha: 1).htmlRGBA // #00ff00ff
let myBlueWebColor = UIColor(red: 0, green: 0, blue: 1, alpha: 1).htmlRGBA // #0000ffff

let myColorBlack = UIColor.black.htmlRGBA // #000000ff
let myLghtGrayColor = UIColor.lightGray.htmlRGBA // #aaaaaaff
let myDarkGrayColor = UIColor.darkGray.htmlRGBA // #555555ff

let myRedHueWebColor = UIColor(hue: 1, saturation: 1, brightness: 1, alpha: 1).htmlRGBA //ff0000ff

RGB Components

UIColor.red.rgba        // (1.0, 0.0, 0.0, 1.0)
UIColor.red.rgba.red // 1
UIColor.red.rgba.green // 0
UIColor.red.rgba.blue // 0
UIColor.red.rgba.alpha // 1

UIColor.green.rgba // (0.0, 1.0, 0.0, 1.0)
UIColor.green.rgba.red // 0
UIColor.green.rgba.green // 1
UIColor.green.rgba.blue // 0
UIColor.green.rgba.alpha // 1

UIColor.blue.rgba // (0.0, 0.0, 1.0, 1.0)
UIColor.blue.rgba.red // 0
UIColor.blue.rgba.green // 0
UIColor.blue.rgba.blue // 1
UIColor.blue.rgba.alpha // 1

Hue Components

UIColor.red.hsba               // (0.0, 1.0, 1.0, 1.0)
UIColor.red.hsba.hue // 1
UIColor.red.hsba.saturation // 1
UIColor.red.hsba.brightness // 1
UIColor.red.hsba.alpha // 1

UIColor.green.hsba // (0.333333333333333, 1.0, 1.0, 1.0)
UIColor.green.hsba.hue // 0.3333333333333333
UIColor.green.hsba.saturation // 1
UIColor.green.hsba.brightness // 1
UIColor.green.hsba.alpha // 1

UIColor.blue.hsba // (0.666666666666667, 1.0, 1.0, 1.0)
UIColor.blue.hsba.hue // 0.666666666666667
UIColor.blue.hsba.saturation // 1.0
UIColor.blue.hsba.brightness // 1.0
UIColor.blue.hsba.alpha // 1.0

UIColor.clear.hsba // (.0 0, .1 0, .2 0, .3 0)

UIColor.blue.htmlRGB //"#0000ff"
UIColor.blue.htmlRGBA //"#0000ffff"

UIColor conversion from RGB to HSV , set brightness to UIColor

+(struct hsv_color)HSVfromRGB:(struct rgb_color)rgb
{
struct hsv_color hsv;

CGFloat rgb_min, rgb_max;
rgb_min = MIN3(rgb.r, rgb.g, rgb.b);
rgb_max = MAX3(rgb.r, rgb.g, rgb.b);

if (rgb_max == rgb_min) {
hsv.hue = 0;
} else if (rgb_max == rgb.r) {
hsv.hue = 60.0f * ((rgb.g - rgb.b) / (rgb_max - rgb_min));
hsv.hue = fmodf(hsv.hue, 360.0f);
} else if (rgb_max == rgb.g) {
hsv.hue = 60.0f * ((rgb.b - rgb.r) / (rgb_max - rgb_min)) + 120.0f;
} else if (rgb_max == rgb.b) {
hsv.hue = 60.0f * ((rgb.r - rgb.g) / (rgb_max - rgb_min)) + 240.0f;
}
hsv.val = rgb_max;
if (rgb_max == 0) {
hsv.sat = 0;
} else {
hsv.sat = 1.0 - (rgb_min / rgb_max);
}

return hsv;
}


Related Topics



Leave a reply



Submit