/
ColorExt.cs
105 lines (89 loc) · 2.04 KB
/
ColorExt.cs
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
#region Header
// _,-'/-'/
// . __,-; ,'( '/
// \. `-.__`-._`:_,-._ _ , . ``
// `:-._,------' ` _,`--` -: `_ , ` ,' :
// `---..__,,--' (C) 2023 ` -'. -'
// # Vita-Nex [http://core.vita-nex.com] #
// {o)xxx|===============- # -===============|xxx(o}
// # #
#endregion
#region References
using System.Drawing;
using VitaNex;
#endregion
namespace System
{
public static class ColorExtUtility
{
public static Color ToColor(this KnownColor color)
{
return Color.FromKnownColor(color);
}
public static Color555 ToColor555(this Color value)
{
return value;
}
public static ushort ToArgb555(this Color value)
{
return ToColor555(value);
}
public static int ToRgb(this Color value)
{
return value.ToArgb() & 0x00FFFFFF;
}
public static Color Interpolate(this Color source, Color target, double percent)
{
if (percent <= 0.0)
{
return source;
}
if (percent >= 1.0)
{
return target;
}
var r = (int)(source.R + (target.R - source.R) * percent);
var g = (int)(source.G + (target.G - source.G) * percent);
var b = (int)(source.B + (target.B - source.B) * percent);
return Color.FromArgb(255, r, g, b);
}
public static Color FixBlackTransparency(this Color source)
{
if (source.IsEmpty || source.A <= 0 || source.R >= 0x08 || source.G >= 0x08 || source.B >= 0x08)
{
return source;
}
var r = source.R;
var g = source.G;
var b = source.B;
if (r != g || r != b)
{
var rd = 0x08 - r;
var gd = 0x08 - g;
var bd = 0x08 - b;
if (rd < gd && rd < bd)
{
r = 0x08;
}
else if (gd < rd && gd < bd)
{
g = 0x08;
}
else if (bd < rd && bd < gd)
{
b = 0x08;
}
else
{
r = g = b = 0x08;
}
}
else
{
r = g = b = 0x08;
}
source = Color.FromArgb(source.A, r, g, b);
return source;
}
}
}