diff --git a/UIImage+Spectrogram/ImageUtils.swift b/UIImage+Spectrogram/ImageUtils.swift index 85741e3..db3d1f5 100644 --- a/UIImage+Spectrogram/ImageUtils.swift +++ b/UIImage+Spectrogram/ImageUtils.swift @@ -43,7 +43,7 @@ public class ImageUtils { let timeWidth = magnitudes.count let frequencyHeight = magnitudes.first!.count - let pixels = magnitudes.map{ $0.map{ RGBA(gray: 1-$0) } } + let pixels = magnitudes.map{ $0.map{ ImageUtils.colorForMagnitude(1.0 - $0) } } let flattenedPixels = pixels.flatMap{ $0 } @@ -87,4 +87,11 @@ public class ImageUtils { public static func mixColor(magnitude: Float) -> UIColor { return UIColor(hue: 0.75, saturation: CGFloat(magnitude), brightness: 1.0, alpha: 1.0) } + + private static func colorForMagnitude(_ mag: Float) -> RGBA { + let color = UIColor(hue: CGFloat(mag), saturation: 1.0, brightness: 1.0, alpha: 1.0) + var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0 + color.getRed(&r, green: &g, blue: &b, alpha: &a) + return RGBA(red: Float(r), green: Float(g), blue: Float(b), alpha: Float(a)) + } }