Skip to content

Commit cdb04b9

Browse files
committed
拆分实现逻辑
1 parent 67a6566 commit cdb04b9

2 files changed

Lines changed: 126 additions & 118 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System.Diagnostics;
2+
using SixLabors.ImageSharp.PixelFormats;
3+
4+
namespace DotNetCampus.MediaConverters.Utils;
5+
6+
static class ColorConverter
7+
{
8+
/// <summary>
9+
/// 将16进制的颜色字符串转为<see cref="Rgba32"/>
10+
/// </summary>
11+
/// <param name="hexColorText">颜色格式是 AARRGGBB</param>
12+
/// <param name="color"></param>
13+
/// <returns></returns>
14+
public static bool TryConvertToColor(string hexColorText, out Rgba32 color)
15+
{
16+
color = new Rgba32();
17+
18+
bool startWithPoundSign = hexColorText.StartsWith('#');
19+
var colorStringLength = hexColorText.Length;
20+
if (startWithPoundSign) colorStringLength -= 1;
21+
int currentOffset = startWithPoundSign ? 1 : 0;
22+
// 可以采用的格式如下
23+
// #FFDFD991 8 个字符 存在 Alpha 通道
24+
// #DFD991 6 个字符
25+
// #FD92 4 个字符 存在 Alpha 通道
26+
// #DAC 3 个字符
27+
if (colorStringLength == 8
28+
|| colorStringLength == 6
29+
|| colorStringLength == 4
30+
|| colorStringLength == 3)
31+
{
32+
bool success;
33+
byte result;
34+
byte a;
35+
36+
int readCount;
37+
// #DFD991 6 个字符
38+
// #FFDFD991 8 个字符 存在 Alpha 通道
39+
//if (colorStringLength == 8 || colorStringLength == 6)
40+
if (colorStringLength > 5)
41+
{
42+
readCount = 2;
43+
}
44+
else
45+
{
46+
readCount = 1;
47+
}
48+
49+
bool includeAlphaChannel = colorStringLength == 8 || colorStringLength == 4;
50+
51+
if (includeAlphaChannel)
52+
{
53+
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
54+
if (!success) return false;
55+
a = result;
56+
currentOffset += readCount;
57+
}
58+
else
59+
{
60+
a = 0xFF;
61+
}
62+
63+
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
64+
if (!success) return false;
65+
byte r = result;
66+
currentOffset += readCount;
67+
68+
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
69+
if (!success) return false;
70+
byte g = result;
71+
currentOffset += readCount;
72+
73+
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
74+
if (!success) return false;
75+
byte b = result;
76+
77+
color = new Rgba32(r, g, b, a);
78+
return true;
79+
}
80+
81+
return false;
82+
}
83+
84+
static (bool success, byte result) HexCharToNumber(string input, int offset, int readCount)
85+
{
86+
Debug.Assert(readCount == 1 || readCount == 2, "要求 readCount 只能是 1 或者 2 的值,这是框架限制,因此不做判断");
87+
88+
byte result = 0;
89+
90+
for (int i = 0; i < readCount; i++, offset++)
91+
{
92+
var c = input[offset];
93+
byte n;
94+
if (c >= '0' && c <= '9')
95+
{
96+
n = (byte) (c - '0');
97+
}
98+
else if (c >= 'a' && c <= 'f')
99+
{
100+
n = (byte) (c - 'a' + 10);
101+
}
102+
else if (c >= 'A' && c <= 'F')
103+
{
104+
n = (byte) (c - 'A' + 10);
105+
}
106+
else
107+
{
108+
return default;
109+
}
110+
111+
result *= 16;
112+
result += n;
113+
}
114+
115+
if (readCount == 1)
116+
{
117+
result = (byte) (result * 16 + result);
118+
}
119+
120+
return (true, result);
121+
}
122+
}
Lines changed: 4 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Diagnostics;
2-
using DotNetCampus.MediaConverters.Contexts;
1+
using DotNetCampus.MediaConverters.Contexts;
32
using SixLabors.ImageSharp.PixelFormats;
43

54
namespace DotNetCampus.MediaConverters.Utils;
@@ -8,126 +7,13 @@ internal static class ReplaceColorInfoExtension
87
{
98
public static (Rgba32 OldColor, Rgba32 NewColor) ToRgba32Pair(this ReplaceColorInfo info)
109
{
11-
if (ConvertToColor(info.OldColor, out var oldColor) && ConvertToColor(info.NewColor, out var newColor))
10+
if (ColorConverter.TryConvertToColor(info.OldColor, out var oldColor) && ColorConverter.TryConvertToColor(info.NewColor, out var newColor))
1211
{
1312
return (oldColor,newColor);
1413
}
1514

1615
throw new FormatException();
1716
}
1817

19-
/// <summary>
20-
/// 将16进制的颜色字符串转为<see cref="Rgba32"/>
21-
/// </summary>
22-
/// <param name="hexColorText">颜色格式是 AARRGGBB</param>
23-
/// <param name="color"></param>
24-
/// <returns></returns>
25-
private static bool ConvertToColor(string hexColorText, out Rgba32 color)
26-
{
27-
color = new Rgba32();
28-
29-
bool startWithPoundSign = hexColorText.StartsWith('#');
30-
var colorStringLength = hexColorText.Length;
31-
if (startWithPoundSign) colorStringLength -= 1;
32-
int currentOffset = startWithPoundSign ? 1 : 0;
33-
// 可以采用的格式如下
34-
// #FFDFD991 8 个字符 存在 Alpha 通道
35-
// #DFD991 6 个字符
36-
// #FD92 4 个字符 存在 Alpha 通道
37-
// #DAC 3 个字符
38-
if (colorStringLength == 8
39-
|| colorStringLength == 6
40-
|| colorStringLength == 4
41-
|| colorStringLength == 3)
42-
{
43-
bool success;
44-
byte result;
45-
byte a;
46-
47-
int readCount;
48-
// #DFD991 6 个字符
49-
// #FFDFD991 8 个字符 存在 Alpha 通道
50-
//if (colorStringLength == 8 || colorStringLength == 6)
51-
if (colorStringLength > 5)
52-
{
53-
readCount = 2;
54-
}
55-
else
56-
{
57-
readCount = 1;
58-
}
59-
60-
bool includeAlphaChannel = colorStringLength == 8 || colorStringLength == 4;
61-
62-
if (includeAlphaChannel)
63-
{
64-
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
65-
if (!success) return false;
66-
a = result;
67-
currentOffset += readCount;
68-
}
69-
else
70-
{
71-
a = 0xFF;
72-
}
73-
74-
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
75-
if (!success) return false;
76-
byte r = result;
77-
currentOffset += readCount;
78-
79-
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
80-
if (!success) return false;
81-
byte g = result;
82-
currentOffset += readCount;
83-
84-
(success, result) = HexCharToNumber(hexColorText, currentOffset, readCount);
85-
if (!success) return false;
86-
byte b = result;
87-
88-
color = new Rgba32(r, g, b, a);
89-
return true;
90-
}
91-
92-
return false;
93-
}
94-
95-
static (bool success, byte result) HexCharToNumber(string input, int offset, int readCount)
96-
{
97-
Debug.Assert(readCount == 1 || readCount == 2, "要求 readCount 只能是 1 或者 2 的值,这是框架限制,因此不做判断");
98-
99-
byte result = 0;
100-
101-
for (int i = 0; i < readCount; i++, offset++)
102-
{
103-
var c = input[offset];
104-
byte n;
105-
if (c >= '0' && c <= '9')
106-
{
107-
n = (byte) (c - '0');
108-
}
109-
else if (c >= 'a' && c <= 'f')
110-
{
111-
n = (byte) (c - 'a' + 10);
112-
}
113-
else if (c >= 'A' && c <= 'F')
114-
{
115-
n = (byte) (c - 'A' + 10);
116-
}
117-
else
118-
{
119-
return default;
120-
}
121-
122-
result *= 16;
123-
result += n;
124-
}
125-
126-
if (readCount == 1)
127-
{
128-
result = (byte) (result * 16 + result);
129-
}
130-
131-
return (true, result);
132-
}
133-
}
18+
19+
}

0 commit comments

Comments
 (0)