Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active May 21, 2024 00:14
Show Gist options
  • Save benigumocom/3d471772fa10a8167663b07fb0d9851c to your computer and use it in GitHub Desktop.
Save benigumocom/3d471772fa10a8167663b07fb0d9851c to your computer and use it in GitHub Desktop.
【Swift】Color の RGB + Opacity の数値を求める 👉 https://android.benigumo.com/20240518/color-rgbo/
import SwiftUI
struct TestColorValues: View {
var color: Color
@Environment(\.self) private var environment
private var resolved: Color.Resolved {
color.resolve(in: environment)
}
var body: some View {
VStack {
Text("\(resolved)")
Text(String(format: "R: %.3f", resolved.red))
Text(String(format: "G: %.3f", resolved.green))
Text(String(format: "B: %.3f", resolved.blue))
Text(String(format: "O: %.3f", resolved.opacity))
}
.monospaced()
.padding()
.background(color)
}
}
#Preview("TestColorValues") {
VStack {
TestColorValues(color: Color(.secondarySystemBackground))
.environment(\.colorScheme, .light)
TestColorValues(color: Color(.secondarySystemBackground))
.environment(\.colorScheme, .dark)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment