-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClrFilters.cs
127 lines (118 loc) · 3.71 KB
/
ClrFilters.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
using System;
/// <summary>
/// Implements filter functions for a color. Functions are to be supplied to an
/// image filter function. Both the lower and upper bounds are inclusive.
/// </summary>
public static class ClrFilters
{
/// <summary>
/// Filters a color by whether its alpha channel is in bounds.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterAlpha(
in Rgb c,
in float lb = 0.0f,
in float ub = 1.0f)
{
return c.Alpha >= lb && c.Alpha <= ub;
}
/// <summary>
/// Filters a color by whether its blue channel is in bounds.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterBlue(
in Rgb c,
in float lb = 0.0f,
in float ub = 1.0f)
{
return c.B >= lb && c.B <= ub;
}
/// <summary>
/// Filters a color by whether its chroma is in bounds.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterChroma(
in Rgb c,
in float lb = 0.0f,
in float ub = 135.0f)
{
Lab lab = Rgb.StandardToSrLab2(c);
float chromaSq = Lab.ChromaSq(lab);
return chromaSq >= (lb * lb) && chromaSq <= (ub * ub);
}
/// <summary>
/// Filters a color by whether its green channel is in bounds.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterGreen(
in Rgb c,
in float lb = 0.0f,
in float ub = 1.0f)
{
return c.G >= lb && c.G <= ub;
}
/// <summary>
/// Filters a color by whether its hue is in bounds.
/// Does not treat the hue as a periodic value.
/// If the color's chroma is less than epsilon, returns false.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterHue(
in Rgb c,
in float lb = 0.0f,
in float ub = 1.0f)
{
Lab lab = Rgb.StandardToSrLab2(c);
float a = lab.A;
float b = lab.B;
if ((a * a + b * b) < Utils.Epsilon) { return false; }
float hue = MathF.Atan2(b, a);
hue = hue < -0.0f ? hue + Utils.Tau : hue;
hue *= Utils.OneTau;
return hue >= lb && hue <= ub;
}
/// <summary>
/// Filters a color by whether its lightness is in bounds.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterLightness(
in Rgb c,
in float lb = 0.0f,
in float ub = 100.0f)
{
Lab lab = Rgb.StandardToSrLab2(c);
return lab.L >= lb && lab.L <= ub;
}
/// <summary>
/// Filters a color by whether its red channel is in bounds.
/// </summary>
/// <param name="c">color</param>
/// <param name="lb">lower bound</param>
/// <param name="ub">upper bound</param>
/// <returns>evaluation</returns>
public static bool FilterRed(
in Rgb c,
in float lb = 0.0f,
in float ub = 1.0f)
{
return c.R >= lb && c.R <= ub;
}
}