|
| 1 | +// ----------------------------------------------------------------------- |
| 2 | +// <copyright file="FloatExtensions.cs" company="ExMod Team"> |
| 3 | +// Copyright (c) ExMod Team. All rights reserved. |
| 4 | +// Licensed under the CC BY-SA 3.0 license. |
| 5 | +// </copyright> |
| 6 | +// ----------------------------------------------------------------------- |
| 7 | + |
| 8 | +namespace Exiled.API.Extensions |
| 9 | +{ |
| 10 | + using System; |
| 11 | + using System.Collections.Generic; |
| 12 | + |
| 13 | + using Exiled.API.Enums; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// A set of extensions for <see cref="float"/>. |
| 17 | + /// </summary> |
| 18 | + public static class FloatExtensions |
| 19 | + { |
| 20 | + private static readonly Dictionary<AspectRatioType, float> AspectRatioReferences = new() |
| 21 | + { |
| 22 | + { AspectRatioType.Unknown, 0f }, |
| 23 | + { AspectRatioType.Ratio1_1, 1f }, |
| 24 | + { AspectRatioType.Ratio3_2, 3f / 2f }, |
| 25 | + { AspectRatioType.Ratio4_3, 4f / 3f }, |
| 26 | + { AspectRatioType.Ratio5_4, 5f / 4f }, |
| 27 | + { AspectRatioType.Ratio16_9, 16f / 9f }, |
| 28 | + { AspectRatioType.Ratio16_10, 16f / 10f }, |
| 29 | + { AspectRatioType.Ratio21_9, 21f / 9f }, |
| 30 | + { AspectRatioType.Ratio32_9, 32f / 9f }, |
| 31 | + }; |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Gets the closest <see cref="AspectRatioType"/> for a given aspect ratio value. |
| 35 | + /// </summary> |
| 36 | + /// <param name="ratio">The aspect ratio value to compare.</param> |
| 37 | + /// <returns>The closest matching <see cref="AspectRatioType"/>.</returns> |
| 38 | + public static AspectRatioType GetAspectRatioLabel(this float ratio) |
| 39 | + { |
| 40 | + float closestDiff = float.MaxValue; |
| 41 | + AspectRatioType closestRatio = AspectRatioType.Unknown; |
| 42 | + |
| 43 | + foreach (KeyValuePair<AspectRatioType, float> kvp in AspectRatioReferences) |
| 44 | + { |
| 45 | + float diff = Math.Abs(ratio - kvp.Value); |
| 46 | + if (diff < closestDiff) |
| 47 | + { |
| 48 | + closestDiff = diff; |
| 49 | + closestRatio = kvp.Key; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return closestRatio; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments