码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 获取UIColor对象的rgb值。

时间:2015-01-06 12:04:09      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:




/**

 *  获取UIColor对象的rgb值。

 *

 *  @param originColor

 *

 *  @return

 */

- (NSString *)getHexStringByColor:(UIColor *)originColor

{

  NSDictionary *colorDic = [self getRGBDictionaryByColor:originColor];

  int r = [colorDic[@"R"] floatValue] * 255;

  int g = [colorDic[@"G"] floatValue] * 255;

  int b = [colorDic[@"B"] floatValue] * 255;

  NSString *red = [NSString stringWithFormat:@"%02x", r];

  NSString *green = [NSString stringWithFormat:@"%02x", g];

  NSString *blue = [NSString stringWithFormat:@"%02x", b];

  

  return [NSString stringWithFormat:@"#%@%@%@", red, green, blue];

}


- (NSDictionary *)getRGBDictionaryByColor:(UIColor *)originColor

{

  CGFloat r=0,g=0,b=0,a=0;

  if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) {

    [originColor getRed:&r green:&g blue:&b alpha:&a];

  }

  else {

    const CGFloat *components = CGColorGetComponents(originColor.CGColor);

    r = components[0];

    g = components[1];

    b = components[2];

    a = components[3];

  }

  

  return @{@"R":@(r),

           @"G":@(g),

           @"B":@(b),

           @"A":@(a)};

}


iOS 获取UIColor对象的rgb值。

标签:

原文地址:http://blog.csdn.net/shouqiangwei/article/details/42454837

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!