1 |
#Region "Microsoft.VisualBasic::b7edc7d3d2f39157f3c2597e704466f2, Microsoft.VisualBasic.Core\Extensions\Image\Colors\CytoscapeColor.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
#End Region
|
43 |
|
44 |
Imports System.Drawing
|
45 |
Imports System.Runtime.CompilerServices
|
46 |
Imports sys = System.Math
|
47 |
|
48 |
Namespace Imaging
|
49 |
|
50 |
Partial Module HexColor
|
51 |
|
52 |
#Region "CONVERSION FROM DECIMAL TO HEXADECIMAL AND VICE VERSA"
|
53 |
|
54 |
Private Function HexadecimaltoDecimal(hexadecimal As String) As Integer
|
55 |
Dim result As Integer = 0
|
56 |
|
57 |
For i As Integer = 0 To hexadecimal.Length - 1
|
58 |
result += Convert.ToInt32(
|
59 |
GetNumberFromNotation(hexadecimal(i)) * sys.Pow(16, hexadecimal.Length - (i + 1)))
|
60 |
Next
|
61 |
|
62 |
Return Convert.ToInt32(result)
|
63 |
End Function
|
64 |
|
65 |
Private Function DeciamlToHexadeciaml(number As Integer) As String
|
66 |
Dim hexvalues As String() = {
|
67 |
"0", "1", "2", "3", "4", "5",
|
68 |
"6", "7", "8", "9", "A", "B",
|
69 |
"C", "D", "E", "F"
|
70 |
}
|
71 |
Dim result As String = "", final As String = ""
|
72 |
Dim [rem] As Integer = 0, div As Integer = 0
|
73 |
|
74 |
While True
|
75 |
[rem] = (number Mod 16)
|
76 |
result &= hexvalues([rem]).ToString()
|
77 |
|
78 |
If number < 16 Then
|
79 |
Exit While
|
80 |
End If
|
81 |
|
82 |
number = (number \ 16)
|
83 |
End While
|
84 |
|
85 |
For i As Integer = (result.Length - 1) To 0 Step -1
|
86 |
final &= result(i)
|
87 |
Next
|
88 |
|
89 |
Return final
|
90 |
End Function
|
91 |
|
92 |
#End Region
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
<Extension> Public Function ToHtmlColor(color As Color) As String
|
100 |
|
101 |
|
102 |
|
103 |
Dim colorString$ = String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B)
|
104 |
Return colorString
|
105 |
End Function
|
106 |
|
107 |
#Region "CHECKING TO FORMAT OF THE INPUT THAT WHETHER IT IS IN THE CORRECT FORMAT (DECIAML/HEXADECIMAL)"
|
108 |
|
109 |
Private Function IsNumber(number As String) As Boolean
|
110 |
If number.Length = 0 Then
|
111 |
Return False
|
112 |
End If
|
113 |
|
114 |
For i As Integer = 0 To number.Length - 1
|
115 |
If Not ([Char].IsDigit(number(i))) Then
|
116 |
Return False
|
117 |
End If
|
118 |
Next
|
119 |
|
120 |
Return True
|
121 |
End Function
|
122 |
|
123 |
Private Function IsHexadecimal(hexadecimal As String) As Boolean
|
124 |
If hexadecimal.Length = 0 Then
|
125 |
Return False
|
126 |
End If
|
127 |
|
128 |
For i As Integer = 0 To hexadecimal.Length - 1
|
129 |
If Not (([Char].IsDigit(hexadecimal(i))) OrElse
|
130 |
(hexadecimal(i) = "A"c) OrElse
|
131 |
(hexadecimal(i) = "B"c) OrElse
|
132 |
(hexadecimal(i) = "C"c) OrElse
|
133 |
(hexadecimal(i) = "D"c) OrElse
|
134 |
(hexadecimal(i) = "E"c) OrElse
|
135 |
(hexadecimal(i) = "F"c)) Then
|
136 |
|
137 |
Return False
|
138 |
End If
|
139 |
Next
|
140 |
|
141 |
Return True
|
142 |
End Function
|
143 |
|
144 |
#End Region
|
145 |
|
146 |
Private Function GetNumberFromNotation(c As Char) As Integer
|
147 |
If c = "A"c Then
|
148 |
Return 10
|
149 |
ElseIf c = "B"c Then
|
150 |
Return 11
|
151 |
ElseIf c = "C"c Then
|
152 |
Return 12
|
153 |
ElseIf c = "D"c Then
|
154 |
Return 13
|
155 |
ElseIf c = "E"c Then
|
156 |
Return 14
|
157 |
ElseIf c = "F"c Then
|
158 |
Return 15
|
159 |
End If
|
160 |
|
161 |
Return Convert.ToInt32(c.ToString())
|
162 |
End Function
|
163 |
|
164 |
Public Function HexToARGB(HexValue As String, alpha As Integer) As Color
|
165 |
Dim r As Color = Color.FromArgb(HexadecimaltoDecimal(HexValue))
|
166 |
r = Color.FromArgb(alpha, r)
|
167 |
Return r
|
168 |
End Function
|
169 |
End Module
|
170 |
End Namespace
|