2

I have two colors here is a log of the instances:

(lldb) po acolor
UIDeviceRGBColorSpace 0.929412 0.133333 0.141176 1

(lldb) po hexColor
UIDeviceRGBColorSpace 0.929412 0.133333 0.141176 1

I have this code that works for iPhone 4s 5 but not for iPhone 6:

if ([acolor isEqual:hexColor])
{
    // other code here.
}

As additional I create this acolor from pixel of image:

CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;

But seems there is some differences for values between iPhone 4 and iPhone 6:

for example if I print red on iPhone 6 it has:

(lldb) po red
0.92941176470588238

and for iPhone 4:

(lldb) po red
0.929411768

The values are different because of 32 and 64 bits architecture as I think.

But as we see at colors they seems have right values and seems they rounded. but it never compare it with success. So acolor is not equal hexColor. Never. only for iPhone 4 and 5.

Of course I can use float type instead of CGFloat. But just noticed that rounding seems work and UIColors has the same values for different devices. But comparing does not work.

I get hexColor using this methods:

+ (UIColor *)colorWithHexString:(NSString *)hexString
{
    const char *cStr = [hexString cStringUsingEncoding:NSASCIIStringEncoding];
    long x = strtol(cStr, NULL, 16);
    return [UIColor colorWithHex:(UInt32)x];
}

+ (UIColor *)colorWithHex:(UInt32)col {

    unsigned char r, g, b;
    b = col & 0xFF;
    g = (col >> 8) & 0xFF;
    r = (col >> 16) & 0xFF;

    return [UIColor colorWithRed:(float)r/255.0f
                           green:(float)g/255.0f
                            blue:(float)b/255.0f
                           alpha:1];
}
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • How are defined `rawData`? – Larme Feb 25 '15 at 13:10
  • Did you try to use `- (BOOL)getRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue alpha:(CGFloat *)alpha` ? – KIDdAe Feb 25 '15 at 13:10
  • yep the problem goes from the raw data, and if we use cgfloat we have biggest value then float. but I did not compare red or green or blue values, as you can see log for colors are the same as for acolor as for hexColor, but it wont compare them. Maybe log also round values. Of course problem are in the type so I just need one for example or float or cgfloat, because we never get success for example with 0.92941176470588238 and it can't be = 0.929411768. But I am comparing UIColors and log says to me that they have the same values for RGBA . – Matrosov Oleksandr Feb 25 '15 at 13:20

1 Answers1

0

Did you try to round both to the same decimal count and then compare them? For example what happens if you round both 9 decimals?

float rounded = roundf (original * 1000000000) / 1000000000.0;

This piece of code is adapted from alastair's post in this question: Round a float value to two post decimal positions

Community
  • 1
  • 1
Phil Niedertscheider
  • 1,068
  • 11
  • 29
  • the problem is that I use float and cgfloat and it's correct for different architecture. but the colors shows me information in digital that everything seems are equal. so I am comparing UIColors not just raw data float value. – Matrosov Oleksandr Feb 25 '15 at 13:22