mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
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
1 changed files with 19 additions and 0 deletions
|
@ -93,6 +93,25 @@ public:
|
||||||
return static_cast<float>(width()) / static_cast<float>(height());
|
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>
|
template<typename U>
|
||||||
[[nodiscard]] constexpr bool contains(Size<U> const& other) const
|
[[nodiscard]] constexpr bool contains(Size<U> const& other) const
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue