user2651293
user2651293

Reputation: 65

How to convert UIColor to Android color (packed int)?

I have gotten pretty far in mobile development figuring things out on my own, but I'm having a lot of trouble figuring this one out...

I am converting Android color (packed int) to UIColor using this macro:

#define ANDROID_COLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:((c)&0xFF)/255.0  alpha:((c>>24)&0xFF)/255.0]

However I also need to convert a UIColor to Android colour. Any help is very much appreciated!

Upvotes: 2

Views: 1908

Answers (1)

rmaddy
rmaddy

Reputation: 318854

Something like this (untested) should work:

UIColor *color =  // your color
CGFloat red, green, blue, alpha;

if (![color getRed:&red green:&green blue:&blue alpha:&alpha]) {

    CGFloat white;
    if ([color getWhite:&white alpha:&alpha]) {
        red = green = blue = white;
    } else {
        NSLog(@"Uh oh, not RGB or greyscale");
    }
}

long r = red * 255;
long g = green * 255;
long b = blue * 255;
long a = alpha * 255;
long packed_color = (r << 16) | (g << 8) | (b) | (a << 24);

That appears to be the reverse of what you showed in your question.

Upvotes: 8

Related Questions