fcanas / ios-color-picker

A color picker for iOS
MIT License
119 stars 27 forks source link

Unable to manually set the location of the crosshairs/loop based on color #4

Closed Lenrocexe closed 10 years ago

Lenrocexe commented 10 years ago

I use the method colorPickerWithColor:delegate to create the picker but the loop does not appear near the given color and is rendered off screen. Apparently the x-position is not calculated correctly due to an incorrect Hue value. (on line 133)

The formula expects a Hue value between 0.0 and 1.0 however the actual value used is the one that UIColor+Utilities returns, a value between 0 and 360.

This causes the actual resulting point to be something like (68955, 101.65)

Example:

Given the color

 [UIColor colorWithHue:0.69 saturation:0.65 brightness:0.65 alpha:1.0]

Then this is the result of the formula (248.40 is the hue value)

xpos = (248.40 * 278.00) + 11.00 = 69066.20

While I expect: (Got 0.69 using [_color getHue:&currentHue saturation:nil brightness:nil alpha:nil])

xpos = (0.69 * 278.00) + 11.00 = 202.82
fcanas commented 10 years ago

Are you using CocoaPods to manage the dependency on UIColor+Utilities? In the CocoaPods version of UIColor+Utilities, this seems to be a problem with targeting versions of iOS below 5.0. For versions 5.0 and above, UIColor+Utilities uses the built-in, and correct version of -colorWithHue:saturation:brightness:alpha:.

Are you targeting iOS 4, or have to not increased your deployment target? The latest version of the color picker does not support iOS 4 due to significant differences in ARC capabilities.

Lenrocexe commented 10 years ago

I currently do not use CocoaPods yet but I downloaded the source from your UIColor+Utilities repo. Also I do not support iOS 4. Minimum target is 5.0.

I checked some more and noticed that the hue call is not passed to UIColor+HSV which I thought it did but instead to UIColor+Expanded.

This not only happens with the hue call but also saturation and brightness. They all call the method [UIColor(Expanded) hue:saturation:brightness:alpha:] which in turn performs it's own conversion using [UIColor(Expanded) red:green:blue:toHue:saturation:brightness:].

fcanas commented 10 years ago

There are a lot of UIColor+Expanded repositories out there. The one this project references via CocoaPods to conform to semantic versioning) defines that method in a conditional macro so it is only compiled when targeting iOS < 5. It is definitely not canonical, and I only contributed a few minor bug fixes. UIColor-Utilities

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 50000
+ (UIColor *)colorWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha {

    // Convert hsb to rgb
    CGFloat r,g,b;
    [self hue:hue saturation:saturation brightness:brightness toRed:&r green:&g blue:&b];

    // Create a color with rgb
    return [self colorWithRed:r green:g blue:b alpha:alpha];
}
#endif

I recommend switching to a version of UIColor+Utilities that performs this check.

Lenrocexe commented 10 years ago

I looked around and tried the most recently updated fork of UIColor+Utilities and it worked perfectly fine. Should have tried that first before complaining. Thank you very much for your support.

I'm also going to try and integrate CocoaPods into my project so that I can avoid these kind of issues in the future.