iOS开发_取出UIColor上对应rgba的值

  • UIColor+Extension.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIColor (Extension)

@property(nonatomic, assign, readonly) CGFloat red;
@property(nonatomic, assign, readonly) CGFloat green;
@property(nonatomic, assign, readonly) CGFloat blue;
@property(nonatomic, assign, readonly) CGFloat alpha;

@end

NS_ASSUME_NONNULL_END
  • UIColor+Extension.m

#import "UIColor+Extension.h"

@implementation UIColor (Extension)

- (CGFloat)red {
    CGFloat r = 0, g, b, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return r;
}

- (CGFloat)green {
    CGFloat r, g = 0, b, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return g;
}

- (CGFloat)blue {
    CGFloat r, g, b = 0, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return b;
}

- (CGFloat)alpha {
    return CGColorGetAlpha(self.CGColor);
}

@end
posted @ 2022-12-08 17:49  CH520  阅读(38)  评论(0编辑  收藏  举报