Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion UIImage+Spectrogram/ImageUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ImageUtils {
let timeWidth = magnitudes.count
let frequencyHeight = magnitudes.first!.count

let pixels = magnitudes.map{ $0.map{ RGBA<Float>(gray: 1-$0) } }
let pixels = magnitudes.map{ $0.map{ ImageUtils.colorForMagnitude(1.0 - $0) } }

let flattenedPixels = pixels.flatMap{ $0 }

Expand Down Expand Up @@ -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<Float> {
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<Float>(red: Float(r), green: Float(g), blue: Float(b), alpha: Float(a))
}
}