1 #Region "Microsoft.VisualBasic::deb20948057d077ab9605a5ca6c33ec4, Microsoft.VisualBasic.Core\Extensions\Image\Colors\HexColor.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: ConvertToRbg, HexToColor, hexToRbgNew, OLE
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Drawing
44 Imports System.Globalization
45
46 Namespace Imaging
47
48     ''' <summary>
49     ''' Convert hex color string to RGB color
50     ''' </summary>
51     ''' <remarks>http://stackoverflow.com/questions/13356486/convert-hex-color-string-to-rgb-color</remarks>
52     Public Module HexColor
53
54         Public Function ConvertToRbg(HexColor As StringAs Color
55             Dim Red As String
56             Dim Green As String
57             Dim Blue As String
58             HexColor = HexColor.Trim("#"c)
59             '"&H" &
60             Red = GetHexInteger(Mid(HexColor, 1, 2))
61             Green = GetHexInteger(Mid(HexColor, 3, 2))
62             Blue = GetHexInteger(Mid(HexColor, 5, 2))
63             Return Color.FromArgb(Integer.Parse(Red), Integer.Parse(Green), Integer.Parse(Blue))
64         End Function
65
66         Public Function HexToColor(hexColor As StringAs Color
67             If hexColor.IndexOf("#"c) <> -1 Then
68                 hexColor = hexColor.Replace("#""")
69             End If
70             Dim red As Integer = 0
71             Dim green As Integer = 0
72             Dim blue As Integer = 0
73             If hexColor.Length = 6 Then
74                 red = Integer.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier)
75                 green = Integer.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier)
76                 blue = Integer.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier)
77             ElseIf hexColor.Length = 3 Then
78                 red = Integer.Parse(hexColor(0).ToString() + hexColor(0).ToString(), NumberStyles.AllowHexSpecifier)
79                 green = Integer.Parse(hexColor(1).ToString() + hexColor(1).ToString(), NumberStyles.AllowHexSpecifier)
80                 blue = Integer.Parse(hexColor(2).ToString() + hexColor(2).ToString(), NumberStyles.AllowHexSpecifier)
81             End If
82             Return Color.FromArgb(red, green, blue)
83         End Function
84
85         Public Function hexToRbgNew(Hex As StringAs Color
86             Hex = Strings.Replace(Hex, "#""")
87             Dim red As String = "&H" & Hex.Substring(0, 2)
88             Hex = Strings.Replace(Hex, red, "", , 1)
89             Dim green As String = "&H" & Hex.Substring(0, 2)
90             Hex = Strings.Replace(Hex, green, "", , 1)
91             Dim blue As String = "&H" & Hex.Substring(0, 2)
92             Hex = Strings.Replace(Hex, blue, "", , 1)
93             Return Color.FromArgb(red, green, blue)
94         End Function
95
96         ''' <summary>
97         ''' 
98         ''' </summary>
99         ''' <param name="c">Example: ``#ffffff``</param>
100         ''' <returns></returns>
101         Public Function OLE(c As StringAs Color
102             c = Strings.Replace(c, "#""")
103             c = "&H" & c
104             ColorTranslator.FromOle(c)
105         End Function
106     End Module
107 End Namespace