Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / VisualPalette.swift

Raw 1.7 KB on 32796b3b2cff314d20e7c78bdc17d74e2eace8c1
1import Foundation
2import simd
3
4/// Fixed color stops, shared by every geometry and shader family.
5enum VisualPalette: Int, CaseIterable {
6 case ultraviolet, electricCandy, emberlight, blueHour, fernlight
7
8 var title: String {
9 switch self {
10 case .ultraviolet: return "Ultraviolet"
11 case .electricCandy: return "Electric Candy"
12 case .emberlight: return "Emberlight"
13 case .blueHour: return "Blue Hour"
14 case .fernlight: return "Fernlight"
15 }
16 }
17
18 var stops: [SIMD3<Float>] {
19 Self.gradients[rawValue]
20 }
21
22 private static let gradients: [[SIMD3<Float>]] = [
23 // Three anchor hues retain each palette's identity; two accents add contrast.
24 [UInt32(0x44209D), 0xB34FF0, 0xFF9B78, 0x45E1CA, 0x9956EB],
25 [0x05F5E5, 0x51FF95, 0xE6FF38, 0xFF3FBA, 0x9B49FF],
26 [0xA51E32, 0xF5682D, 0xFFD267, 0xD966AC, 0x52CFC8],
27 [0x142E91, 0x3376EF, 0x55D9DF, 0xFFB66D, 0x678DF6],
28 [0x165A43, 0x38B771, 0xBCE76B, 0xFFBB82, 0x9D83EB]
29 ].map { hex in hex.map { SIMD3(Float(($0 >> 16) & 255), Float(($0 >> 8) & 255), Float($0 & 255)) / 255 } }
30
31 static func color(_ position: Float, palette: Int) -> SIMD4<Float> {
32 let stops = (VisualPalette(rawValue: palette) ?? .ultraviolet).stops
33 // Ping-pong through the stops so animation never introduces unrelated hues.
34 let p = position.isFinite ? position : 0
35 let cycle = p - floor(p / 2) * 2
36 let location = (cycle <= 1 ? cycle : 2 - cycle) * Float(stops.count - 1)
37 let first = min(stops.count - 1, Int(location))
38 let second = min(stops.count - 1, first + 1)
39 let rgb = stops[first] + (stops[second] - stops[first]) * (location - Float(first))
40 return SIMD4(rgb, 1)
41 }
42}