Skip to content

Instantly share code, notes, and snippets.

@EnesKaraosman
Last active May 9, 2024 22:41
Show Gist options
  • Save EnesKaraosman/efb9c2d989e51d20253976c8fb1aa734 to your computer and use it in GitHub Desktop.
Save EnesKaraosman/efb9c2d989e51d20253976c8fb1aa734 to your computer and use it in GitHub Desktop.
Generatin random color in SwiftUI & UIKit
#if canImport(UIKit)
import UIKit
extension UIColor {
static var random: UIColor {
return UIColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
#elseif canImport(SwiftUI)
import SwiftUI
extension Color {
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
#else
// all other platforms – use a custom color object
#endif
@mohitnandwani
Copy link

@treyharris AFAIK no difference in particular

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment