yangyin
2024-10-25 1e65c49ba929103e79d87322ca1a3573c2b532e2
1
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Helper;
using System;
using System.Drawing;
using System.Reflection;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Extension
{
    #region MaterialExtension
 
    public static class MaterialExtension
    {
        public static bool HasProperty(this object objectToCheck, string propertyName)
        {
            try
            {
                Type type = objectToCheck.GetType();
 
                return type.GetProperty(propertyName) != null;
            }
            catch (AmbiguousMatchException)
            {
                // ambiguous means there is more than one result,
                // which means: a method with that name does exist
                return true;
            }
        }
 
        public static bool IsMaterialControl(this object obj)
        {
            if (obj is MaterialDrawHelper.MaterialControlI)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        public static string ToSecureString(this string plainString)
        {
            if (plainString == null)
            {
                return null;
            }
 
            string secureString = "";
            for (uint i = 0; i < plainString.Length; i++)
            {
                secureString += '\u25CF';
            }
            return secureString;
        }
 
        public static Color ToColor(this int argb)
        {
            return Color.FromArgb(
                (argb & 0xFF0000) >> 16,
                (argb & 0x00FF00) >> 8,
                 argb & 0x0000FF);
        }
 
        public static Color RemoveAlpha(this Color color)
        {
            return Color.FromArgb(color.R, color.G, color.B);
        }
 
        public static int PercentageToColorComponent(this int percentage)
        {
            return (int)(percentage / 100d * 255d);
        }
 
        // Simulate Clamp function from .NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8
        // https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Math.cs,538
        public static int Clamp(this int value, int min, int max)
        {
            if (min > max)
            {
                throw new ArgumentException(string.Format("'{0}' cannot be greater than {1}.", min, max));
            }
 
            if (value < min)
            {
                return min;
            }
            else if (value > max)
            {
                return max;
            }
 
            return value;
        }
 
        public static int GetIntFromHexColor(string HexColor)
        {
            //#ffffff // #ffffffff
            try
            {
                if (HexColor.Length is <= 9 or >= 2)
                {
                    if (HexColor[0] == '#')
                    {
                        return Convert.ToInt32(HexColor.Replace("#", "0x"), 16);
                    }
                    else
                    {
                        return Convert.ToInt32(HexColor.Insert(0, "0x"), 16);
                    }
 
                }
            }
            catch
            {
                return 0x00;
            }
 
            return 0x00;
        }
    }
 
    #endregion
}