oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2
3extension Color {
4 init(hex: String) {
5 let value = UInt64(hex.trimmingCharacters(in: CharacterSet(charactersIn: "#")), radix: 16) ?? 0
6 self.init(
7 .sRGB, red: Double((value >> 16) & 255) / 255, green: Double((value >> 8) & 255) / 255,
8 blue: Double(value & 255) / 255, opacity: 1)
9 }
10 static func oklch(_ l: Double, _ c: Double, _ h: Double) -> Color {
11 let a = c * cos(h * .pi / 180)
12 let b = c * sin(h * .pi / 180)
13 let x = pow(l + 0.3963377774 * a + 0.2158037573 * b, 3)
14 let y = pow(l - 0.1055613458 * a - 0.0638541728 * b, 3)
15 let z = pow(l - 0.0894841775 * a - 1.291485548 * b, 3)
16 func gamma(_ v: Double) -> Double {
17 min(1, max(0, v <= 0.0031308 ? 12.92 * v : 1.055 * pow(v, 1 / 2.4) - 0.055))
18 }
19 return Color(
20 .sRGB, red: gamma(4.0767416621 * x - 3.3077115913 * y + 0.2309699292 * z),
21 green: gamma(-1.2684380046 * x + 2.6097574011 * y - 0.3413193965 * z),
22 blue: gamma(-0.0041960863 * x - 0.7034186147 * y + 1.707614701 * z), opacity: 1)
23 }
24}
25