Created
February 28, 2019 05:28
-
-
Save mlaster/378ae6c62e9fcf8d1268282831e710f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Darwin | |
let Max = 5000.0 | |
func log(_ x: Double, base: Double) -> Double { | |
let result = log(x + 1.0) / log(base + 1.0) | |
print(" log(\(x), base: \(base)) -> \(result)") | |
return result | |
} | |
func scale(_ x: Double) -> UInt8 { | |
print(" scale(\(x))") | |
let inverseX = Double(Max - x) | |
print(" inverseX: \(inverseX)") | |
let inverseLog = log(inverseX, base: Max) | |
print(" inverseLog: \(inverseLog)") | |
let result = 255 - UInt8(inverseLog * 255.0) | |
print(" result: \(result)") | |
return result | |
} | |
func unscale(_ x: UInt8) -> Double { | |
let inverseX = Double(255 - x) | |
let inverseLog = log(inverseX, base: 255) // Convert to range 0.0 ... 1.0 | |
return Max - (inverseLog * Max) | |
} | |
for i: UInt8 in 0...255 { | |
let unscaled = unscale(i) | |
let scaled = scale(unscaled) | |
print("\(i) -> \(unscaled) -> \(scaled)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment