Skip to content

Commit 9266d3c

Browse files
committed
💾 Feat(Resolution): Optimized.
1 parent 98235d2 commit 9266d3c

1 file changed

Lines changed: 23 additions & 52 deletions

File tree

Common.BasicHelper/Graphics/Screen/Resolution.cs

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Common.BasicHelper.Utils.Extensions;
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56

67
namespace Common.BasicHelper.Graphics.Screen;
78

@@ -50,10 +51,10 @@ public class Resolution
5051

5152
public double? AspectRatio => Width / Height;
5253

53-
public string Description { get; set; } = string.Empty;
54+
public string? Description { get; set; }
5455

5556
/// <summary>
56-
/// 宽高整数化
57+
/// Integerize width and height
5758
/// </summary>
5859
public Resolution Integerization()
5960
{
@@ -65,10 +66,10 @@ public Resolution Integerization()
6566
}
6667

6768
/// <summary>
68-
/// 根据字符串返回分辨率对象
69+
/// Returns a resolution object based on a string
6970
/// </summary>
70-
/// <param name="input">字符串: 宽x高@刷新率</param>
71-
/// <returns>分辨率对象</returns>
71+
/// <param name="input">Format: {Width}x{Height}@{FPS}</param>
72+
/// <returns>Resolution object</returns>
7273
public static Resolution Parse(string input)
7374
{
7475
var res_fps = input.Split('@');
@@ -77,7 +78,7 @@ public static Resolution Parse(string input)
7778
var resolution = new Resolution
7879
{
7980
Width = Convert.ToDouble(res[0]),
80-
Height = Convert.ToDouble(res[1])
81+
Height = Convert.ToDouble(res[1]),
8182
};
8283

8384
if (res_fps.Length == 2) resolution.FramePerSecond = Convert.ToDouble(res_fps[1]);
@@ -87,27 +88,30 @@ public static Resolution Parse(string input)
8788
}
8889

8990
/// <summary>
90-
/// 根据字符串返回分辨率对象
91+
/// Returns a resolution object based on a string
9192
/// </summary>
92-
/// <param name="input">字符串: 宽x高@刷新率</param>
93-
/// <param name="descr">字符串: 描述信息</param>
94-
/// <returns>分辨率对象</returns>
95-
public static Resolution Parse(string input, string descr)
93+
/// <param name="input">Format: {Width}x{Height}@{FPS}</param>
94+
/// <param name="descr">Description</param>
95+
/// <returns>Resolution object</returns>
96+
public static Resolution Parse(string input, string? descr = null)
9697
{
9798
var resolution = Parse(input);
9899

99-
resolution.Description = descr;
100+
if (descr is null)
101+
resolution.Description = resolutions.FirstOrDefault((x) => x.Equals(resolution))?.Description;
102+
else
103+
resolution.Description = descr;
100104

101105
return resolution;
102106
}
103107

104108
/// <summary>
105-
/// 建议分辨率
109+
/// Suggest content resolution
106110
/// </summary>
107-
/// <param name="screen">理想屏幕</param>
108-
/// <param name="content">理想内容</param>
109-
/// <param name="actual">实际屏幕</param>
110-
/// <returns>建议的内容尺寸</returns>
111+
/// <param name="screen">Target screen resolution</param>
112+
/// <param name="content">Target content resolution</param>
113+
/// <param name="actual">Actual screen resolution</param>
114+
/// <returns>Suggested content resolution</returns>
111115
public static Resolution Suggest(Resolution screen, Resolution content, Resolution actual)
112116
{
113117
// 理想内容面积与理想屏幕面积之比
@@ -145,20 +149,8 @@ public static Resolution Suggest(Resolution screen, Resolution content, Resoluti
145149
return suggest;
146150
}
147151

148-
/// <summary>
149-
/// 重载运算符: 除法
150-
/// </summary>
151-
/// <param name="a">分辨率1</param>
152-
/// <param name="b">分辨率2</param>
153-
/// <returns>面积之比</returns>
154152
public static double? operator /(Resolution a, Resolution b) => a.Area / b.Area;
155153

156-
/// <summary>
157-
/// 重载运算符: 加法
158-
/// </summary>
159-
/// <param name="a">分辨率1</param>
160-
/// <param name="b">分辨率2</param>
161-
/// <returns>分辨率</returns>
162154
public static Resolution operator +(Resolution a, Resolution b) => new()
163155
{
164156
Width = a.Width + b.Width,
@@ -167,40 +159,19 @@ public static Resolution Suggest(Resolution screen, Resolution content, Resoluti
167159
Description = $"{a.Description}\nPlus\n{b.Description}"
168160
};
169161

170-
/// <summary>
171-
/// 重写 ToString() 方法
172-
/// </summary>
173-
/// <returns>表示分辨率及刷新率的字符串</returns>
174162
public override string ToString() =>
175163
$"{Width}x{Height}{(FramePerSecond is null ? "" : "@")}{FramePerSecond}";
176164

177-
/// <summary>
178-
/// 重写 Equals() 方法
179-
/// </summary>
180-
/// <param name="obj">目标比较分辨率</param>
181-
/// <returns>是否相等</returns>
182165
public override bool Equals(object obj)
183166
{
184167
if (obj is not Resolution)
185168
ErrorCodes.CB0017.BuildMessage(parameterName: nameof(Equals)).Throw<ArgumentException>();
186169

187-
return GetHashCode() == obj.GetHashCode();
170+
var target = obj as Resolution;
188171

189-
//if (obj is not Resolution res) return false;
190-
191-
//if (Width.Equals(res.Width) && Height.Equals(res.Height))
192-
// if (FramePerSecond != null && res.FramePerSecond != null)
193-
// if (FramePerSecond.Equals(res.FramePerSecond))
194-
// return true;
195-
// else return false;
196-
// else return true;
197-
//else return false;
172+
return Width == target?.Width && Height == target?.Height && FramePerSecond == target?.FramePerSecond;
198173
}
199174

200-
/// <summary>
201-
/// 重写 GetHashCode() 方法
202-
/// </summary>
203-
/// <returns>哈希值</returns>
204175
public override int GetHashCode() => (int)(
205176
0
206177
+ (Area.GetHashCode() ^ AspectRatio.GetHashCode())

0 commit comments

Comments
 (0)