//
//  colorManager.swift
//  wallpaper_project
//
//  Created by 忆海16 on 2024/3/11.
//
import UIKit
import Foundation

var screenWidth = UIScreen.main.bounds.size.width
var screenHeight = UIScreen.main.bounds.size.height

extension UIColor{
    
    ///转hex值
    ///
    class func hexSting(color:String,alpha:CGFloat)->UIColor{
        //删除字符串中的kongge
        var cString:String = color.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        if cString.count < 6{
            return UIColor.white
        }
        if cString.hasPrefix("OX") {
            let temstr:NSString = cString as NSString
            cString = temstr.substring(from: 2)as String
        }
        if cString.hasPrefix("#") {
            let temstr:NSString = cString as NSString
            cString = temstr.substring(from: 1) as String
        }
        if cString.count != 6{
            return UIColor.white
        }
        var range:NSRange = NSMakeRange(0, 2)
        let temstr2:NSString = cString as NSString
        let rString = temstr2.substring(with: range)as String
        range = NSMakeRange(2, 2)
        let gString = temstr2.substring(with: range)as String
        range = NSMakeRange(4, 2)
        let bString = temstr2.substring(with: range)as String
        var r:UInt32 = 0,g:UInt32 = 0,b:UInt32 = 0
        Scanner(string: rString).scanHexInt32(&r)
        Scanner(string: gString).scanHexInt32(&g)
        Scanner(string: bString).scanHexInt32(&b)
        
        return self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: alpha)
    }
    class func colorWithHexString(color:String)->UIColor{
        return hexSting(color: color, alpha: 1.0)
    }
}
func generateRandomString(length: Int) -> String {
    let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    var randomString = ""
    for _ in 0..<length {
        let randomIndex = Int.random(in: 0..<letters.count)
        let randomLetter = letters[letters.index(letters.startIndex, offsetBy: randomIndex)]
        randomString += String(randomLetter)
    }
    return randomString
}
