//
//  RDSRgbColor.m
//  LampRibbon
//
//  Created by coderYK on 2017/8/24.
//  Copyright © 2017年 RD. All rights reserved.
//

#import "RDSRgbColor.h"

@implementation RDSRgbColor

+ (instancetype)rds_rgbColorWithRgbArr:(NSArray *)array {
    
    DDLog(@"%@",array);
    RDSRgbColor *color = [[RDSRgbColor alloc] init];
//    color.red_hex = array[0];
//    color.green_hex = array[1];
//    color.blue_hex = array[2];
    
    color.red = [array[0] intValue];
    color.green = [array[1] intValue];
    color.blue = [array[2] intValue];
    
    return color;
}

+ (instancetype)rds_colorWithR:(int)red G:(int)green B:(int)blue {
    
    RDSRgbColor *color = [[RDSRgbColor alloc] init];
    color.red = red;
    color.green = green;
    color.blue = blue;
    
    return color;
}

+ (instancetype)rds_colorWithRgbHexString:(NSString *)rgb_hex {
    
    RDSRgbColor *color = [[RDSRgbColor alloc] init];
    color.red = (int)[[rgb_hex substringWithRange:NSMakeRange(0, 2)] rds_uintegerValueFromHexString];
    color.green = (int)[[rgb_hex substringWithRange:NSMakeRange(2, 2)] rds_uintegerValueFromHexString];
    color.blue = (int)[[rgb_hex substringWithRange:NSMakeRange(4, 2)] rds_uintegerValueFromHexString];
    return color;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    if (self = [super init]) {
        self.red = [aDecoder decodeIntForKey:@"red"];
        self.green = [aDecoder decodeIntForKey:@"green"];
        self.blue = [aDecoder decodeIntForKey:@"blue"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    
    [aCoder encodeInt:self.red forKey:@"red"];
    [aCoder encodeInt:self.green forKey:@"green"];
    [aCoder encodeInt:self.blue forKey:@"blue"];
}

- (id)copyWithZone:(NSZone *)zone {
    
    RDSRgbColor *copyColor = [[[self class] allocWithZone:zone] init];
    copyColor.red = self.red;
    copyColor.green = self.green;
    copyColor.blue = self.blue;
    return copyColor;
}

- (NSString *)description {
    
    return [NSString stringWithFormat:@"red:%d, green:%d, blue:%d", _red, _green, _blue];
}

#pragma mark - get
- (NSString *)red_hex {
    return [NSString stringWithFormat:@"%02x", self.red];
}

- (NSString *)green_hex {
    return [NSString stringWithFormat:@"%02x", self.green];
}

- (NSString *)blue_hex {
    return [NSString stringWithFormat:@"%02x", self.blue];
}

- (NSString *)rgb_hex {
    return [NSString stringWithFormat:@"%02x%02x%02x", self.red, self.green, self.blue];
}

- (int)brightness {
    
    int temp = 0;
    temp = self.red > self.green ? self.red : self.green;
    temp = temp > self.blue ? temp : self.blue;
    CGFloat persent = temp/255.0;
    return persent*100;
}

- (void)setBrightness:(int)brightness {
    
    CGFloat oriPersent = self.brightness/100.0;
    CGFloat persent = brightness/100.0;
    self.red = ((self.red/oriPersent)>255 ? 255 : self.red/oriPersent)*persent;
    self.green = ((self.green/oriPersent)>255 ? 255 : self.green/oriPersent)*persent;
    self.blue = ((self.blue/oriPersent)>255 ? 255 : self.blue/oriPersent)*persent;
}

@end
