1- using System . Diagnostics ;
2- using DotNetCampus . MediaConverters . Contexts ;
1+ using DotNetCampus . MediaConverters . Contexts ;
32using SixLabors . ImageSharp . PixelFormats ;
43
54namespace 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