-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorMath.cs
More file actions
141 lines (119 loc) · 4.28 KB
/
Copy pathColorMath.cs
File metadata and controls
141 lines (119 loc) · 4.28 KB
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
namespace ColorWheel;
internal readonly record struct Hsl(double H, double S, double L);
internal sealed record HarmonyColor(
string Name,
Color Color,
double Angle,
string Role,
string Guidance);
internal static class ColorMath
{
public static Hsl ToHsl(Color color)
{
var r = color.R / 255d;
var g = color.G / 255d;
var b = color.B / 255d;
var max = Math.Max(r, Math.Max(g, b));
var min = Math.Min(r, Math.Min(g, b));
var delta = max - min;
var lightness = (max + min) / 2d;
if (delta == 0)
return new Hsl(0, 0, lightness);
var saturation = delta / (1d - Math.Abs(2d * lightness - 1d));
double hue;
if (max == r)
hue = 60d * (((g - b) / delta) % 6d);
else if (max == g)
hue = 60d * (((b - r) / delta) + 2d);
else
hue = 60d * (((r - g) / delta) + 4d);
return new Hsl(NormalizeHue(hue), saturation, lightness);
}
public static Color FromHsl(Hsl hsl)
{
var hue = NormalizeHue(hsl.H);
var saturation = Clamp01(hsl.S);
var lightness = Clamp01(hsl.L);
var chroma = (1d - Math.Abs(2d * lightness - 1d)) * saturation;
var x = chroma * (1d - Math.Abs((hue / 60d) % 2d - 1d));
var m = lightness - chroma / 2d;
var (r1, g1, b1) = hue switch
{
< 60d => (chroma, x, 0d),
< 120d => (x, chroma, 0d),
< 180d => (0d, chroma, x),
< 240d => (0d, x, chroma),
< 300d => (x, 0d, chroma),
_ => (chroma, 0d, x)
};
return Color.FromArgb(
ToByte((r1 + m) * 255d),
ToByte((g1 + m) * 255d),
ToByte((b1 + m) * 255d));
}
public static Color ShiftHue(Color color, double degrees)
{
var hsl = ToHsl(color);
return FromHsl(hsl with { H = hsl.H + degrees });
}
public static Color Mix(Color a, Color b, double amountOfB)
{
var t = Clamp01(amountOfB);
return Color.FromArgb(
ToByte(a.R + (b.R - a.R) * t),
ToByte(a.G + (b.G - a.G) * t),
ToByte(a.B + (b.B - a.B) * t));
}
public static Color PushSaturation(Color color, double multiplier)
{
var hsl = ToHsl(color);
return FromHsl(hsl with { S = Clamp01(hsl.S * multiplier) });
}
public static Color PushTemperature(Color color, bool warm)
{
var hsl = ToHsl(color);
return FromHsl(hsl with
{
H = hsl.H + (warm ? -10d : 10d),
S = Clamp01(hsl.S * 1.06d),
L = Clamp01(hsl.L + (warm ? 0.025d : -0.015d))
});
}
public static double RelativeLuminance(Color color)
{
static double Channel(byte value)
{
var v = value / 255d;
return v <= 0.03928d ? v / 12.92d : Math.Pow((v + 0.055d) / 1.055d, 2.4d);
}
return 0.2126d * Channel(color.R) + 0.7152d * Channel(color.G) + 0.0722d * Channel(color.B);
}
public static string Hex(Color color) => $"#{color.R:X2}{color.G:X2}{color.B:X2}";
public static Color BestTextColor(Color background)
{
return RelativeLuminance(background) > 0.48d ? Color.FromArgb(22, 24, 28) : Color.White;
}
public static double ContrastRatio(Color a, Color b)
{
var l1 = RelativeLuminance(a);
var l2 = RelativeLuminance(b);
var lighter = Math.Max(l1, l2);
var darker = Math.Min(l1, l2);
return (lighter + 0.05d) / (darker + 0.05d);
}
public static Color Chaos(Color color)
{
var seed = color.R * 73856093 ^ color.G * 19349663 ^ color.B * 83492791;
var r = (seed * 13 + 12345) & 255;
var g = (seed * 29 + 67890) & 255;
var b = (seed * 53 + 13579) & 255;
return Color.FromArgb(r, g, b);
}
private static double NormalizeHue(double hue)
{
hue %= 360d;
return hue < 0d ? hue + 360d : hue;
}
private static double Clamp01(double value) => Math.Min(1d, Math.Max(0d, value));
private static int ToByte(double value) => Math.Min(255, Math.Max(0, (int)Math.Round(value)));
}