//
//  Color+Extensions.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 27/03/2024.
//

import SwiftUI

extension Color {
    
    func variation(withHueOffset hueOffset: Double = 0, saturationFactor: Double = 0.4, brightnessFactor: Double = 0.8, opacity: Double = 0.5) -> Color {
        var hue: CGFloat = 0
        var saturation: CGFloat = 0
        var brightness: CGFloat = 0
        var alpha: CGFloat = 0
        
        UIColor(self).getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
        
        // Apply adjustments
        hue += CGFloat(hueOffset)
        saturation *= CGFloat(saturationFactor)
        brightness *= CGFloat(brightnessFactor)
        alpha *= CGFloat(opacity)
        
        // Clamp values
        hue = max(0, min(hue, 1))
        saturation = max(0, min(saturation, 1))
        brightness = max(0, min(brightness, 1))
        alpha = max(0, min(alpha, 1))
        
        return Color(hue: Double(hue), saturation: Double(saturation), brightness: Double(brightness), opacity: Double(alpha))
    }
    
}
