ItemLayout.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Model.Dto
  4. {
  5. public static class ItemLayout
  6. {
  7. public static double? GetDisplayAspectRatio(BaseItemDto item)
  8. {
  9. List<BaseItemDto> items = new List<BaseItemDto>();
  10. items.Add(item);
  11. return GetDisplayAspectRatio(items);
  12. }
  13. public static double? GetDisplayAspectRatio(List<BaseItemDto> items)
  14. {
  15. List<double> values = new List<double>();
  16. foreach (BaseItemDto item in items)
  17. {
  18. if (item.PrimaryImageAspectRatio.HasValue)
  19. {
  20. values.Add(item.PrimaryImageAspectRatio.Value);
  21. }
  22. }
  23. if (values.Count == 0)
  24. {
  25. return null;
  26. }
  27. values.Sort();
  28. double halfDouble = values.Count;
  29. halfDouble /= 2;
  30. int half = Convert.ToInt32(Math.Floor(halfDouble));
  31. double result;
  32. if (values.Count % 2 > 0)
  33. result = values[half];
  34. else
  35. result = (values[half - 1] + values[half]) / 2.0;
  36. // If really close to 2:3 (poster image), just return 2:3
  37. if (Math.Abs(0.66666666667 - result) <= .15)
  38. {
  39. return 0.66666666667;
  40. }
  41. // If really close to 16:9 (episode image), just return 16:9
  42. if (Math.Abs(1.777777778 - result) <= .2)
  43. {
  44. return 1.777777778;
  45. }
  46. // If really close to 1 (square image), just return 1
  47. if (Math.Abs(1 - result) <= .15)
  48. {
  49. return 1.0;
  50. }
  51. // If really close to 4:3 (poster image), just return 2:3
  52. if (Math.Abs(1.33333333333 - result) <= .15)
  53. {
  54. return 1.33333333333;
  55. }
  56. return result;
  57. }
  58. }
  59. }