测试已测试 | ✓ |
语言语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2014年12月 |
由 Daniel L. Alves 维护。
依赖于 | |
NitroMath | ~> 1.1.0 |
NitroNSDictionaryCategories | ~> 1.1.0 |
NitroUIColorCategories 为 iOS 的 UIColor
类型提供了初始化、转换、比较、RGBA 组件和亮度获取器。
1) 初始化
UIColor *opaqueRed = [UIColor colorWithByteRed: 255
byteGreen: 0
byteBlue: 0];
UIColor *semiTransparentRed = [UIColor colorWithByteRed: 255
byteGreen: 0
byteBlue: 0
byteAlpha: 128];
// With hexadecimal numbers
UIColor *opaqueBlue = [UIColor colorFromRGBHex: 0x0000FF];
UIColor *semiTransparentBlue = [UIColor colorFromRGBAHex: 0x0000FF80];
UIColor *blueWithAlphaFirst = [UIColor colorFromARGBHex: 0xFF0000FF];
// With hexadecimal strings
UIColor *opaqueGreen = [UIColor colorFromRGBHexString: @"0x00FF00"];
UIColor *semiTransparentGreen = [UIColor colorFromRGBAHexString: @"0x00FF0080"];
UIColor *greenWithAlphaFirst = [UIColor colorFromARGBHexString: @"0xFF00FF00"];
// ... which don't really need the '0x' prefix
UIColor *opaqueMagenta = [UIColor colorFromRGBHexString: @"FF00FF"];
UIColor *semiTransparentMagenta = [UIColor colorFromRGBAHexString: @"FF00FF80"];
UIColor *magentaWithAlphaFirst = [UIColor colorFromARGBHexString: @"FFFF00FF"];
// With dictionaries (Ruby? Anyone?)
UIColor *opaqueCyanWithFloats = [UIColor colorFromColorComponentsDictionary: @{
COLOR_DICT_COMPONENT_KEY_GREEN: @( 1.0f ),
COLOR_DICT_COMPONENT_KEY_BLUE: @( 1.0f ),
COLOR_DICT_COMPONENT_KEY_ALPHA: @( 1.0f )
}];
UIColor *transparentYellowWithBytes = [UIColor colorFromColorByteComponentsDictionary: @{
COLOR_DICT_COMPONENT_KEY_RED: @( 255 ),
COLOR_DICT_COMPONENT_KEY_GREEN: @( 255 )
}];
2) 转换
// To hexadecimal numbers
UIColor *red = [UIColor redColor];
uint32_t rgba = red.toRGBAHex; // Will be 0xFF0000FF
uint32_t argb = red.toARGBHex; // Will be 0xFFFF0000
// To hexadecimal strings
UIColor *blue = [UIColor blueColor];
NSString *rgbString = blue.toRGBHexString; // Will be @"0x0000FF"
NSString *argbString = blue.toARGBHexString; // Will be @"0xFF0000FF"
NSString *rgbaString = blue.toRGBAHexString; // Will be @"0x0000FFFF"
// To dictionaries
UIColor *transparentWhite = [UIColor colorWithWhite: 1.0f alpha: 0.0f];
// Will be @{ COLOR_DICT_COMPONENT_KEY_RED: @( 1.0f ),
// COLOR_DICT_COMPONENT_KEY_GREEN: @( 1.0f ),
// COLOR_DICT_COMPONENT_KEY_BLUE: @( 1.0f ),
// COLOR_DICT_COMPONENT_KEY_ALPHA: @( 0.0f ) }
NSDictionary *components = transparentWhite.toColorComponentsDictionary;
// Will be @{ COLOR_DICT_COMPONENT_KEY_RED: @( 255 ),
// COLOR_DICT_COMPONENT_KEY_GREEN: @( 255 ),
// COLOR_DICT_COMPONENT_KEY_BLUE: @( 255 ),
// COLOR_DICT_COMPONENT_KEY_ALPHA: @( 0 ) }
NSDictionary *byteComponents = transparentWhite.toColorByteComponentsDictionary;
3) 比较
// Compare colors by their components
UIColor *white = [UIColor whiteColor];
[white componentsAreEqualToComponentsOfColor: [UIColor colorWithByteRed: 255
byteGreen: 255
byteBlue: 255]]; // YES
[white componentsAreEqualToComponentsOfColor: [UIColor colorWithRed: 1.0f
green: 1.0f
blue: 1.0f
alpha: 1.0f]]; // YES
[white componentsAreEqualToComponentsOfColor: [UIColor colorWithWhite: 1.0f
alpha: 1.0f]]; // YES
[white componentsAreEqualToComponentsOfColor: [UIColor colorWithByteRed: 255
byteGreen: 255
byteBlue: 255
byteAlpha: 0]]; // NO
[white componentsAreEqualToComponentsOfColor: [UIColor colorWithWhite: 1.0f
alpha: 0.0f]]; // NO
// ...
4) RGBA 组件获取器
// Get the color red component as byte or float
UIColor *color = /* Any color */;
CGFloat redComponent = color.red;
uint8_t byteRedComponent = color.byteRed;
// Get the color green component as byte or float
CGFloat greenComponent = color.green;
uint8_t byteGreenComponent = color.byteGreen;
// Get the color blue component as byte or float
CGFloat blueComponent = color.blue;
uint8_t byteBlueComponent = color.byteBlue;
// Get the color alpha component as byte or float
CGFloat alphaComponent = color.alpha;
uint8_t byteAlphaComponent = color.byteAlpha;
5) 亮度获取器
// Get the color luminance as byte or float
UIColor *color = /* Any color */;
CGFloat luminance = color.luminance;
uint8_t byteLuminance = color.byteLuminance;
6) 工具
// Helper macros to create colors RGBA or ARGB hexadecimal values
uint32_t magentaHex = RGBA_TO_HEX( 255, 0, 255, 255 ); // Will be 0xFF00FFFF
uint32_t magentaHexWithAlphaFirst = ARGB_TO_HEX( 255, 255, 0, 255 ); // Will be 0xFFFF00FF
iOS 4.3 或更高版本,仅限 ARC
NitroUIColorCategories 可通过 CocoaPods 安装,只需将以下行添加到您的 Podfile 中
pod "NitroUIColorCategories"
NitroUIColorCategories 会为使用它的目标添加 -ObjC
链接器标志。如果没有它,分类代码将被打包,导致链接器错误。有关静态库中分类的更多信息,请参见:使用分类构建 Objective-C 静态库
NitroUIColorCategories 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。