1 #Region "Microsoft.VisualBasic::b7edc7d3d2f39157f3c2597e704466f2, Microsoft.VisualBasic.Core\Extensions\Image\Colors\CytoscapeColor.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module HexColor
35     
36     '         Function: DeciamlToHexadeciaml, GetNumberFromNotation, HexadecimaltoDecimal, HexToARGB, IsHexadecimal
37     '                   IsNumber, ToHtmlColor
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 StringAs 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 IntegerAs 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         ''' <summary>
95         ''' To HTML color
96         ''' </summary>
97         ''' <param name="color"></param>
98         ''' <returns></returns>
99         <Extension> Public Function ToHtmlColor(color As Color) As String
100             'Dim RGBValue As Integer = color.ToArgb
101             'Dim HexValue = DeciamlToHexadeciaml(RGBValue)
102             'Return HexValue
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 StringAs 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 StringAs 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 CharAs Integer
147             If c = "A"Then
148                 Return 10
149             ElseIf c = "B"Then
150                 Return 11
151             ElseIf c = "C"Then
152                 Return 12
153             ElseIf c = "D"Then
154                 Return 13
155             ElseIf c = "E"Then
156                 Return 14
157             ElseIf c = "F"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 IntegerAs 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