LibGfx: Add Size::match_aspect_ratio
This will create a size that matches the given aspect ratio while preserving one of the two dimensions.
This commit is contained in:
parent
5b5b4f57fa
commit
af439cf3af
Notes:
sideshowbarker
2024-07-17 04:09:07 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/af439cf3af Pull-request: https://github.com/SerenityOS/serenity/pull/15718 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/davidot ✅ Reviewed-by: https://github.com/idispatch ✅
1 changed files with 19 additions and 0 deletions
|
@ -93,6 +93,25 @@ public:
|
|||
return static_cast<float>(width()) / static_cast<float>(height());
|
||||
}
|
||||
|
||||
// Horizontal means preserve the width, Vertical means preserve the height.
|
||||
[[nodiscard]] constexpr Size<T> match_aspect_ratio(float aspect_ratio, Orientation side_to_preserve) const
|
||||
{
|
||||
VERIFY(aspect_ratio != 0.0f);
|
||||
auto matched = *this;
|
||||
auto height_corresponding_to_width = static_cast<T>(static_cast<float>(width()) / aspect_ratio);
|
||||
auto width_corresponding_to_height = static_cast<T>(static_cast<float>(height()) * aspect_ratio);
|
||||
|
||||
switch (side_to_preserve) {
|
||||
case Orientation::Vertical:
|
||||
matched.m_width = width_corresponding_to_height;
|
||||
break;
|
||||
case Orientation::Horizontal:
|
||||
matched.m_height = height_corresponding_to_width;
|
||||
break;
|
||||
}
|
||||
return matched;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
[[nodiscard]] constexpr bool contains(Size<U> const& other) const
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue