FLEXColor.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // FLEXColor.m
  3. // FLEX
  4. //
  5. // Created by Benny Wong on 6/18/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXColor.h"
  9. #import "FLEXUtility.h"
  10. #import <UIKit/UIInterface.h>
  11. /**
  12. tvOS notes: alot of these properties are marked as unavailable on tvOS, not only is that a bald faced lie, but without using these values the UI gets completely screwy!
  13. i initially tried to do some creative macro undef/redef but that didnt work, so going to use good ole KVC, valueForKey: works on class methods as well! and this will work
  14. perfectly without needing to patch another SDK file for this to build for tvOS
  15. */
  16. #if FLEX_AT_LEAST_IOS13_SDK
  17. #define FLEXDynamicColor(dynamic, static) ({ \
  18. UIColor *c; \
  19. if (@available(iOS 13.0, *)) { \
  20. c = [UIColor dynamic]; \
  21. } else { \
  22. c = [UIColor static]; \
  23. } \
  24. c; \
  25. });
  26. #else
  27. #define FLEXDynamicColor(dynamic, static) [UIColor static]
  28. #endif
  29. @implementation FLEXColor
  30. #pragma mark - Background Colors
  31. + (UIColor *)primaryBackgroundColor {
  32. return FLEXDynamicColor(valueForKey:@"systemBackgroundColor", whiteColor);
  33. }
  34. + (UIColor *)primaryBackgroundColorWithAlpha:(CGFloat)alpha {
  35. return [[self primaryBackgroundColor] colorWithAlphaComponent:alpha];
  36. }
  37. + (UIColor *)secondaryBackgroundColor {
  38. return FLEXDynamicColor(
  39. valueForKey:@"secondarySystemBackgroundColor",
  40. colorWithHue:2.0/3.0 saturation:0.02 brightness:0.97 alpha:1
  41. );
  42. }
  43. + (UIColor *)secondaryBackgroundColorWithAlpha:(CGFloat)alpha {
  44. return [[self secondaryBackgroundColor] colorWithAlphaComponent:alpha];
  45. }
  46. + (UIColor *)tertiaryBackgroundColor {
  47. // All the background/fill colors are varying shades
  48. // of white and black with really low alpha levels.
  49. // We use systemGray4Color instead to avoid alpha issues.
  50. return FLEXDynamicColor(valueForKey:@"systemGray4Color", lightGrayColor);
  51. }
  52. + (UIColor *)tertiaryBackgroundColorWithAlpha:(CGFloat)alpha {
  53. return [[self tertiaryBackgroundColor] colorWithAlphaComponent:alpha];
  54. }
  55. + (UIColor *)groupedBackgroundColor {
  56. return FLEXDynamicColor(
  57. valueForKey:@"systemGroupedBackgroundColor",
  58. colorWithHue:2.0/3.0 saturation:0.02 brightness:0.97 alpha:1
  59. );
  60. }
  61. + (UIColor *)groupedBackgroundColorWithAlpha:(CGFloat)alpha {
  62. return [[self groupedBackgroundColor] colorWithAlphaComponent:alpha];
  63. }
  64. + (UIColor *)secondaryGroupedBackgroundColor {
  65. return FLEXDynamicColor(valueForKey:@"secondarySystemGroupedBackgroundColor", whiteColor);
  66. }
  67. + (UIColor *)secondaryGroupedBackgroundColorWithAlpha:(CGFloat)alpha {
  68. return [[self secondaryGroupedBackgroundColor] colorWithAlphaComponent:alpha];
  69. }
  70. #pragma mark - Text colors
  71. + (UIColor *)primaryTextColor {
  72. return FLEXDynamicColor(labelColor, blackColor);
  73. }
  74. + (UIColor *)deemphasizedTextColor {
  75. return FLEXDynamicColor(valueForKey:@"secondaryLabelColor", lightGrayColor);
  76. }
  77. #pragma mark - UI Element Colors
  78. + (UIColor *)tintColor {
  79. #if FLEX_AT_LEAST_IOS13_SDK
  80. if (@available(iOS 13.0, *)) {
  81. return [UIColor valueForKey:@"systemBlueColor"];
  82. } else {
  83. #pragma clang diagnostic push
  84. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  85. return UIApplication.sharedApplication.keyWindow.tintColor;
  86. #pragma clang diagnostic pop
  87. }
  88. #else
  89. return UIApplication.sharedApplication.keyWindow.tintColor;
  90. #endif
  91. }
  92. + (UIColor *)scrollViewBackgroundColor {
  93. return FLEXDynamicColor(
  94. valueForKey:@"systemGroupedBackgroundColor",
  95. colorWithHue:2.0/3.0 saturation:0.02 brightness:0.95 alpha:1
  96. );
  97. }
  98. + (UIColor *)iconColor {
  99. return FLEXDynamicColor(labelColor, blackColor);
  100. }
  101. + (UIColor *)borderColor {
  102. return [self primaryBackgroundColor];
  103. }
  104. + (UIColor *)toolbarItemHighlightedColor {
  105. return FLEXDynamicColor(
  106. valueForKey:@"quaternaryLabelColor",
  107. colorWithHue:2.0/3.0 saturation:0.1 brightness:0.25 alpha:0.6
  108. );
  109. }
  110. + (UIColor *)toolbarItemSelectedColor {
  111. return FLEXDynamicColor(
  112. valueForKey:@"secondaryLabelColor",
  113. colorWithHue:2.0/3.0 saturation:0.1 brightness:0.25 alpha:0.68
  114. );
  115. }
  116. + (UIColor *)hairlineColor {
  117. return FLEXDynamicColor(valueForKey:@"systemGray3Color", colorWithWhite:0.75 alpha:1);
  118. }
  119. + (UIColor *)destructiveColor {
  120. return FLEXDynamicColor(valueForKey:@"systemRedColor", redColor);
  121. }
  122. @end