酷店

UIColor+KDPColor.m 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // UIColor+KDPColor.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "UIColor+KDPColor.h"
  9. @implementation UIColor (KDPColor)
  10. + (UIColor*)gradientOneColor:(UIColor*)oneColor toColor:(UIColor*)twoColor Width:(int)width
  11. {
  12. CGSize size = CGSizeMake(width, 44);
  13. UIGraphicsBeginImageContextWithOptions(size, NO, 0);
  14. CGContextRef context = UIGraphicsGetCurrentContext();
  15. CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  16. NSArray* colors = [NSArray arrayWithObjects:(id)oneColor.CGColor, (id)twoColor.CGColor, nil];
  17. CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (__bridge CFArrayRef)colors, NULL);
  18. CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(size.width, 0), 0);
  19. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  20. CGGradientRelease(gradient);
  21. CGColorSpaceRelease(colorspace);
  22. UIGraphicsEndImageContext();
  23. return [UIColor colorWithPatternImage:image];
  24. }
  25. +(instancetype)colorWithHex:(uint32_t)hex
  26. {
  27. uint8_t r = (hex & 0xff0000) >> 16;
  28. uint8_t g = (hex & 0x00ff00) >> 8;
  29. uint8_t b = hex & 0x0000ff;
  30. return [self colorWithRed:r green:g blue:b];
  31. }
  32. +(instancetype)colorWithRed:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue
  33. {
  34. return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
  35. }
  36. + (instancetype)baseColor{
  37. return [self colorWithHex:0xFF235F];
  38. }
  39. @end