//
//  HexColor.swift
//  MusicPlayer
//
//  Created by Mr.Zhou on 2024/3/27.
//

import Foundation
//MARK: - Color扩展
extension UIColor {
    /// String -> UIColor
    convenience init(hex: String,alpha:CGFloat? = 1) {
        let string = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: string)
             
        if string.hasPrefix("#") {
            scanner.scanLocation = 1
        }
             
        var color: UInt32 = 0
        scanner.scanHexInt32(&color)
             
        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask
            
        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0
            
        self.init(red: red, green: green, blue: blue, alpha: alpha!)
    }
    /// UIColor -> String
        var hextoString: String? {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0
             
        let multiplier = CGFloat(255.999999)
            
        guard self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) else {
            return nil
        }
            
        if alpha == 1.0 {
            return String(
                format: "#%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier)
            )
        }
        else {
            return String(
                format: "#%02lX%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier),
                Int(alpha * multiplier)
            )
        }
    }
    //返回随机颜色
    class var randomColor:UIColor{
        get  {
            let  red =  CGFloat (arc4random()%256)/255.0
            let  green =  CGFloat (arc4random()%256)/255.0
            let  blue =  CGFloat (arc4random()%256)/255.0
            return  UIColor (red: red, green: green, blue: blue, alpha: 1.0)
        }
    }
}

