28 lines
1003 B
Swift
28 lines
1003 B
Swift
// ParameterFormatting.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
/// Threshold above which a value renders with zero fractional digits.
|
|
internal let parameterFormatLargeMagnitudeThreshold: Double = 1000
|
|
|
|
/// Threshold above which a value renders with one fractional digit.
|
|
internal let parameterFormatMediumMagnitudeThreshold: Double = 10
|
|
|
|
/// Render a numeric parameter value with magnitude-adaptive precision.
|
|
///
|
|
/// - `|v| >= 1000`: no decimals.
|
|
/// - `1000 > |v| >= 10`: one decimal.
|
|
/// - `|v| < 10`: three decimals.
|
|
///
|
|
/// Shared by `ParameterKnob` and `ParameterXY` so the two controls agree on
|
|
/// display formatting.
|
|
internal func formattedParameterValue(_ v: Double) -> String {
|
|
if abs(v) >= parameterFormatLargeMagnitudeThreshold { return String(format: "%.0f", v) }
|
|
if abs(v) >= parameterFormatMediumMagnitudeThreshold { return String(format: "%.1f", v) }
|
|
return String(format: "%.3f", v)
|
|
}
|