Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fa7ec0611c
Fetching contributors…

Cannot retrieve contributors at this time

41 lines (31 sloc) 1.02 kb
package mogrify
import (
"math"
"fmt"
)
type Bounds struct {
Width, Height int
}
func (b Bounds) String() string {
return fmt.Sprintf("%dx%d", b.Width, b.Height)
}
func (b Bounds) ScaleProportionally(targetWidth, targetHeight int) Bounds {
scalex := (float64)(targetWidth) / (float64)(b.Width)
scaley := (float64)(targetHeight) / (float64)(b.Height)
scale := math.Min(scalex, scaley)
return Bounds{ (int)(math.Floor((float64)(b.Width) * scale)), (int)(math.Floor((float64)(b.Height) * scale)) }
}
func (b Bounds) ShrinkProportionally(targetWidth, targetHeight int) Bounds {
// Make sure there is work to be done
if b.Width < targetWidth || b.Height < targetHeight {
return b
}
return b.ScaleProportionally(targetWidth, targetHeight)
}
func (b Bounds) GrowProportionally(targetWidth, targetHeight int) Bounds {
// Make sure there is work to be done
if b.Width > targetWidth || b.Height > targetHeight {
return b
}
return b.ScaleProportionally(targetWidth, targetHeight)
}
Jump to Line
Something went wrong with that request. Please try again.