CHGenerator/Sources/Generator/Extensions/NSSize+Extensions.swift

29 lines
553 B
Swift
Raw Normal View History

import Foundation
extension NSSize {
func scaledDown(to desiredWidth: CGFloat) -> NSSize {
if width == desiredWidth {
return self
}
if width < desiredWidth {
// Don't scale larger
return self
}
2022-09-16 15:32:55 +02:00
let height = (height * desiredWidth / width).rounded(.down)
return NSSize(width: desiredWidth, height: height)
}
}
extension NSSize {
var ratio: CGFloat {
guard height != 0 else {
return 0
}
return width / height
}
}