Advertisement
AhmedBafkir

Make DarkMode for any app

Oct 30th, 2018
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //@Peacefull_0
  2. //this UIColor+AhmedBafkir.h
  3.  
  4. #import <UIKit/UIKit.h>
  5.  
  6. @interface UIColor (CompareAndHex)
  7.  
  8. -(bool)isSameColor:(UIColor*)other;
  9.  
  10. - (instancetype)initWithHex:(NSString *)hexString;
  11.  
  12. @end
  13.  
  14. // —++++
  15.  
  16. //this UIColor+AhmedBafkir.m
  17.  
  18. #import "UIColor+AhmedBafkir.h"
  19.  
  20. @implementation UIColor (CompareAndHex)
  21.  
  22. -(bool)isSameColor:(UIColor*)other
  23. {
  24.     CGFloat fLHS[4];
  25.     CGFloat fRHS[4];
  26.  
  27.     [self getRed:&fLHS[0] green:&fLHS[1] blue:&fLHS[2] alpha:&fLHS[3]];
  28.     [other getRed:&fRHS[0] green:&fRHS[1] blue:&fRHS[2] alpha:&fRHS[3]];
  29.  
  30.     // reduce rounding errors - convert all into int for compare
  31.     for( int i=0;i<4;i++ )
  32.     {
  33.         if( ((int)(fLHS[i]*255))!=((int)(fRHS[i]*255)) )
  34.             return false;
  35.     }
  36.     return true;
  37. }
  38.  
  39. - (instancetype)initWithHex:(NSString *)hexString {
  40.     NSString *noHashString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
  41.     NSScanner *scanner = [NSScanner scannerWithString:noHashString];
  42.     [scanner setCharactersToBeSkipped:[NSCharacterSet symbolCharacterSet]];
  43.    
  44.     unsigned hex;
  45.     if (![scanner scanHexInt:&hex]) return nil;
  46.     int r = (hex >> 16) & 0xFF;
  47.     int g = (hex >> 8) & 0xFF;
  48.     int b = (hex) & 0xFF;
  49.    
  50.     return [self initWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f];
  51. }
  52.  
  53. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement