Skip to content

Commit 5c38e1e

Browse files
committed
Aspect ratio detection code moved to Models
1 parent 8bda1b1 commit 5c38e1e

3 files changed

Lines changed: 69 additions & 53 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace ScreenGrid.Models
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Globalization;
6+
using System.Linq;
7+
8+
public static class AspectRatioDetector
9+
{
10+
// TODO: add tests
11+
12+
/// <summary>
13+
/// Common aspect ratios
14+
/// https://en.wikipedia.org/wiki/Aspect_ratio_%28image%29
15+
/// </summary>
16+
private static Dictionary<double, string> commonAspectRatios = new Dictionary<double, string>()
17+
{
18+
// TODO: create class with separated properties
19+
// TODO: add descriptions for displaying in tooltip
20+
{ 1.0, "1:1" },
21+
{ 6.0 / 5.0, "6:5" },
22+
{ 5.0 / 4.0, "5:4" },
23+
{ 4.0 / 3.0, "4:3" },
24+
{ 11.0 / 8.0, "11:8" },
25+
{ Math.Sqrt(2.0), "1.41:1" }, // ISO 216 paper sizes
26+
{ 1.43, "1.43:1" },
27+
{ 3.0 / 2.0, "3:2" },
28+
{ 16.0 / 10.0, "16:10" }, // The golden ratio
29+
{ 1.618, "16.18:10" },
30+
{ 5.0 / 3.0, "5:3" },
31+
{ 16.0 / 9.0, "16:9" },
32+
{ 1.85, "1.85:1" },
33+
{ 2.35, "2.35:1" },
34+
{ 2.39, "2.39:1" },
35+
{ 2.414, "2.414:1" }, // The silver ratio
36+
{ 2.76, "2.76:1" },
37+
};
38+
39+
public static string DetectCommonAspectRatio(double ratio)
40+
{
41+
const double Tolerance = 0.01;
42+
43+
if (ratio >= 1.0)
44+
{
45+
// Horizontal
46+
var nearest = commonAspectRatios.OrderBy(kvp => Math.Abs(kvp.Key - ratio)).First();
47+
if (Math.Abs(nearest.Key - ratio) < Tolerance)
48+
{
49+
return nearest.Value;
50+
}
51+
}
52+
else
53+
{
54+
// Vertical
55+
ratio = 1.0 / ratio;
56+
var nearest = commonAspectRatios.OrderBy(kvp => Math.Abs(kvp.Key - ratio)).First();
57+
if (Math.Abs(nearest.Key - ratio) < Tolerance)
58+
{
59+
var arr = nearest.Value.Split(':');
60+
return String.Format(CultureInfo.InvariantCulture, "{0}:{1}", arr[1], arr[0]);
61+
}
62+
}
63+
64+
return String.Empty;
65+
}
66+
}
67+
}

Src/ScreenGrid.Models/ScreenGrid.Models.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<Compile Include="AppsInterop\NativeWindow.cs" />
4242
<Compile Include="AppsInterop\OctaneRenderWindow.cs" />
4343
<Compile Include="AppsInterop\PhotoViewerWindow.cs" />
44+
<Compile Include="AspectRatioDetector.cs" />
4445
<Compile Include="FlatImage.cs" />
4546
<Compile Include="Geometry\Line.cs" />
4647
<Compile Include="Geometry\Point.cs" />

Src/ScreenGrid.ViewModels/ScreenGridViewModel.cs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -293,64 +293,12 @@ private Tuple<int, int> ImageSize
293293
}
294294
}
295295

296-
// TODO: move code to Models, add tests
297-
298-
/// <summary>
299-
/// Common aspect ratios
300-
/// https://en.wikipedia.org/wiki/Aspect_ratio_%28image%29
301-
/// </summary>
302-
private static Dictionary<double, string> CommonAspectRatios = new Dictionary<double, string>()
303-
{
304-
// TODO: create class with separated properties
305-
// TODO: add descriptions for displaying in tooltip
306-
{ 1.0, "1:1" },
307-
{ 6.0 / 5.0, "6:5" },
308-
{ 5.0 / 4.0, "5:4" },
309-
{ 4.0 / 3.0, "4:3" },
310-
{ 11.0 / 8.0, "11:8" },
311-
{ Math.Sqrt(2.0), "1.41:1" }, // ISO 216 paper sizes
312-
{ 1.43, "1.43:1" },
313-
{ 3.0 / 2.0, "3:2" },
314-
{ 16.0 / 10.0, "16:10" }, // The golden ratio
315-
{ 1.618, "16.18:10" },
316-
{ 5.0 / 3.0, "5:3" },
317-
{ 16.0 / 9.0, "16:9" },
318-
{ 1.85, "1.85:1" },
319-
{ 2.35, "2.35:1" },
320-
{ 2.39, "2.39:1" },
321-
{ 2.414, "2.414:1" }, // The silver ratio
322-
{ 2.76, "2.76:1" },
323-
};
324-
325296
private string AspectRatio
326297
{
327298
get
328299
{
329-
const double tolerance = 0.01;
330300
var ratio = ((double)this.ImageSize.Item1) / ((double)this.ImageSize.Item2);
331-
332-
if (ratio >= 1.0)
333-
{
334-
// Horizontal
335-
var nearest = CommonAspectRatios.OrderBy(kvp => Math.Abs(kvp.Key - ratio)).First();
336-
if (Math.Abs(nearest.Key - ratio) < tolerance)
337-
{
338-
return nearest.Value;
339-
}
340-
}
341-
else
342-
{
343-
// Vertical
344-
ratio = 1.0 / ratio;
345-
var nearest = CommonAspectRatios.OrderBy(kvp => Math.Abs(kvp.Key - ratio)).First();
346-
if (Math.Abs(nearest.Key - ratio) < tolerance)
347-
{
348-
var arr = nearest.Value.Split(':');
349-
return String.Format(CultureInfo.InvariantCulture, "{0}:{1}", arr[1], arr[0]);
350-
}
351-
}
352-
353-
return String.Empty;
301+
return Models.AspectRatioDetector.DetectCommonAspectRatio(ratio);
354302
}
355303
}
356304

0 commit comments

Comments
 (0)