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

iOS 获取UIColor对象的lab字符串值。

时间:2015-01-06 11:56:51      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:



- (NSString *)getCIELABString:(UIColor *)originColor  {

  // Method provided by the Colours class extension

  NSDictionary *cieDict = [self getCIE_LabArrayByColor:originColor ];

  return [NSString stringWithFormat:@"(%0.2f, %0.2f, %0.2f)",

          [cieDict[@"CIE_L"] floatValue],

          [cieDict[@"CIE_A"] floatValue],

          [cieDict[@"CIE_B"] floatValue]];

  

}


#pragma mark - LAB from Color

- (NSDictionary *)getCIE_LabArrayByColor:(UIColor *)originColor {

  // Convert Color to XYZ format first

  NSDictionary *rgba = [self getRGBDictionaryByColor:originColor];

  CGFloat R = [rgba[@"R"] floatValue];

  CGFloat G = [rgba[@"G"] floatValue];

  CGFloat B = [rgba[@"B"] floatValue];

  

  // Create deltaR block

  void (^deltaRGB)(CGFloat *R);

  deltaRGB = ^(CGFloat *r) {

    *r = (*r > 0.04045) ? pow((*r + 0.055)/1.055, 2.40) : (*r/12.92);

  };

  deltaRGB(&R);

  deltaRGB(&G);

  deltaRGB(&B);

  CGFloat X = R*41.24 + G*35.76 + B*18.05;

  CGFloat Y = R*21.26 + G*71.52 + B*7.22;

  CGFloat Z = R*1.93 + G*11.92 + B*95.05;

  

  // Convert XYZ to L*a*b*

  X = X/95.047;

  Y = Y/100.000;

  Z = Z/108.883;

  

  // Create deltaF block

  void (^deltaF)(CGFloat *f);

  deltaF = ^(CGFloat *f){

    *f = (*f > pow((6.0/29.0), 3.0)) ? pow(*f, 1.0/3.0) : (1/3)*pow((29.0/6.0), 2.0) * *f + 4/29.0;

  };

  deltaF(&X);

  deltaF(&Y);

  deltaF(&Z);

  NSNumber *L = @(116*Y - 16);

  NSNumber *a = @(500 * (X - Y));

  NSNumber *b = @(200 * (Y - Z));

  

  return @{@"CIE_L":L,

           @"CIE_A":a,

           @"CIE_B":b,

           @"CIE_ALPHA":rgba[@"A"]};

}


/**

 *  获取UIColor对象的RGB值。

 *

 *  @return 包含rgb值的字典对象。

 */

- (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对象的lab字符串值。

标签:

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

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