将UIColor转换为RGB值

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //将UIColor转换为RGB值  
  2. - (NSMutableArray *) changeUIColorToRGB:(UIColor *)color  
  3. {  
  4.     NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];  
  5.     NSString *RGBStr = nil;  
  6.     //获得RGB值描述  
  7.     NSString *RGBValue = [NSString stringWithFormat:@"%@",color];  
  8.     //将RGB值描述分隔成字符串  
  9.     NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];  
  10.     //获取红色值  
  11.     int r = [[RGBArr objectAtIndex:1] intValue] * 255;  
  12.     RGBStr = [NSString stringWithFormat:@"%d",r];  
  13.     [RGBStrValueArr addObject:RGBStr];  
  14.     //获取绿色值  
  15.     int g = [[RGBArr objectAtIndex:2] intValue] * 255;  
  16.     RGBStr = [NSString stringWithFormat:@"%d",g];  
  17.     [RGBStrValueArr addObject:RGBStr];  
  18.     //获取蓝色值  
  19.     int b = [[RGBArr objectAtIndex:3] intValue] * 255;  
  20.     RGBStr = [NSString stringWithFormat:@"%d",b];  
  21.     [RGBStrValueArr addObject:RGBStr];  
  22.     //返回保存RGB值的数组  
  23.     return [RGBStrValueArr autorelease];  
  24. }  



[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //16进制颜色(html颜色值)字符串转为UIColor  
  2. +(UIColor *) hexStringToColor: (NSString *) stringToConvert  
  3. {  
  4.      NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];  
  5.      // String should be 6 or 8 characters  
  6.      
  7.      if ([cString length] < 6return [UIColor blackColor];  
  8.      // strip 0X if it appears  
  9.      if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];  
  10.      if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];  
  11.      if ([cString length] != 6return [UIColor blackColor];  
  12.      
  13.      // Separate into r, g, b substrings  
  14.      
  15.      NSRange range;  
  16.      range.location = 0;  
  17.      range.length = 2;  
  18.      NSString *rString = [cString substringWithRange:range];  
  19.      range.location = 2;  
  20.      NSString *gString = [cString substringWithRange:range];  
  21.      range.location = 4;  
  22.      NSString *bString = [cString substringWithRange:range];  
  23.      // Scan values  
  24.      unsigned int r, g, b;  
  25.      
  26.      [[NSScanner scannerWithString:rString] scanHexInt:&r];  
  27.      [[NSScanner scannerWithString:gString] scanHexInt:&g];  
  28.      [[NSScanner scannerWithString:bString] scanHexInt:&b];  
  29.      
  30.      return [UIColor colorWithRed:((float) r / 255.0f)  
  31.                                  green:((float) g / 255.0f)  
  32.                                    blue:((float) b / 255.0f)  
  33.                                  alpha:1.0f];  
  34. }  

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (NSMutableArray *) changeUIColorToRGB:(UIColor *)color  
  2. {  
  3.     size_t  n = CGColorGetNumberOfComponents(color.CGColor);  
  4.     const CGFloat *rgba = CGColorGetComponents(color.CGColor);  
  5.     NSMutableArray *resultArr = [NSMutableArray arrayWithCapacity:n];  
  6.     for (int i=0; i<n; i++)   
  7.     {  
  8.         [resultArr addObject:[NSNumber numberWithFloat:rgba]];  
  9.     }  
  10.     return resultArr;  
  11. }   

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. 【OBJC类扩展之Hex值颜色转换】UIColor+Hex  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. @interface UIColor (Hex)  
  5.   
  6. + (UIColor *)colorWithHex:(long)hexColor;  
  7. + (UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity;  
  8.   
  9. @end  
  10.   
  11.   
  12. #import "UIColor+Hex.h"  
  13.   
  14. @implementation UIColor (Hex)  
  15.   
  16. + (UIColor*) colorWithHex:(long)hexColor;  
  17. {  
  18.     return [UIColor colorWithHex:hexColor alpha:1.];      
  19. }  
  20.   
  21. + (UIColor *)colorWithHex:(long)hexColor alpha:(float)opacity  
  22. {  
  23.     float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;  
  24.     float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;  
  25.     float blue = ((float)(hexColor & 0xFF))/255.0;  
  26.     return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];    
  27. }     
  28.   
  29. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值