-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce AspectRatio struct (#10368)
# Objective - Fix an inconsistency in the calculation of aspect ratio's. - Fixes #10288 ## Solution - Created an intermediate `AspectRatio` struct, as suggested in the issue. This is currently just used in any places where aspect ratio calculations happen, to prevent doing it wrong. In my and @mamekoro 's opinion, it would be better if this was used instead of a normal `f32` in various places, but I didn't want to make too many changes to begin with. ## Migration Guide - Anywhere where you are currently expecting a f32 when getting aspect ratios, you will now receive a `AspectRatio` struct. this still holds the same value. --------- Co-authored-by: Alice Cecile <[email protected]>
- Loading branch information
1 parent
f12e906
commit 6b9cd57
Showing
6 changed files
with
40 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Provides a simple aspect ratio struct to help with calculations. | ||
/// An `AspectRatio` is the ratio of width to height. | ||
pub struct AspectRatio(f32); | ||
|
||
impl AspectRatio { | ||
/// Create a new `AspectRatio` from a given `width` and `height`. | ||
#[inline] | ||
pub fn new(width: f32, height: f32) -> Self { | ||
Self(width / height) | ||
} | ||
|
||
/// Create a new `AspectRatio` from a given amount of `x` pixels and `y` pixels. | ||
#[inline] | ||
pub fn from_pixels(x: u32, y: u32) -> Self { | ||
Self::new(x as f32, y as f32) | ||
} | ||
} | ||
|
||
impl From<AspectRatio> for f32 { | ||
#[inline] | ||
fn from(aspect_ratio: AspectRatio) -> Self { | ||
aspect_ratio.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters