forked from Nominom/BCnEncoder.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorChooser.cs
More file actions
187 lines (164 loc) · 5.05 KB
/
Copy pathColorChooser.cs
File metadata and controls
187 lines (164 loc) · 5.05 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
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
using System;
using BCnEncoder.Shared;
namespace BCnEncoder.Encoder
{
internal static class ColorChooser
{
public static int ChooseClosestColor4(ReadOnlySpan<ColorRgb24> colors, ColorRgba32 color, float rWeight, float gWeight, float bWeight, out float error)
{
ReadOnlySpan<float> d = stackalloc float[4] {
MathF.Abs(colors[0].r - color.r) * rWeight
+ MathF.Abs(colors[0].g - color.g) * gWeight
+ MathF.Abs(colors[0].b - color.b) * bWeight,
MathF.Abs(colors[1].r - color.r) * rWeight
+ MathF.Abs(colors[1].g - color.g) * gWeight
+ MathF.Abs(colors[1].b - color.b) * bWeight,
MathF.Abs(colors[2].r - color.r) * rWeight
+ MathF.Abs(colors[2].g - color.g) * gWeight
+ MathF.Abs(colors[2].b - color.b) * bWeight,
MathF.Abs(colors[3].r - color.r) * rWeight
+ MathF.Abs(colors[3].g - color.g) * gWeight
+ MathF.Abs(colors[3].b - color.b) * bWeight,
};
var b0 = d[0] > d[3] ? 1 : 0;
var b1 = d[1] > d[2] ? 1 : 0;
var b2 = d[0] > d[2] ? 1 : 0;
var b3 = d[1] > d[3] ? 1 : 0;
var b4 = d[2] > d[3] ? 1 : 0;
var x0 = b1 & b2;
var x1 = b0 & b3;
var x2 = b0 & b4;
var idx = x2 | ((x0 | x1) << 1);
error = d[idx];
return idx;
}
public static int ChooseClosestColor4AlphaCutoff(ReadOnlySpan<ColorRgb24> colors, ColorRgba32 color, float rWeight, float gWeight, float bWeight, int alphaCutoff, bool hasAlpha, out float error)
{
if (hasAlpha && color.a < alphaCutoff)
{
error = 0;
return 3;
}
ReadOnlySpan<float> d = stackalloc float[4] {
MathF.Abs(colors[0].r - color.r) * rWeight
+ MathF.Abs(colors[0].g - color.g) * gWeight
+ MathF.Abs(colors[0].b - color.b) * bWeight,
MathF.Abs(colors[1].r - color.r) * rWeight
+ MathF.Abs(colors[1].g - color.g) * gWeight
+ MathF.Abs(colors[1].b - color.b) * bWeight,
MathF.Abs(colors[2].r - color.r) * rWeight
+ MathF.Abs(colors[2].g - color.g) * gWeight
+ MathF.Abs(colors[2].b - color.b) * bWeight,
hasAlpha ? 999 :
MathF.Abs(colors[3].r - color.r) * rWeight
+ MathF.Abs(colors[3].g - color.g) * gWeight
+ MathF.Abs(colors[3].b - color.b) * bWeight,
};
var b0 = d[0] > d[2] ? 1 : 0;
var b1 = d[1] > d[3] ? 1 : 0;
var b2 = d[0] > d[3] ? 1 : 0;
var b3 = d[1] > d[2] ? 1 : 0;
var nb3 = d[1] > d[2] ? 0 : 1;
var b4 = d[0] > d[1] ? 1 : 0;
var b5 = d[2] > d[3] ? 1 : 0;
var idx = (nb3 & b4) | (b2 & b5) | (((b0 & b3) | (b1 & b2)) << 1);
error = d[idx];
return idx;
}
public static int ChooseClosestColor(Span<ColorRgb24> colors, ColorRgba32 color)
{
var closest = 0;
var closestError =
Math.Abs(colors[0].r - color.r)
+ Math.Abs(colors[0].g - color.g)
+ Math.Abs(colors[0].b - color.b);
for (var i = 1; i < colors.Length; i++)
{
var error =
Math.Abs(colors[i].r - color.r)
+ Math.Abs(colors[i].g - color.g)
+ Math.Abs(colors[i].b - color.b);
if (error < closestError)
{
closest = i;
closestError = error;
}
}
return closest;
}
public static int ChooseClosestColor(Span<ColorRgba32> colors, ColorRgba32 color)
{
var closest = 0;
var closestError =
Math.Abs(colors[0].r - color.r)
+ Math.Abs(colors[0].g - color.g)
+ Math.Abs(colors[0].b - color.b)
+ Math.Abs(colors[0].a - color.a);
for (var i = 1; i < colors.Length; i++)
{
var error =
Math.Abs(colors[i].r - color.r)
+ Math.Abs(colors[i].g - color.g)
+ Math.Abs(colors[i].b - color.b)
+ Math.Abs(colors[i].a - color.a);
if (error < closestError)
{
closest = i;
closestError = error;
}
}
return closest;
}
public static int ChooseClosestColorAlphaCutOff(Span<ColorRgba32> colors, ColorRgba32 color, byte alphaCutOff = 255 / 2)
{
if (color.a <= alphaCutOff)
{
return 3;
}
var closest = 0;
var closestError =
Math.Abs(colors[0].r - color.r)
+ Math.Abs(colors[0].g - color.g)
+ Math.Abs(colors[0].b - color.b);
for (var i = 1; i < colors.Length; i++)
{
if (i == 3) continue; // Skip transparent
var error =
Math.Abs(colors[i].r - color.r)
+ Math.Abs(colors[i].g - color.g)
+ Math.Abs(colors[i].b - color.b);
if (error < closestError)
{
closest = i;
closestError = error;
}
}
return closest;
}
public static int ChooseClosestColor(Span<ColorYCbCr> colors, ColorYCbCr color, float luminanceMultiplier = 4)
{
var closest = 0;
float closestError = 0;
var first = true;
for (var i = 0; i < colors.Length; i++)
{
var error = MathF.Abs(colors[i].y - color.y) * luminanceMultiplier
+ MathF.Abs(colors[i].cb - color.cb)
+ MathF.Abs(colors[i].cr - color.cr);
if (first)
{
closestError = error;
first = false;
}
else if (error < closestError)
{
closest = i;
closestError = error;
}
}
return closest;
}
public static int ChooseClosestColor(Span<ColorYCbCr> colors, ColorRgba32 color, float luminanceMultiplier = 4)
=> ChooseClosestColor(colors, new ColorYCbCr(color), luminanceMultiplier);
}
}