RDSRgbColor.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // RDSRgbColor.m
  3. // LampRibbon
  4. //
  5. // Created by coderYK on 2017/8/24.
  6. // Copyright © 2017年 RD. All rights reserved.
  7. //
  8. #import "RDSRgbColor.h"
  9. @implementation RDSRgbColor
  10. + (instancetype)rds_rgbColorWithRgbArr:(NSArray *)array {
  11. DDLog(@"%@",array);
  12. RDSRgbColor *color = [[RDSRgbColor alloc] init];
  13. // color.red_hex = array[0];
  14. // color.green_hex = array[1];
  15. // color.blue_hex = array[2];
  16. color.red = [array[0] intValue];
  17. color.green = [array[1] intValue];
  18. color.blue = [array[2] intValue];
  19. return color;
  20. }
  21. + (instancetype)rds_colorWithR:(int)red G:(int)green B:(int)blue {
  22. RDSRgbColor *color = [[RDSRgbColor alloc] init];
  23. color.red = red;
  24. color.green = green;
  25. color.blue = blue;
  26. return color;
  27. }
  28. + (instancetype)rds_colorWithRgbHexString:(NSString *)rgb_hex {
  29. RDSRgbColor *color = [[RDSRgbColor alloc] init];
  30. color.red = (int)[[rgb_hex substringWithRange:NSMakeRange(0, 2)] rds_uintegerValueFromHexString];
  31. color.green = (int)[[rgb_hex substringWithRange:NSMakeRange(2, 2)] rds_uintegerValueFromHexString];
  32. color.blue = (int)[[rgb_hex substringWithRange:NSMakeRange(4, 2)] rds_uintegerValueFromHexString];
  33. return color;
  34. }
  35. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  36. if (self = [super init]) {
  37. self.red = [aDecoder decodeIntForKey:@"red"];
  38. self.green = [aDecoder decodeIntForKey:@"green"];
  39. self.blue = [aDecoder decodeIntForKey:@"blue"];
  40. }
  41. return self;
  42. }
  43. - (void)encodeWithCoder:(NSCoder *)aCoder {
  44. [aCoder encodeInt:self.red forKey:@"red"];
  45. [aCoder encodeInt:self.green forKey:@"green"];
  46. [aCoder encodeInt:self.blue forKey:@"blue"];
  47. }
  48. - (id)copyWithZone:(NSZone *)zone {
  49. RDSRgbColor *copyColor = [[[self class] allocWithZone:zone] init];
  50. copyColor.red = self.red;
  51. copyColor.green = self.green;
  52. copyColor.blue = self.blue;
  53. return copyColor;
  54. }
  55. - (NSString *)description {
  56. return [NSString stringWithFormat:@"red:%d, green:%d, blue:%d", _red, _green, _blue];
  57. }
  58. #pragma mark - get
  59. - (NSString *)red_hex {
  60. return [NSString stringWithFormat:@"%02x", self.red];
  61. }
  62. - (NSString *)green_hex {
  63. return [NSString stringWithFormat:@"%02x", self.green];
  64. }
  65. - (NSString *)blue_hex {
  66. return [NSString stringWithFormat:@"%02x", self.blue];
  67. }
  68. - (NSString *)rgb_hex {
  69. return [NSString stringWithFormat:@"%02x%02x%02x", self.red, self.green, self.blue];
  70. }
  71. - (int)brightness {
  72. int temp = 0;
  73. temp = self.red > self.green ? self.red : self.green;
  74. temp = temp > self.blue ? temp : self.blue;
  75. CGFloat persent = temp/255.0;
  76. return persent*100;
  77. }
  78. - (void)setBrightness:(int)brightness {
  79. CGFloat oriPersent = self.brightness/100.0;
  80. CGFloat persent = brightness/100.0;
  81. self.red = ((self.red/oriPersent)>255 ? 255 : self.red/oriPersent)*persent;
  82. self.green = ((self.green/oriPersent)>255 ? 255 : self.green/oriPersent)*persent;
  83. self.blue = ((self.blue/oriPersent)>255 ? 255 : self.blue/oriPersent)*persent;
  84. }
  85. @end