<commit_before>//
//  UIColorExtensions.swift
//  SwifterSwift
//
//  Created by <NAME> on 8/6/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

// MARK: - Properties
public extension UIColor {
	
	/// SwifterSwift: Red component of UIColor (read-only).
	public var redComponent: Int {
		var red: CGFloat = 0
		getRed(&red, green: nil, blue: nil, alpha: nil)
		return Int(red * 255)
	}
	
	/// SwifterSwift: Green component of UIColor (read-only).
	public var greenComponent: Int {
		var green: CGFloat = 0
		getRed(nil, green: &green, blue: nil, alpha: nil)
		return Int(green * 255)
	}
	
	/// SwifterSwift: blue component of UIColor (read-only).
	public var blueComponent: Int {
		var blue: CGFloat = 0
		getRed(nil, green: nil, blue: &blue, alpha: nil)
		return Int(blue * 255)
	}
	
	/// SwifterSwift: Alpha of UIColor (read-only).
	public var alpha: CGFloat {
		var a: CGFloat = 0
		getRed(nil, green: nil, blue: nil, alpha: &a)
		return a
	}
	
	/// SwifterSwift: Hexadecimal value string (read-only).
	public var hexString: String {
		var red:	CGFloat = 0
		var green:	CGFloat = 0
		var blue:	CGFloat = 0
		var alpha:	CGFloat = 0
		
		getRed(&red, green: &green, blue: &blue, alpha: &alpha)
		let rgb: Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0
		return NSString(format:"#%06x", rgb).uppercased as String
	}
	
	/// SwifterSwift: Short hexadecimal value string (read-only, if applicable).
	public var shortHexString: String? {
		let string = hexString.replacing("#", with: "")
		guard string[0] == string[1]
			&& string[2] == string[3]
			&& string[4] == string[5] else {
				return nil
		}
		guard let first = string[0], let second = string[2], let third = string[5] else {
			return nil
		}
		return  "#" + first + second + third
	}
	
	/// SwifterSwift: Short hexadecimal value string, or full hexadecimal string if not possible (read-only).
	public var shortHexOrHexString: String {
		return shortHexString ?? hexString
	}
	
	/// SwifterSwift: Random color.
	public static var random: UIColor {
		let r = Int(arc4random_uniform(255))
		let g = Int(arc4random_uniform(255))
		let b = Int(arc4random_uniform(255))
		return UIColor(red: r, green: g, blue: b)
		
	}
	
}


// MARK: - Methods
public extension UIColor {
	
	/// SwifterSwift: Blend two UIColors
	///
	/// - Parameters:
	///   - color1: first color to blend
	///   - intensity1: intensity of first color (default is 0.5)
	///   - color2: second color to blend
	///   - intensity2: intensity of second color (default is 0.5)
	/// - Returns: UIColor created by blending first and seond colors.
	public static func blend(_ color1: UIColor, intensity1: CGFloat = 0.5, with color2: UIColor, intensity2: CGFloat = 0.5) -> UIColor {
		// http://stackoverflow.com/questions/27342715/blend-uicolors-in-swift
		let total = intensity1 + intensity2
		let l1 = intensity1/total
		let l2 = intensity2/total
		guard l1 > 0 else { return color2}
		guard l2 > 0 else { return color1}
		var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
		var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
		color1.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
		color2.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
		return UIColor(red: l1*r1 + l2*r2, green: l1*g1 + l2*g2, blue: l1*b1 + l2*b2, alpha: l1*a1 + l2*a2)
	}
	
}


// MARK: - Initializers
public extension UIColor {
	
	/// SwifterSwift: Create UIColor from hexadecimal value with optional transparency.
	///
	/// - Parameters:
	///   - hex: hex Int (example: 0xDECEB5).
	///   - transparency: optional transparency value (default is 1).<commit_msg>feat: add static UIColor method to get complementary of given color<commit_after>//
//  UIColorExtensions.swift
//  SwifterSwift
//
//  Created by <NAME> on 8/6/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

// MARK: - Properties
public extension UIColor {
	
	/// SwifterSwift: Red component of UIColor (read-only).
	public var redComponent: Int {
		var red: CGFloat = 0
		getRed(&red, green: nil, blue: nil, alpha: nil)
		return Int(red * 255)
	}
	
	/// SwifterSwift: Green component of UIColor (read-only).
	public var greenComponent: Int {
		var green: CGFloat = 0
		getRed(nil, green: &green, blue: nil, alpha: nil)
		return Int(green * 255)
	}
	
	/// SwifterSwift: blue component of UIColor (read-only).
	public var blueComponent: Int {
		var blue: CGFloat = 0
		getRed(nil, green: nil, blue: &blue, alpha: nil)
		return Int(blue * 255)
	}
	
	/// SwifterSwift: Alpha of UIColor (read-only).
	public var alpha: CGFloat {
		var a: CGFloat = 0
		getRed(nil, green: nil, blue: nil, alpha: &a)
		return a
	}
	
	/// SwifterSwift: Hexadecimal value string (read-only).
	public var hexString: String {
		var red:	CGFloat = 0
		var green:	CGFloat = 0
		var blue:	CGFloat = 0
		var alpha:	CGFloat = 0
		
		getRed(&red, green: &green, blue: &blue, alpha: &alpha)
		let rgb: Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0
		return NSString(format:"#%06x", rgb).uppercased as String
	}
	
	/// SwifterSwift: Short hexadecimal value string (read-only, if applicable).
	public var shortHexString: String? {
		let string = hexString.replacing("#", with: "")
		guard string[0] == string[1]
			&& string[2] == string[3]
			&& string[4] == string[5] else {
				return nil
		}
		guard let first = string[0], let second = string[2], let third = string[5] else {
			return nil
		}
		return  "#" + first + second + third
	}
	
	/// SwifterSwift: Short hexadecimal value string, or full hexadecimal string if not possible (read-only).
	public var shortHexOrHexString: String {
		return shortHexString ?? hexString
	}
	
	/// SwifterSwift: Random color.
	public static var random: UIColor {
		let r = Int(arc4random_uniform(255))
		let g = Int(arc4random_uniform(255))
		let b = Int(arc4random_uniform(255))
		return UIColor(red: r, green: g, blue: b)
		
	}
	
}


// MARK: - Methods
public extension UIColor {
	
	/// SwifterSwift: Blend two UIColors
	///
	/// - Parameters:
	///   - color1: first color to blend
	///   - intensity1: intensity of first color (default is 0.5)
	///   - color2: second color to blend
	///   - intensity2: intensity of second color (default is 0.5)
	/// - Returns: UIColor created by blending first and seond colors.
	public static func blend(_ color1: UIColor, intensity1: CGFloat = 0.5, with color2: UIColor, intensity2: CGFloat = 0.5) -> UIColor {
		// http://stackoverflow.com/questions/27342715/blend-uicolors-in-swift
		let total = intensity1 + intensity2
		let l1 = intensity1/total
		let l2 = intensity2/total
		guard l1 > 0 else { return color2}
		guard l2 > 0 else { return color1}
		var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
		var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
		color1.getRed(&r1, green: &g1, blue: &b1, alpha: &a1)
		color2.getRed(&r2, green: &g2, blue: &b2, alpha: &a2)
		return UIColor(red: l1*r1 + l2*r2, green: l1*g1 + l2*g2, blue: l1*b1 + l2*b2, alpha: l1*a1 + l2*a2)
	}
    
    /// SwifterSwift: Blend two UIColors
    ///
    /// - Parameters:
    ///   - color: color of which opposite color is desired
    /// - Returns: UIColor initialized from the corresponding complementary color.
    public static func getComplementary(forColor color: UIColor) -> UIColor {
        let componentColors = color.cgColor.components
        let r: CGFloat = sqrt(pow(255.0, 2.0) - pow((componentColors![0]*255), 2.0))/255
        let g: CGFloat = sqrt(pow(255.0, 2.0) - pow((componentColors![1]*255), 2.0))/255
        let b: CGFloat = sqrt(pow(255.0, 2.0) - pow((componentColors![2]*255), 2.0))/255
        return UIColor(red: r, green: g, blue: b, alpha: 1.0)
    }
	
}


// MARK: - Initializers
public extension UIColor {
	
	/// SwifterSwift: Create UIColor from hexadecimal value with optional transparency.
	///
	/// - Parameters:
	///   - hex: hex Int (example: 0xDECEB5).
	///   - transparency: optional transparency value (default is 1).
{'fraction_non_alphanumeric': 0.1416448271558717, 'fraction_numerical': 0.04130787470360664, 'mean_word_length': 3.180490349504434, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4430736', 'n_tokens_mistral': 3212, 'n_tokens_neox': 2891, 'n_words': 890}