Repository Public License
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Camera9// File : Packages/ORIXCameraKit/Sources/ORIXImaging/Capture/WhiteBalance.swift10// Description : White-balance presets, gain clamping for the device, and a11// grey-world cast estimate for Analysis mode.12// Module : ORIXImaging13// Repository : https://git.spboucher.ai/orix-camera14// Project Website : https://www.orixlab.dev15// Created : 2026-11-2116// Last Modified : 2026-11-2117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author : Simon-Pierre Boucher23// Position : Professor of Finance24// Institution : Université du Québec en Outaouais (UQO)25// Department : Département des sciences administratives26// Organization : SP Boucher27//28// Website : https://spboucher.ai29// Git : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General : info@spboucher.ai36// Professional : contact@spboucher.ai37// Direct : simon@spboucher.ai38// Research : research@spboucher.ai39// Development : dev@spboucher.ai40// Support : support@spboucher.ai41// Security/Admin : admin@spboucher.ai42// Notifications : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification : Public69// Distribution : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License : Proprietary, all rights reserved (see license file)83// License File : LICENSE84// Maintainer : Simon-Pierre Boucher85// Technical Contact: dev@spboucher.ai86//87// =============================================================================88// END OF SP BOUCHER SOURCE HEADER89// =============================================================================9091import Foundation9293public struct WhiteBalancePreset: Sendable, Equatable, Identifiable, Codable {94 public var id: String95 public var title: String96 public var symbol: String97 /// Kelvin.98 public var temperature: Double99 public var tint: Double100101 public static let builtIn: [WhiteBalancePreset] = [102 .init(id: "tungsten", title: "Tungsten", symbol: "lightbulb", temperature: 3200, tint: 0),103 .init(id: "fluorescent", title: "Fluorescent", symbol: "light.strip.2", temperature: 4000, tint: 15),104 .init(id: "daylight", title: "Daylight", symbol: "sun.max", temperature: 5500, tint: 0),105 .init(id: "cloudy", title: "Cloudy", symbol: "cloud", temperature: 6500, tint: 0),106 .init(id: "shade", title: "Shade", symbol: "building.2", temperature: 7500, tint: 5),107 ]108}109110public struct WhiteBalanceGains: Sendable, Equatable {111 public var red: Double112 public var green: Double113 public var blue: Double114 public init(red: Double, green: Double, blue: Double) { self.red = red; self.green = green; self.blue = blue }115116 /// Gains the device accepts: every gain inside 1…maxGain. Values are first normalized so the smallest is 1 (which117 /// preserves the ratios, the part that defines the colour), then capped.118 public func clamped(maxGain: Double) -> WhiteBalanceGains {119 let smallest = min(red, green, blue)120 let k = smallest > 0 ? 1 / smallest : 1121 func fit(_ v: Double) -> Double { min(max(v * k, 1), max(maxGain, 1)) }122 return WhiteBalanceGains(red: fit(red), green: fit(green), blue: fit(blue))123 }124}125126public struct ColorCast: Sendable, Equatable {127 /// Mean red / mean green and mean blue / mean green of the frame (1 = neutral under the grey-world assumption).128 public var redOverGreen: Double129 public var blueOverGreen: Double130131 public init(histogram: HistogramData) {132 let g = max(histogram.mean(.green), 0.001)133 redOverGreen = histogram.mean(.red) / g134 blueOverGreen = histogram.mean(.blue) / g135 }136137 /// −1 (cool) … 1 (warm), a compact indicator for the UI.138 public var warmth: Double { min(max((redOverGreen - blueOverGreen) * 1.5, -1), 1) }139}140