1
+ ' Licensed to the .NET Foundation under one or more agreements.
2
+ ' The .NET Foundation licenses this file to you under the MIT license.
3
+ ' See the LICENSE file in the project root for more information.
4
+ Option Explicit On
5
+ Option Infer Off
6
+ Option Strict On
7
+
8
+ Imports System.IO
9
+
10
+ Public Module CodeColorSelector
11
+
12
+ Private ReadOnly ColorMappingDictionary As New Dictionary( Of String , Color) From {
13
+ { "class name" , Color.FromArgb( 0 , 128 , 128 )},
14
+ { "comment" , Color.FromArgb( 0 , 100 , 0 )},
15
+ { "constant name" , Color.Black},
16
+ { "default" , Color.Black},
17
+ { "delegate name" , Color.FromArgb( 0 , 128 , 128 )},
18
+ { "enum name" , Color.FromArgb( 0 , 128 , 128 )},
19
+ { "enum member name" , Color.FromArgb( 0 , 128 , 128 )},
20
+ { "error" , Color.Red},
21
+ { "excluded code" , Color.FromArgb( 128 , 128 , 128 )},
22
+ { "event name" , Color.Black},
23
+ { "extension method name" , Color.Black},
24
+ { "field name" , Color.Black},
25
+ { "identifier" , Color.Black},
26
+ { "interface name" , Color.FromArgb( 0 , 128 , 128 )},
27
+ { "keyword" , Color.FromArgb( 0 , 0 , 255 )},
28
+ { "keyword - control" , Color.FromArgb( 143 , 8 , 196 )},
29
+ { "label name" , Color.Black},
30
+ { "local name" , Color.Black},
31
+ { "method name" , Color.Black},
32
+ { "module name" , Color.FromArgb( 0 , 128 , 128 )},
33
+ { "namespace name" , Color.Black},
34
+ { "number" , Color.Black},
35
+ { "operator" , Color.Black},
36
+ { "operator - overloaded" , Color.Black},
37
+ { "parameter name" , Color.Black},
38
+ { "preprocessor keyword" , Color.Gray},
39
+ { "preprocessor text" , Color.Black},
40
+ { "property name" , Color.Black},
41
+ { "punctuation" , Color.Black},
42
+ { "static symbol" , Color.Black},
43
+ { "string - escape character" , Color.Yellow},
44
+ { "string - verbatim" , Color.FromArgb( 128 , 0 , 0 )},
45
+ { "string" , Color.FromArgb( 163 , 21 , 21 )},
46
+ { "struct name" , Color.FromArgb( 43 , 145 , 175 )},
47
+ { "text" , Color.Black},
48
+ { "type parameter name" , Color.DarkGray},
49
+ { "xml doc comment - attribute name" , Color.FromArgb( 128 , 128 , 128 )},
50
+ { "xml doc comment - attribute quotes" , Color.FromArgb( 128 , 128 , 128 )},
51
+ { "xml doc comment - attribute value" , Color.FromArgb( 128 , 128 , 128 )},
52
+ { "xml doc comment - cdata section" , Color.FromArgb( 128 , 128 , 128 )},
53
+ { "xml doc comment - comment" , Color.FromArgb( 128 , 128 , 128 )},
54
+ { "xml doc comment - delimiter" , Color.FromArgb( 128 , 128 , 128 )},
55
+ { "xml doc comment - entity reference" , Color.FromArgb( 0 , 128 , 0 )},
56
+ { "xml doc comment - name" , Color.FromArgb( 128 , 128 , 128 )},
57
+ { "xml doc comment - processing instruction" , Color.FromArgb( 128 , 128 , 128 )},
58
+ { "xml doc comment - text" , Color.FromArgb( 0 , 128 , 0 )},
59
+ { "xml literal - attribute name" , Color.FromArgb( 128 , 128 , 128 )},
60
+ { "xml literal - attribute quotes" , Color.FromArgb( 128 , 128 , 128 )},
61
+ { "xml literal - attribute value" , Color.FromArgb( 128 , 128 , 128 )},
62
+ { "xml literal - cdata section" , Color.FromArgb( 128 , 128 , 128 )},
63
+ { "xml literal - comment" , Color.FromArgb( 128 , 128 , 128 )},
64
+ { "xml literal - delimiter" , Color.FromArgb( 100 , 100 , 185 )},
65
+ { "xml literal - embedded expression" , Color.FromArgb( 128 , 128 , 128 )},
66
+ { "xml literal - entity reference" , Color.FromArgb( 185 , 100 , 100 )},
67
+ { "xml literal - name" , Color.FromArgb( 132 , 70 , 70 )},
68
+ { "xml literal - processing instruction" , Color.FromArgb( 128 , 128 , 128 )},
69
+ { "xml literal - text" , Color.FromArgb( 85 , 85 , 85 )}
70
+ }
71
+
72
+ Private ReadOnly FullPath As String = Path.Combine(FileIO.SpecialDirectories.MyDocuments, "CodeHighlightingColorDictionary.csv" )
73
+
74
+ Private Sub UpdateColorDictionaryFromFile(FPath As String )
75
+ If Not File.Exists(FPath) Then
76
+ WriteColorDictionaryToFile(FPath)
77
+ Exit Sub
78
+ End If
79
+ Dim FileStream As FileStream = File.OpenRead(FPath)
80
+ Dim sr As New IO.StreamReader(FileStream)
81
+ sr.ReadLine()
82
+ While (sr.Peek() <> - 1 )
83
+ Dim line As String = sr.ReadLine()
84
+ Dim Split() As String = line.Split( ","c )
85
+ Dim key As String = Split( 0 )
86
+ Dim A As Integer = Convert.ToInt32(Split( 1 ), Globalization.CultureInfo.InvariantCulture)
87
+ Dim R As Integer = Convert.ToInt32(Split( 2 ), Globalization.CultureInfo.InvariantCulture)
88
+ Dim G As Integer = Convert.ToInt32(Split( 3 ), Globalization.CultureInfo.InvariantCulture)
89
+ Dim B As Integer = Convert.ToInt32(Split( 4 ), Globalization.CultureInfo.InvariantCulture)
90
+ ColorMappingDictionary( key ) = Color.FromArgb(A, R, G, B)
91
+ End While
92
+ sr.Close()
93
+ FileStream.Close()
94
+ End Sub
95
+
96
+ Private Sub WriteColorDictionaryToFile(FPath As String )
97
+ Dim FileStream As FileStream = File.OpenWrite(FPath)
98
+ Dim sw As New IO.StreamWriter(FileStream)
99
+ sw.WriteLine( $"Key,A,R,G,B" )
100
+ For Each kvp As KeyValuePair( Of String , Color) In ColorMappingDictionary
101
+ sw.WriteLine( $"{kvp.Key},{kvp.Value.A},{kvp.Value.R},{kvp.Value.G},{kvp.Value.B}" )
102
+ Next
103
+ sw.Flush()
104
+ sw.Close()
105
+ FileStream.Close()
106
+ End Sub
107
+
108
+ Friend Function GetColorFromName(Name As String ) As Color
109
+ Try
110
+ If String .IsNullOrWhiteSpace(Name) Then
111
+ Return ColorMappingDictionary( "default" )
112
+ End If
113
+ Return ColorMappingDictionary(Name)
114
+ Catch ex As Exception
115
+ Debug.Print( $"GetColorFromName missing({Name})" )
116
+ Stop
117
+ Return ColorMappingDictionary( "error" )
118
+ End Try
119
+ End Function
120
+
121
+ Public Function GetColorNameList() As Dictionary( Of String , Color).KeyCollection
122
+ Return ColorMappingDictionary.Keys
123
+ End Function
124
+
125
+ Public Sub SetColor(name As String , value As Color)
126
+ ColorMappingDictionary(name) = value
127
+ WriteColorDictionaryToFile(FullPath)
128
+ End Sub
129
+
130
+ Public Sub UpdateColorDictionaryFromFile()
131
+ UpdateColorDictionaryFromFile(FullPath)
132
+ End Sub
133
+
134
+ Public Sub WriteColorDictionaryToFile()
135
+ WriteColorDictionaryToFile(FullPath)
136
+ End Sub
137
+
138
+ End Module
0 commit comments