-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClrBlends.cs
165 lines (143 loc) · 5.15 KB
/
ClrBlends.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
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
using System;
/// <summary>
/// Implements blend functions for two colors. Functions are to be supplied to
/// an image blending function.
/// </summary>
public static class ClrBlends
{
/// <summary>
/// Blends two colors in LCH. The under color's lightness is retained while
/// its hue and saturation are blended according to the transparency of the
/// over and under color.
/// </summary>
/// <param name="under">under color</param>
/// <param name="over">over color</param>
/// <returns>blended color</returns>
public static Rgb Color(in Rgb under, in Rgb over)
{
float t = over.Alpha;
float v = under.Alpha;
if (t <= 0.0f) { return under; }
if (v <= 0.0f) { return over; }
Lch uLch = Rgb.StandardToSrLch(under);
Lch oLch = Rgb.StandardToSrLch(over);
float u = 1.0f - t;
float tuv = MathF.Min(1.0f, t + u * v);
float cc = u * uLch.C + t * oLch.C;
float ch = u * uLch.H + t * oLch.H;
ch -= MathF.Floor(ch);
Lch cLch = new(l: uLch.L, c: cc, h: ch, alpha: tuv);
return Rgb.SrLchToStandard(cLch);
}
/// <summary>
/// Blends two colors in LCH. The under color's lightness and chroma are
/// retained while its hue is blended according to the transparency of the
/// over and under color.
/// </summary>
/// <param name="under">under color</param>
/// <param name="over">over color</param>
/// <returns>blended color</returns>
public static Rgb Hue(in Rgb under, in Rgb over)
{
float t = over.Alpha;
float v = under.Alpha;
if (t <= 0.0f) { return under; }
if (v <= 0.0f) { return over; }
Lch uLch = Rgb.StandardToSrLch(under);
Lch oLch = Rgb.StandardToSrLch(over);
float u = 1.0f - t;
float tuv = MathF.Min(1.0f, t + u * v);
float ch = u * uLch.H + t * oLch.H;
ch -= MathF.Floor(ch);
Lch cLch = new(l: uLch.L, c: uLch.C, h: ch, alpha: tuv);
return Rgb.SrLchToStandard(cLch);
}
/// <summary>
/// Blends two colors in LCH. The under color's hue and chroma are retained
/// while its lightness is blended according to the transparency of the
/// over and under color.
/// </summary>
/// <param name="under">under color</param>
/// <param name="over">over color</param>
/// <returns>blended color</returns>
public static Rgb Luminosity(in Rgb under, in Rgb over)
{
float t = over.Alpha;
float v = under.Alpha;
if (t <= 0.0f) { return under; }
if (v <= 0.0f) { return over; }
Lch uLch = Rgb.StandardToSrLch(under);
Lch oLch = Rgb.StandardToSrLch(over);
float u = 1.0f - t;
float tuv = MathF.Min(1.0f, t + u * v);
float cl = u * uLch.L + t * oLch.L;
Lch cLch = new(l: cl, c: uLch.C, h: uLch.H, alpha: tuv);
return Rgb.SrLchToStandard(cLch);
}
/// <summary>
/// Replaces the under color with the over color except when the over color
/// is transparent.
/// </summary>
/// <param name="under">under color</param>
/// <param name="over">over color</param>
/// <returns>replacement color</returns>
public static Rgb Replace(in Rgb under, in Rgb over)
{
if (over.Alpha <= 0.0f) { return under; }
return over;
}
/// <summary>
/// Blends two colors in LCH. The under color's hue and lightness are
/// retained while its chroma is blended according to the transparency of
/// the over and under color.
/// </summary>
/// <param name="under">under color</param>
/// <param name="over">over color</param>
/// <returns>blended color</returns>
public static Rgb Saturation(in Rgb under, in Rgb over)
{
float t = over.Alpha;
float v = under.Alpha;
if (t <= 0.0f) { return under; }
if (v <= 0.0f) { return over; }
Lch uLch = Rgb.StandardToSrLch(under);
Lch oLch = Rgb.StandardToSrLch(over);
float u = 1.0f - t;
float tuv = MathF.Min(1.0f, t + u * v);
float cc = u * uLch.C + t * oLch.C;
Lch cLch = new(l: uLch.L, c: cc, h: uLch.H, alpha: tuv);
return Rgb.SrLchToStandard(cLch);
}
/// <summary>
/// Blends two colors in standard RGB.
/// </summary>
/// <param name="under">under color</param>
/// <param name="over">over color</param>
/// <returns>blended color</returns>
public static Rgb Standard(in Rgb under, in Rgb over)
{
float t = over.Alpha;
float u = 1.0f - t;
float v = under.Alpha;
float uv = u * v;
float tuv = t + uv;
if (tuv >= 1.0f)
{
return new Rgb(
uv * under.R + t * over.R,
uv * under.G + t * over.G,
uv * under.B + t * over.B,
1.0f);
}
else if (tuv > 0.0f)
{
float tuvInv = 1.0f / tuv;
return new Rgb(
(uv * under.R + t * over.R) * tuvInv,
(uv * under.G + t * over.G) * tuvInv,
(uv * under.B + t * over.B) * tuvInv,
tuv);
}
return Rgb.ClearBlack;
}
}