How to get RGB values from UIColor?

后端 未结 15 1605
孤城傲影
孤城傲影 2020-11-28 22:27

I\'m creating a color object using the following code.

curView.backgroundColor = [[UIColor alloc] initWithHue:229 saturation:40 brightness:75 alpha:1];


        
相关标签:
15条回答
  • 2020-11-28 23:17

    Hopefully this will be helpful

    CGFloat red, green, blue, alpha;
    
    //Create a sample color
    
    UIColor *redColor = [UIColor redColor];
    
    //Call 
    
    [redColor getRed: &red 
      green: &green
      blue: &blue 
      alpha: &alpha];
    NSLog(@"red = %f. Green = %f. Blue = %f. Alpha = %f",
      red,
      green,
      blue,
      alpha);
    
    0 讨论(0)
  • 2020-11-28 23:17

    The top voted answer is outdated:

    error: :3:1: error: 'CGColorGetComponents' has been replaced by property 'CGColor.components'

    Instead, use

    myUIColor.cgColor.components

    0 讨论(0)
  • 2020-11-28 23:18

    You can use CIColor components (swift 5)

    let ciColor = CIColor(color: backgroundColor)
    let alpha = ciColor.alpha
    let red = ciColor.red
    let blue = ciColor.blue
    let green = ciColor.green
    

    this works for non-RGB color space too

    0 讨论(0)
  • 2020-11-28 23:22
    const CGFloat *colors = CGColorGetComponents( curView.backgroundColor.CGColor );
    

    These links provide further details:

    • UIColor Reference
    • CGColorGetComponents reference
    0 讨论(0)
  • 2020-11-28 23:25

    Some useful macros I've made for this and other color controls:

    In your case you would just use

    getRGBA(myColor, red, green, blue, alpha);
    
    NSLog(@"Red Value: %f", red);
    NSLog(@"Blue Value: %f", green);
    NSLog(@"Green Value: %f", blue);
    

    Macros:

    #define rgba(r,g,b,a) [UIColor colorWithRed:((float)(r))/255.0f green:((float)(g))/255.0f blue:((float)(b))/255.0f alpha:a]
    #define rgb(r,g,b) rgba(r, g, b, 1.0f)
    
    #define rgbaf(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:a]
    #define rgbf(r,g,b) rgbaf(r, g, b, 1.0f)
    
    #define rgba_fromColor(__color, __r, __g, __b, __a) \
    CGFloat __r, __g, __b, __a;\
    UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
    [__unpackedColor getRed:&__r green:&__g blue:&__b alpha:&__a];
    #define getRGBA(__color, __r, __g, __b, __a) hsba_fromColor(__color, __r, __g, __b, __a)
    
    #define getRed(__color)  (\
    (^float (void){\
    rgba_fromColor(__color, r, g, b, a);\
    return r;\
    })()\
    )
    
    #define getGreen(__color)  (\
    (^float (void){\
    rgba_fromColor(__color, r, g, b, a);\
    return g;\
    })()\
    )
    
    #define getBlue(__color)  (\
    (^float (void){\
    rgba_fromColor(__color, r, g, b, a);\
    return b;\
    })()\
    )
    
    #define getAlpha(__color)  (\
    (^float (void){\
    rgba_fromColor(__color, r, g, b, a);\
    return a;\
    })()\
    )
    
    
    
    
    
    
    
    
    
    
    #define hsba(h,s,b,a) [UIColor colorWithHue:((float)(h))/360.0f saturation:((float)(s))/100.0f brightness:((float)(b))/100.0f alpha:a]
    #define hsb(h,s,b) hsba(h, s, b, 1.0f)
    
    #define hsbaf(h,s,b,a) [UIColor colorWithHue:(h) saturation:(s) brightness:(b) alpha:a]
    #define hsbf(h,s,b) rgbaf(h, s, b, 1.0f)
    
    #define hsba_fromColor(__color, __h, __s, __b, __a) \
    CGFloat __h, __s, __b, __a;\
    UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
    [__unpackedColor getHue:&__h saturation:&__s brightness:&__b alpha:&__a];
    #define getHSBA(__color, __h, __s, __b, __a) rgba_fromColor(__color, __h, __s, __b, __a)
    
    #define getHue(__color)  (\
    (^float (void){\
    hsba_fromColor(__color, h, s, b, a);\
    return h;\
    })()\
    )
    
    #define getSaturation(__color)  (\
    (^float (void){\
    hsba_fromColor(__color, h, s, b, a);\
    return s;\
    })()\
    )
    
    #define getBrightness(__color)  (\
    (^float (void){\
    hsba_fromColor(__color, h, s, b, a);\
    return b;\
    })()\
    )
    
    /*
    ///already defined in RGBA macros
    #define getAlpha(__color)  (\
    (^float (void){\
    hsba_fromColor(__color, h, s, b, a);\
    return a;\
    })()\
    )
    */
    
    0 讨论(0)
  • 2020-11-28 23:26

    Since iOS 2.0 there is a private instance method on UIColor called styleString which returns an RGB or RGBA string representation of the color, even for colors like whiteColor outside the RGB space.

    Objective-C:

    @interface UIColor (Private)
    
    - (NSString *)styleString;
    
    @end
    
    // ...
    
    [[UIColor whiteColor] styleString]; // rgb(255,255,255)
    [[UIColor redColor] styleString]; // rgb(255,0,0)
    [[UIColor lightTextColor] styleString]; // rgba(255,255,255,0.600000)
    

    In Swift you could use a bridging header to expose the interface. With pure Swift, you will need to create an @objc protocol with the private method, and unsafeBitCast UIColor with the protocol:

    @objc protocol  UIColorPrivate {
        func styleString() -> String
    }
    
    let white = UIColor.whiteColor()
    let red = UIColor.redColor()
    let lightTextColor = UIColor.lightTextColor()
    
    let whitePrivate = unsafeBitCast(white, UIColorPrivate.self)
    let redPrivate = unsafeBitCast(red, UIColorPrivate.self)
    let lightTextColorPrivate = unsafeBitCast(lightTextColor, UIColorPrivate.self)
    
    whitePrivate.styleString() // rgb(255,255,255)
    redPrivate.styleString() // rgb(255,0,0)
    lightTextColorPrivate.styleString() // rgba(255,255,255,0.600000)
    
    0 讨论(0)
提交回复
热议问题