Skip to content

Commit a033fd4

Browse files
improve adjustColor hue
1 parent a4124d3 commit a033fd4

File tree

2 files changed

+14
-29
lines changed

2 files changed

+14
-29
lines changed

lib/src/filter/adjust_color.dart

+3-26
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ Image adjustColor(Image src,
6767
exposure = exposure?.clamp(0, 1000);
6868
amount = amount.clamp(0, 1000);
6969

70-
const degToRad = 0.0174532925;
7170
const avgLumR = 0.5;
7271
const avgLumG = 0.5;
7372
const avgLumB = 0.5;
@@ -100,19 +99,6 @@ Image adjustColor(Image src,
10099
exposure = pow(2.0, exposure);
101100
}
102101

103-
late num hueR;
104-
late num hueG;
105-
late num hueB;
106-
if (hue != null) {
107-
hue *= degToRad;
108-
final s = sin(hue);
109-
final c = cos(hue);
110-
111-
hueR = (2.0 * c) / 3.0;
112-
hueG = (-sqrt(3.0) * s - c) / 3.0;
113-
hueB = ((sqrt(3.0) * s - c) + 1.0) / 3.0;
114-
}
115-
116102
final hsv = <num>[0.0, 0.0, 0.0];
117103

118104
for (final frame in src.frames) {
@@ -138,9 +124,10 @@ Image adjustColor(Image src,
138124
b *= tb;
139125
}
140126

141-
if (saturation != null) {
127+
if (saturation != null || hue != null) {
142128
rgbToHsv(r, g, b, hsv);
143-
hsv[1] *= saturation;
129+
hsv[0] += hue ?? 0.0;
130+
hsv[1] *= saturation ?? 1.0;
144131
hsvToRgb(hsv[0], hsv[1], hsv[2], hsv);
145132
r = hsv[0];
146133
g = hsv[1];
@@ -165,16 +152,6 @@ Image adjustColor(Image src,
165152
b = b * exposure;
166153
}
167154

168-
if (hue != null && hue != 0.0) {
169-
final hr = r * hueR + g * hueG + b * hueB;
170-
final hg = r * hueB + g * hueR + b * hueG;
171-
final hb = r * hueG + g * hueB + b * hueR;
172-
173-
r = hr;
174-
g = hg;
175-
b = hb;
176-
}
177-
178155
final msk =
179156
mask?.getPixel(p.x, p.y).getChannelNormalized(maskChannel) ?? 1;
180157
final blend = msk * amount;

lib/src/util/color_util.dart

+11-3
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,20 @@ void rgbToHsv(num r, num g, num b, List<num> hsv) {
217217
/// Returns a list \[r, g, b\] with values in the range \[0, 255\].
218218
void hsvToRgb(num h, num s, num v, List<num> rgb) {
219219
if (s == 0) {
220-
rgb[0] = v.clamp(0, 1);
221-
rgb[1] = v.clamp(0, 1);
222-
rgb[2] = v.clamp(0, 1);
220+
final g = v.clamp(0, 1);
221+
rgb[0] = g;
222+
rgb[1] = g;
223+
rgb[2] = g;
223224
return;
224225
}
225226

227+
while (h < 0) {
228+
h += 360;
229+
}
230+
while (h > 360) {
231+
h -= 360;
232+
}
233+
226234
h /= 60.0;
227235
final i = h.floor();
228236
final f = h - i;

0 commit comments

Comments
 (0)