//
//  color.swift
//  anniversary_Project
//
//  Created by 忆海16 on 2024/4/15.
//

import Foundation
import UIKit

extension UIColor{
    

    class func hexSting(color:String,alpha:CGFloat)->UIColor{
       
        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 isMidnight() -> Bool {
    let calendar = Calendar.current
    let now = Date()
    
    let components = calendar.dateComponents([.hour, .minute], from: now)
    
    if let hour = components.hour, let minute = components.minute {
        return hour == 0 && minute == 0
    }
    
    return false
}
