1
1
import '../image/image.dart' ;
2
2
import '../util/min_max.dart' ;
3
3
4
- enum Modes {
5
- highlights,
6
- shadows
7
- }
8
- /// Solarize the colors of the [src] image - Started from invert.dart file.
9
- Image solarize (Image src, {required int threshold, required int md}) {
10
- /// threshold should be int from 1 to 254; mode should either '0' or '1'
11
- /// mode '0' is normal solarization, bright objetcs become black
12
- /// mode '1' will solarize the shadows like a Man Ray photograph
4
+ enum SolarizeMode { highlights, shadows }
5
+
6
+ /// Solarize the colors of the [src] image.
7
+ /// {threshold} should be int from 1 to 254. If {mode} is
8
+ /// SolarizeMode.highlights, bright objects become black, otherwise it will
9
+ /// solarize shadows.
10
+ Image solarize (Image src,
11
+ {required int threshold, SolarizeMode mode = SolarizeMode .highlights}) {
13
12
final max = src.maxChannelValue;
14
- final trld = (max * (threshold / 255 )).toInt ();
15
- final mode = Modes .values[md].toString ();
13
+ final thresholdRange = (max * (threshold / 255 )).toInt ();
16
14
for (final frame in src.frames) {
17
15
if (src.hasPalette) {
18
16
final p = frame.palette! ;
19
17
final numColors = p.numColors;
20
18
for (var i = 0 ; i < numColors; ++ i) {
21
- if (mode == " highlights" ) {
22
- if (p.getGreen (i) > trld ) {
19
+ if (mode == SolarizeMode . highlights) {
20
+ if (p.getGreen (i) > thresholdRange ) {
23
21
final r = max - p.getRed (i);
24
22
final g = max - p.getGreen (i);
25
23
final b = max - p.getBlue (i);
@@ -31,7 +29,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
31
29
p.setRgb (i, r, g, b);
32
30
}
33
31
} else {
34
- if (p.getGreen (i) < trld ) {
32
+ if (p.getGreen (i) < thresholdRange ) {
35
33
final r = max - p.getRed (i);
36
34
final g = max - p.getGreen (i);
37
35
final b = max - p.getBlue (i);
@@ -47,8 +45,8 @@ Image solarize(Image src, {required int threshold, required int md}) {
47
45
} else {
48
46
if (max != 0.0 ) {
49
47
for (final p in frame) {
50
- if (mode == " highlights" ) {
51
- if (p.g > trld ) {
48
+ if (mode == SolarizeMode . highlights) {
49
+ if (p.g > thresholdRange ) {
52
50
p
53
51
..r = max - p.r
54
52
..g = max - p.g
@@ -60,7 +58,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
60
58
..b = p.b;
61
59
}
62
60
} else {
63
- if (p.g < trld ) {
61
+ if (p.g < thresholdRange ) {
64
62
p
65
63
..r = max - p.r
66
64
..g = max - p.g
@@ -77,8 +75,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
77
75
}
78
76
}
79
77
80
- /// I used code from normalize here with the original
81
- /// max value and zero to improve contrast
78
+ /// max value and zero are used to improve contrast
82
79
const num a = 0 ;
83
80
final num b = max;
84
81
@@ -94,7 +91,7 @@ Image solarize(Image src, {required int threshold, required int md}) {
94
91
final fM = mx.toDouble ();
95
92
96
93
if (mn != a || mx != b) {
97
- for (var frame in src.frames) {
94
+ for (final frame in src.frames) {
98
95
for (final p in frame) {
99
96
p
100
97
..r = (p.r - fm) / (fM - fm) * (b - a) + a
0 commit comments