react-native-navigation的迁移库

Color+Interpolation.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Color+Interpolation.m
  3. //
  4. // Created by Leo Natan on 01/10/2016.
  5. // Copyright © 2016 Leo Natan. All rights reserved.
  6. //
  7. #if __has_include(<UIKit/UIKit.h>) || __has_include(<AppKit/AppKit.h>)
  8. #import "Color+Interpolation.h"
  9. #if __has_include(<UIKit/UIKit.h>)
  10. #define Color UIColor
  11. #else
  12. #define Color NSColor
  13. #endif
  14. #define SWAP(x, y) do { __typeof(x) __ZZZZ__SWAP = x; x = y; y = __ZZZZ__SWAP; } while(0)
  15. //Same value as LNInterpolationBehaviorUseDefault
  16. LNInterpolationBehavior const LNInterpolationBehaviorUseLABColorSpace = @"LNInterpolationBehaviorUseDefault";
  17. LNInterpolationBehavior const LNInterpolationBehaviorUseRGBColorSpace = @"LNInterpolationBehaviorUseRGB";
  18. extern double LNLinearInterpolate(double from, double to, double p);
  19. static NSArray<NSNumber*>* LNRGBComponentsFromColor(Color* color)
  20. {
  21. size_t numberOfComponents = CGColorGetNumberOfComponents(color.CGColor);
  22. const CGFloat* components = CGColorGetComponents(color.CGColor);
  23. return numberOfComponents == 2 ? @[@(components[0]), @(components[0]), @(components[0]), @(components[1])] : @[@(components[0]), @(components[1]), @(components[2]), @(components[3])];
  24. }
  25. static Color* LNColorFromRGBComponents(NSArray<NSNumber*>* components)
  26. {
  27. return [Color colorWithRed:components[0].doubleValue green:components[1].doubleValue blue:components[2].doubleValue alpha:components[3].doubleValue];
  28. }
  29. static NSArray<NSNumber*>* LNLabComponentsFromColor(Color* color)
  30. {
  31. NSArray<NSNumber*>* rgbComponents = LNRGBComponentsFromColor(color);
  32. CGFloat r = rgbComponents[0].doubleValue;
  33. CGFloat g = rgbComponents[1].doubleValue;
  34. CGFloat b = rgbComponents[2].doubleValue;
  35. //RGB -> XYZ
  36. //http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html
  37. r = (r > 0.04045) ? pow((r + 0.055) / 1.055, 2.4) : (r / 12.92);
  38. g = (g > 0.04045) ? pow((g + 0.055) / 1.055, 2.4) : (g / 12.92);
  39. b = (b > 0.04045) ? pow((b + 0.055) / 1.055, 2.4) : (b / 12.92);
  40. //http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html (sRGB -> XYZ)
  41. CGFloat x = r * 41.24564 + g * 35.75761 + b * 18.04375;
  42. CGFloat y = r * 21.26729 + g * 71.51522 + b * 07.21750;
  43. CGFloat z = r * 01.93339 + g * 11.91920 + b * 95.03041;
  44. //XYZ -> Lab
  45. //http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_Lab.html
  46. static const CGFloat eps = 216.0 / 24389.0;
  47. static const CGFloat k = 24389.0 / 27.0;
  48. x = x / 100.0;//95.047;
  49. y = y / 100.0;//100.000;
  50. z = z / 100.0;//108.883;
  51. x = x > eps ? pow(x, 1.0 / 3.0) : (k * x + 16.0) / 116.0;
  52. y = y > eps ? pow(y, 1.0 / 3.0) : (k * y + 16.0) / 116.0;
  53. z = z > eps ? pow(z, 1.0 / 3.0) : (k * z + 16.0) / 116.0;
  54. CGFloat l = 116 * y - 16;
  55. CGFloat a = 500 * (x - y);
  56. b = 200 * (y - z);
  57. return @[@(l), @(a), @(b), rgbComponents[3]];
  58. }
  59. static Color* LNColorFromLabComponents(NSArray<NSNumber*>* components)
  60. {
  61. CGFloat l = components[0].doubleValue;
  62. CGFloat a = components[1].doubleValue;
  63. CGFloat b = components[2].doubleValue;
  64. //Lab -> XYZ
  65. //http://www.brucelindbloom.com/index.html?Eqn_Lab_to_XYZ.html
  66. static const CGFloat eps = 216.0 / 24389.0;
  67. static const CGFloat k = 24389.0 / 27.0;
  68. CGFloat y = (l + 16.0) / 116.0;
  69. CGFloat x = a / 500 + y;
  70. CGFloat z = y - b / 200;
  71. x = pow(x, 3.0) > eps ? pow(x, 3.0) : (116 * x - 16) / k;
  72. y = l > k * eps ? pow((l + 16) / 116, 3.0) : l / k;
  73. z = pow(z, 3.0) > eps ? pow(z, 3.0) : (116 * z - 16) / k;
  74. x = x * 1.0;//.95047;
  75. y = y * 1.0;//1.00000;
  76. z = z * 1.0;//1.08883;
  77. //XYZ -> RGB
  78. //http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html (XYZ -> sRGB)
  79. CGFloat r = x * 3.2404542 + y * -1.5371385 + z * -0.4985314;
  80. CGFloat g = x * -0.9692660 + y * 1.8760108 + z * 0.0415560;
  81. b = x * 0.0556434 + y * -0.2040259 + z * 1.0572252;
  82. r = r <= 0.0031308 ? 12.92 * r : 1.055 * pow(r, 1.0 / 2.4) - 0.055;
  83. g = g <= 0.0031308 ? 12.92 * g : 1.055 * pow(g, 1.0 / 2.4) - 0.055;
  84. b = b <= 0.0031308 ? 12.92 * b : 1.055 * pow(b, 1.0 / 2.4) - 0.055;
  85. // return Color
  86. return LNColorFromRGBComponents(@[@(r), @(g), @(b), components[3]]);
  87. }
  88. static inline __attribute__((__always_inline__)) Color* LNInterpolateColor(Color* fromValue, Color* toValue, CGFloat p, NSArray* (*compConverter)(Color*), Color* (*colorConverter)(NSArray*))
  89. {
  90. NSArray<NSNumber*>* arrayC1 = compConverter(fromValue);
  91. NSArray<NSNumber*>* arrayC2 = compConverter(toValue);
  92. NSMutableArray<NSNumber*>* arrayOutput = [NSMutableArray new];
  93. [arrayC1 enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  94. arrayOutput[idx] = @(LNLinearInterpolate(obj.doubleValue, arrayC2[idx].doubleValue, p));
  95. }];
  96. return colorConverter(arrayOutput);
  97. }
  98. @implementation Color (Interpolation)
  99. - (instancetype)interpolateToValue:(Color*)toValue progress:(double)p
  100. {
  101. return [self interpolateToValue:toValue progress:p behavior:LNInterpolationBehaviorUseDefault];
  102. }
  103. - (instancetype)interpolateToValue:(id)toValue progress:(double)p behavior:(LNInterpolationBehavior)behavior
  104. {
  105. NSParameterAssert([toValue isKindOfClass:[Color class]]);
  106. if(p <= 0)
  107. {
  108. return self;
  109. }
  110. if(p >= 1)
  111. {
  112. return toValue;
  113. }
  114. return LNInterpolateColor(self, toValue, p, behavior == LNInterpolationBehaviorUseRGBColorSpace ? LNRGBComponentsFromColor : LNLabComponentsFromColor, behavior == LNInterpolationBehaviorUseRGBColorSpace ? LNColorFromRGBComponents : LNColorFromLabComponents);
  115. }
  116. @end
  117. #endif