-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathScaleUtil.hx
More file actions
100 lines (88 loc) · 2.97 KB
/
ScaleUtil.hx
File metadata and controls
100 lines (88 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.utils;
import openfl.geom.Rectangle;
/**
Utility functions for scaling geometry.
@since 1.0.0
**/
class ScaleUtil {
/**
The entire rectangle will be scaled to fit into the specified area,
while maintaining the original aspect ratio. This might leave empty bars
at either the top and bottom, or left and right.
@since 1.0.0
**/
public static function fitRectangle(original:Rectangle, into:Rectangle, ?result:Rectangle):Rectangle {
var width = original.width;
var height = original.height;
var intoWidth = into.width;
var intoHeight = into.height;
var scale = scaleToFit(width, height, intoWidth, intoHeight);
if (result == null) {
result = new Rectangle();
}
var resultWidth = width * scale;
var resultHeight = height * scale;
result.width = resultWidth;
result.height = resultHeight;
result.x = into.x + (intoWidth - resultWidth) / 2.0;
result.y = into.y + (intoHeight - resultHeight) / 2.0;
return result;
}
/**
Specifies that the rectangle fills the specified area, without
distortion but possibly with some cropping, while maintaining the
original aspect ratio.
@since 1.0.0
**/
public static function fillRectangle(original:Rectangle, into:Rectangle, ?result:Rectangle):Rectangle {
var width = original.width;
var height = original.height;
var intoWidth = into.width;
var intoHeight = into.height;
var scale = scaleToFill(width, height, intoWidth, intoHeight);
if (result == null) {
result = new Rectangle();
}
var resultWidth = width * scale;
var resultHeight = height * scale;
result.width = resultWidth;
result.height = resultHeight;
result.x = into.x + (intoWidth - resultWidth) / 2.0;
result.y = into.y + (intoHeight - resultHeight) / 2.0;
return result;
}
/**
Calculates the scale factor to fit the original dimensions into the
target dimensions, while maintaining the original aspect ratio. This
might leave empty bars at either the top and bottom, or left and right.
@since 1.0.0
**/
public static function scaleToFit(originalWidth:Float, originalHeight:Float, targetWidth:Float, targetHeight:Float):Float {
var widthRatio = targetWidth / originalWidth;
var heightRatio = targetHeight / originalHeight;
if (widthRatio < heightRatio) {
return widthRatio;
}
return heightRatio;
}
/**
Calculates the scale factor to fill the specified area, without
distortion but possibly with some cropping, while maintaining the
original aspect ratio.
@since 1.0.0
**/
public static function scaleToFill(originalWidth:Float, originalHeight:Float, targetWidth:Float, targetHeight:Float):Float {
var widthRatio = targetWidth / originalWidth;
var heightRatio = targetHeight / originalHeight;
if (widthRatio > heightRatio) {
return widthRatio;
}
return heightRatio;
}
}