tangxu
2024-10-22 7a0d1efa4784d176a32f182ecae671711e98ca92
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Manager;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Helper
{
    #region MaterialHelper
 
    public static class MaterialColorHelper
    {
        public static Color Lighten(this Color color, float percent)
        {
            float lighting = color.GetBrightness();
            lighting = lighting + (lighting * percent);
            if (lighting > 1.0)
            {
                lighting = 1;
            }
            else if (lighting <= 0)
            {
                lighting = 0.1f;
            }
            Color tintedColor = FromHsl(color.A, color.GetHue(), color.GetSaturation(), lighting);
 
            return tintedColor;
        }
 
        public static Color Darken(this Color color, float percent)
        {
            float lighting = color.GetBrightness();
            lighting = lighting - (lighting * percent);
            if (lighting > 1.0)
            {
                lighting = 1;
            }
            else if (lighting <= 0)
            {
                lighting = 0;
            }
            Color tintedColor = FromHsl(color.A, color.GetHue(), color.GetSaturation(), lighting);
 
            return tintedColor;
        }
 
        public static Color FromHsl(int alpha, float hue, float saturation, float lighting)
        {
            if (alpha is < 0 or > 255)
            {
                throw new ArgumentOutOfRangeException("alpha");
            }
            if (hue is < 0f or > 360f)
            {
                throw new ArgumentOutOfRangeException("hue");
            }
            if (saturation is < 0f or > 1f)
            {
                throw new ArgumentOutOfRangeException("saturation");
            }
            if (lighting is < 0f or > 1f)
            {
                throw new ArgumentOutOfRangeException("lighting");
            }
 
            if (0 == saturation)
            {
                return Color.FromArgb(alpha, Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255));
            }
 
            float fMax, fMid, fMin;
            int iSextant, iMax, iMid, iMin;
 
            if (0.5 < lighting)
            {
                fMax = lighting - (lighting * saturation) + saturation;
                fMin = lighting + (lighting * saturation) - saturation;
            }
            else
            {
                fMax = lighting + (lighting * saturation);
                fMin = lighting - (lighting * saturation);
            }
 
            iSextant = (int)Math.Floor(hue / 60f);
            if (300f <= hue)
            {
                hue -= 360f;
            }
            hue /= 60f;
            hue -= 2f * (float)Math.Floor((iSextant + 1f) % 6f / 2f);
            if (0 == iSextant % 2)
            {
                fMid = (hue * (fMax - fMin)) + fMin;
            }
            else
            {
                fMid = fMin - (hue * (fMax - fMin));
            }
 
            iMax = Convert.ToInt32(fMax * 255);
            iMid = Convert.ToInt32(fMid * 255);
            iMin = Convert.ToInt32(fMin * 255);
 
            return iSextant switch
            {
                1 => Color.FromArgb(alpha, iMid, iMax, iMin),
                2 => Color.FromArgb(alpha, iMin, iMax, iMid),
                3 => Color.FromArgb(alpha, iMin, iMid, iMax),
                4 => Color.FromArgb(alpha, iMid, iMin, iMax),
                5 => Color.FromArgb(alpha, iMax, iMin, iMid),
                _ => Color.FromArgb(alpha, iMax, iMid, iMin),
            };
        }
 
        public static Color RemoveAlpha(Color foreground, Color background)
        {
            if (foreground.A == 255)
            {
                return foreground;
            }
 
            double alpha = foreground.A / 255.0;
            double diff = 1.0 - alpha;
            return Color.FromArgb(255,
                (byte)((foreground.R * alpha) + (background.R * diff)),
                (byte)((foreground.G * alpha) + (background.G * diff)),
                (byte)((foreground.B * alpha) + (background.B * diff)));
        }
    }
 
    public static class MaterialDrawHelper
    {
        public static GraphicsPath CreateRoundRect(float x, float y, float width, float height, float radius)
        {
            GraphicsPath gp = new();
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90);
            gp.CloseFigure();
            return gp;
        }
 
        public static GraphicsPath CreateRoundRect(Rectangle rect, float radius)
        {
            return CreateRoundRect(rect.X, rect.Y, rect.Width, rect.Height, radius);
        }
 
        public static GraphicsPath CreateRoundRect(RectangleF rect, float radius)
        {
            return CreateRoundRect(rect.X, rect.Y, rect.Width, rect.Height, radius);
        }
 
        public static Color BlendColor(Color backgroundColor, Color frontColor, double blend)
        {
            double ratio = blend / 255d;
            double invRatio = 1d - ratio;
            int r = (int)((backgroundColor.R * invRatio) + (frontColor.R * ratio));
            int g = (int)((backgroundColor.G * invRatio) + (frontColor.G * ratio));
            int b = (int)((backgroundColor.B * invRatio) + (frontColor.B * ratio));
            return Color.FromArgb(r, g, b);
        }
 
        public static Color BlendColor(Color backgroundColor, Color frontColor)
        {
            return BlendColor(backgroundColor, frontColor, frontColor.A);
        }
 
        public static void DrawSquareShadow(Graphics g, Rectangle bounds)
        {
            using SolidBrush shadowBrush = new(Color.FromArgb(12, 0, 0, 0));
            GraphicsPath path;
            path = CreateRoundRect(new RectangleF(bounds.X - 3.5f, bounds.Y - 1.5f, bounds.Width + 6, bounds.Height + 6), 8);
            g.FillPath(shadowBrush, path);
            path = CreateRoundRect(new RectangleF(bounds.X - 2.5f, bounds.Y - 1.5f, bounds.Width + 4, bounds.Height + 4), 6);
            g.FillPath(shadowBrush, path);
            path = CreateRoundRect(new RectangleF(bounds.X - 1.5f, bounds.Y - 0.5f, bounds.Width + 2, bounds.Height + 2), 4);
            g.FillPath(shadowBrush, path);
            path = CreateRoundRect(new RectangleF(bounds.X - 0.5f, bounds.Y + 1.5f, bounds.Width + 0, bounds.Height + 0), 4);
            g.FillPath(shadowBrush, path);
            path = CreateRoundRect(new RectangleF(bounds.X - 0.5f, bounds.Y + 2.5f, bounds.Width + 0, bounds.Height + 0), 4);
            g.FillPath(shadowBrush, path);
            path.Dispose();
        }
 
        public static void DrawRoundShadow(Graphics g, Rectangle bounds)
        {
            using SolidBrush shadowBrush = new(Color.FromArgb(12, 0, 0, 0));
            g.FillEllipse(shadowBrush, new Rectangle(bounds.X - 2, bounds.Y - 1, bounds.Width + 4, bounds.Height + 6));
            g.FillEllipse(shadowBrush, new Rectangle(bounds.X - 1, bounds.Y - 1, bounds.Width + 2, bounds.Height + 4));
            g.FillEllipse(shadowBrush, new Rectangle(bounds.X - 0, bounds.Y - 0, bounds.Width + 0, bounds.Height + 2));
            g.FillEllipse(shadowBrush, new Rectangle(bounds.X - 0, bounds.Y + 2, bounds.Width + 0, bounds.Height + 0));
            g.FillEllipse(shadowBrush, new Rectangle(bounds.X - 0, bounds.Y + 1, bounds.Width + 0, bounds.Height + 0));
        }
 
        public interface MaterialControlI
        {
            int Depth { get; set; }
 
            MaterialSkinManager SkinManager { get; }
 
            MaterialMouseState MouseState { get; set; }
        }
 
        public enum MaterialMouseState
        {
            HOVER,
            DOWN,
            OUT
        }
    }
 
    #endregion
}