Skip to content

Instantly share code, notes, and snippets.

@heestand-xyz
Last active October 24, 2018 12:40
Show Gist options
  • Save heestand-xyz/609e49f9aedd5e9bcb6888ef60cc1492 to your computer and use it in GitHub Desktop.
Save heestand-xyz/609e49f9aedd5e9bcb6888ef60cc1492 to your computer and use it in GitHub Desktop.
Range
func range(_ val: CGFloat, low: CGFloat, high: CGFloat, clamp: Bool = true, toLow: CGFloat = 0.0, toHigh: CGFloat = 1.0) -> CGFloat {
var norm = (val - low) / (high - low)
if clamp { norm = min(max(norm, 0.0), 1.0) }
return toLow + norm * (toHigh - toLow)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment