Advertisement
Guest User

AdaptiveLabels

a guest
Nov 4th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <SpringBoard/SpringBoard.h>
  2. #import <AVFoundation/AVFoundation.h>
  3. #import <UIKit/UIKit.h>
  4. #import <UIKit/UIColor.h>
  5.  
  6. %hook SBIconView
  7.  
  8. %new
  9. -(NSDictionary*)mainColoursInImage:(UIImage *)_iconImageView detail:(int)detail {
  10.  
  11.     float dimension = 10;
  12.     float flexibility = 2;
  13.     float range = 60;
  14.    
  15.  
  16.     if (detail==0){
  17.         dimension = 4;
  18.         flexibility = 1;
  19.         range = 100;
  20.        
  21.  
  22.     } else if (detail==2){
  23.         dimension = 100;
  24.         flexibility = 10;
  25.         range = 20;
  26.     }
  27.    
  28.  
  29.     NSMutableArray * colours = [NSMutableArray new];
  30.     CGImageRef imageRef = [_iconImageView CGImage];
  31.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  32.     unsigned char *rawData = (unsigned char*) calloc(dimension * dimension * 4, sizeof(unsigned char));
  33.     NSUInteger bytesPerPixel = 4;
  34.     NSUInteger bytesPerRow = bytesPerPixel * dimension;
  35.     NSUInteger bitsPerComponent = 8;
  36.     CGContextRef context = CGBitmapContextCreate(rawData, dimension, dimension, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  37.     CGColorSpaceRelease(colorSpace);
  38.     CGContextDrawImage(context, CGRectMake(0, 0, dimension, dimension), imageRef);
  39.     CGContextRelease(context);
  40.    
  41.     float x = 0;
  42.     float y = 0;
  43.     for (int n = 0; n<(dimension*dimension); n++){
  44.        
  45.         int index = (bytesPerRow * y) + x * bytesPerPixel;
  46.         int red   = rawData[index];
  47.         int green = rawData[index + 1];
  48.         int blue  = rawData[index + 2];
  49.         int alpha = rawData[index + 3];
  50.         NSArray * a = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%i",red],[NSString stringWithFormat:@"%i",green],[NSString stringWithFormat:@"%i",blue],[NSString stringWithFormat:@"%i",alpha], nil];
  51.         [colours addObject:a];
  52.        
  53.         y++;
  54.         if (y==dimension){
  55.             y=0;
  56.             x++;
  57.         }
  58.     }
  59.     free(rawData);
  60.    
  61.  
  62.     NSArray * copyColours = [NSArray arrayWithArray:colours];
  63.     NSMutableArray * flexibleColours = [NSMutableArray new];
  64.    
  65.     float flexFactor = flexibility * 2 + 1;
  66.     float factor = flexFactor * flexFactor * 3;
  67.     for (int n = 0; n<(dimension * dimension); n++){
  68.        
  69.         NSArray * pixelColours = copyColours[n];
  70.         NSMutableArray * reds = [NSMutableArray new];
  71.         NSMutableArray * greens = [NSMutableArray new];
  72.         NSMutableArray * blues = [NSMutableArray new];
  73.        
  74.         for (int p = 0; p<3; p++){
  75.            
  76.             NSString * rgbStr = pixelColours[p];
  77.             int rgb = [rgbStr intValue];
  78.            
  79.             for (int f = -flexibility; f<flexibility+1; f++){
  80.                 int newRGB = rgb+f;
  81.                 if (newRGB<0){
  82.                     newRGB = 0;
  83.                 }
  84.                 if (p==0){
  85.                     [reds addObject:[NSString stringWithFormat:@"%i",newRGB]];
  86.                 } else if (p==1){
  87.                     [greens addObject:[NSString stringWithFormat:@"%i",newRGB]];
  88.                 } else if (p==2){
  89.                     [blues addObject:[NSString stringWithFormat:@"%i",newRGB]];
  90.                 }
  91.             }
  92.         }
  93.        
  94.         int r = 0;
  95.         int g = 0;
  96.         int b = 0;
  97.         for (int k = 0; k<factor; k++){
  98.            
  99.             int red = [reds[r] intValue];
  100.             int green = [greens[g] intValue];
  101.             int blue = [blues[b] intValue];
  102.            
  103.             NSString * rgbString = [NSString stringWithFormat:@"%i,%i,%i",red,green,blue];
  104.             [flexibleColours addObject:rgbString];
  105.            
  106.             b++;
  107.             if (b==flexFactor){ b=0; g++; }
  108.             if (g==flexFactor){ g=0; r++; }
  109.         }
  110.     }
  111.  
  112.  
  113.    
  114.     NSMutableDictionary * colourCounter = [NSMutableDictionary new];
  115.    
  116.  
  117.     NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:flexibleColours];
  118.     for (NSString *item in countedSet) {
  119.         NSUInteger count = [countedSet countForObject:item];
  120.         [colourCounter setValue:[NSNumber numberWithInteger:count] forKey:item];
  121.     }
  122.    
  123.  
  124.     NSArray *orderedKeys = [colourCounter keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
  125.         return [obj2 compare:obj1];
  126.     }];
  127.    
  128.  
  129.     NSMutableArray * ranges = [NSMutableArray new];
  130.     for (NSString * key in orderedKeys){
  131.         NSArray * rgb = [key componentsSeparatedByString:@","];
  132.         int r = [rgb[0] intValue];
  133.         int g = [rgb[1] intValue];
  134.         int b = [rgb[2] intValue];
  135.         bool exclude = false;
  136.         for (NSString * ranged_key in ranges){
  137.             NSArray * ranged_rgb = [ranged_key componentsSeparatedByString:@","];
  138.            
  139.             int ranged_r = [ranged_rgb[0] intValue];
  140.             int ranged_g = [ranged_rgb[1] intValue];
  141.             int ranged_b = [ranged_rgb[2] intValue];
  142.            
  143.             if (r>= ranged_r-range && r<= ranged_r+range){
  144.                 if (g>= ranged_g-range && g<= ranged_g+range){
  145.                     if (b>= ranged_b-range && b<= ranged_b+range){
  146.                         exclude = true;
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.        
  152.         if (!exclude){ [ranges addObject:key]; }
  153.     }
  154.    
  155.  
  156.     NSMutableArray * colourArray = [NSMutableArray new];
  157.     for (NSString * key in ranges){
  158.         NSArray * rgb = [key componentsSeparatedByString:@","];
  159.         float r = [rgb[0] floatValue];
  160.         float g = [rgb[1] floatValue];
  161.         float b = [rgb[2] floatValue];
  162.         UIColor * colour = [UIColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0f];
  163.         [colourArray addObject:colour];
  164.     }
  165.    
  166.  
  167.    
  168.    
  169.  
  170.     NSMutableDictionary * temp = [NSMutableDictionary new];
  171.     float totalCount = 0.0f;
  172.     for (NSString * rangeKey in ranges){
  173.         NSNumber * count = colourCounter[rangeKey];
  174.         totalCount += [count intValue];
  175.         temp[rangeKey]=count;
  176.     }
  177.    
  178.  
  179.     NSMutableDictionary * colourDictionary = [NSMutableDictionary new];
  180.     for (NSString * key in temp){
  181.         float count = [temp[key] floatValue];
  182.         float percentage = count/totalCount;
  183.         NSArray * rgb = [key componentsSeparatedByString:@","];
  184.         float r = [rgb[0] floatValue];
  185.         float g = [rgb[1] floatValue];
  186.         float b = [rgb[2] floatValue];
  187.         UIColor * colour = [UIColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1.0f];
  188.         colourDictionary[colour]=[NSNumber numberWithFloat:percentage];
  189.     }
  190.    
  191.     return colourDictionary;
  192.    
  193. }
  194. %end
  195.  
  196. %hook UILabel
  197.  
  198. -(void)layoutSubviews {
  199.     self.textColor = [UIColor colour];
  200. }
  201.  
  202. %end
  203.  
  204. %hook SBUILegibilityLabel
  205. -(void)setOptions:(long long)arg1{
  206.     %orig(arg1 = 0);
  207. }
  208. %end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement