✨UnitInterval
將變數限制在 0 ... 1 之間。
@propertyWrapper
public struct UnitInterval<Value: FloatingPoint> {
// 沒有私密屬性
// 統一的對外通道(wrappedValue)直接使用另一個 property wrapper
@Clamped(in: 0...1)
public var wrappedValue: Value = .zero
public init(wrappedValue: Value = .zero) {
self.wrappedValue = wrappedValue
}
}
struct RGB {
@UnitInterval var red: Double = 0
@UnitInterval var green: Double = 0
@UnitInterval var blue: Double = 0
}
let cornflowerBlue = RGB(red: 0.392, green: 0.584, blue: 0.929)
let white = RGB(red: 1, green: 2, blue: 3)
let black = RGB()
print(cornflowerBlue.red, white.green, black.blue) // 0.392 1.0 0.0
paiza.io - Property Wrappers
Last updated