-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageAnalysis.java
495 lines (444 loc) · 23.9 KB
/
ImageAnalysis.java
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package org.bot;
import lombok.RequiredArgsConstructor;
import org.bot.enums.GameConstantsEnum;
import org.bot.utils.ImageUtils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Comparator;
import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.bot.GameInfo.HEIGHT;
import static org.bot.GameInfo.WIDTH;
import static org.bot.enums.ColorsEnum.*;
import static org.bot.enums.GameConstantsEnum.*;
/**
* This class performing base analysis of football field scheme image
*/
public class ImageAnalysis {
private static final Comparator<Point> POINT_COMPARATOR = Comparator.comparingDouble(Point::getY)
.thenComparingDouble(Point::getX);
private static final double SEARCH_RADIUS_NEARLY_PLAYER = 8;
private static final int X_DISTANCE_NEARLY_PLAYER = 6;
private static final int Y_DISTANCE_NEARLY_PLAYER = 9;
private static final int OVERLAY_PLAYER_BASE_DISTANCE = 5;
private static final int CORNER_POINT_DISTANCE = 25;
private static final int BALL_POSSESSION_RADIUS = 10;
private final BufferedImage bufferedImage;
private final SortedSet<Point> playmates;
private final SortedSet<Point> opposites;
private Point activePlayer;
private final SortedSet<Point> activePlayerCandidates = new TreeSet<>(POINT_COMPARATOR);
private Point ball;
private boolean isPlaymateBallPossession;
private boolean isNobodyBallPossession;
private boolean isShadingField;
private boolean isCorner;
private GameConstantsEnum playmateSide;
private final int[][] pixels;
@RequiredArgsConstructor
private static final class SearchConditions {
final Point point;
final boolean isBall;
final boolean isPlaymate;
final boolean isOpposite;
BaseData baseData;
private record BaseData(int x, int y, int pixel) {
}
}
public ImageAnalysis(BufferedImage bufferedImage) {
this.bufferedImage = bufferedImage;
this.playmates = new TreeSet<>(POINT_COMPARATOR);
this.opposites = new TreeSet<>(POINT_COMPARATOR);
this.pixels = new int[WIDTH][HEIGHT];
}
/**
* Concept:<p>
* 1-Step: Iterating by {@code bufferedImage} rectangle top-down and left right straight
* and get pixel integer color with x,y coordinates. <p>
* 2-Step: Check whether pixel represent bound of player.<p>
* 3-Step: Check whether exist Point which can be center coordinates of Player
* and located below and to the right then given (x,y), if Yes - current {@code x} replace {@code endPlayerBound}
* and continue for cycle, if No - starting 4 Step.<p>
* 4-Step: Find middle of bound Player and trying to add in Set new Player via {@code setPlayerCoordinate} method.<p>
* 5-Step: If already exist point which locate in radius = 8 from given (x, y) point then set of players not modify,
* else add new coordinate in corresponding Set in depend on player belong to.<p>
*/
public GameInfo analyse() {
setShadingField();
baseAnalyseRun();
searchOverlayPlayers();
setPlayerPossessionOfBall();
setPlaymateSide();
setCornerState();
searchShadingActivePlayer();
return new GameInfo(activePlayer, ball, isPlaymateBallPossession, isNobodyBallPossession, isShadingField,
isCorner, playmateSide, playmates, opposites, pixels);
}
private void searchShadingActivePlayer() {
if (ball != null && activePlayer == null) {
// filter false active players
opposites.forEach(opponent -> activePlayerCandidates.retainAll(activePlayerCandidates.stream()
.filter(point -> point.distance(opponent.getX(), opponent.getY()) > SEARCH_RADIUS_NEARLY_PLAYER)
.collect(Collectors.toSet())));
// find potential candidates
SortedSet<Point> potentialCandidates = new TreeSet<>(POINT_COMPARATOR);
activePlayerCandidates.forEach(player -> potentialCandidates.addAll(
playmates.stream()
.filter(point -> point.distance(player.getX(), player.getY()) < SEARCH_RADIUS_NEARLY_PLAYER)
.collect(Collectors.toSet())
));
// remove potentialCandidates from playmates
playmates.removeAll(potentialCandidates);
// if empty then set closest for ball
Optional<Point> candidate = potentialCandidates.stream().findFirst();
if (candidate.isEmpty() || candidate.get().distance(ball) > ((double) HEIGHT / 2)) {
activePlayer = playmates.stream().min(Comparator.comparing(player -> player.distance(ball)))
.orElse(null);
}
else activePlayer = candidate.get();
}
}
private void setCornerState() {
if (ball != null) {
isCorner = LEFT_TOP_CORNER.getPoint().distance(ball) < CORNER_POINT_DISTANCE
|| LEFT_BOTTOM_CORNER.getPoint().distance(ball) < CORNER_POINT_DISTANCE
|| RIGHT_TOP_CORNER.getPoint().distance(ball) < CORNER_POINT_DISTANCE
|| RIGHT_BOTTOM_CORNER.getPoint().distance(ball) < CORNER_POINT_DISTANCE;
}
}
@SuppressWarnings({"java:S127", "java:S3776"})
private void baseAnalyseRun() {
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int pixel = ImageUtils.getRGB(bufferedImage, x, y);
pixels[x][y] = pixel;
int xPrev = x;
if (isBoundPlayerColor(pixel)) {
if (isExistBottomRightNearPoint(x, y)) {
x = getEndPlayerBound(x + 1, y, this::isBoundPlayerColor);
addSkippedValuesInPixels(xPrev, x, y);
} else {
x = addPlayer(x, y, this::isBoundPlayerColor, false);
addSkippedValuesInPixels(xPrev, x, y);
}
} else if (activePlayer == null && isActivePlayerColor(pixel)) {
if (isShadingField) {
activePlayerCandidates.add(new Point(x, y));
} else {
x = addPlayer(x, y, this::isActivePlayerColor, true);
addSkippedValuesInPixels(xPrev, x, y);
}
} else if (ball == null && isBallColor(pixel)) {
ball = new Point(x + 1, y + 5);
}
}
}
}
private void addSkippedValuesInPixels(int xPrev, int x, int y) {
if (xPrev < x) {
for (int i = xPrev + 1; i <= x; i++) {
pixels[i][y] = ImageUtils.getRGB(bufferedImage, i, y);
}
}
}
private void setShadingField() {
int shades = 0;
shades = isShadingFieldColor(ImageUtils.getRGB(bufferedImage, 10, 10)) ? shades + 1 : shades;
shades = isShadingFieldColor(ImageUtils.getRGB(bufferedImage, WIDTH - 10, 10)) ? shades + 1 : shades;
shades = isShadingFieldColor(ImageUtils.getRGB(bufferedImage, 10, HEIGHT - 10)) ? shades + 1 : shades;
shades = isShadingFieldColor(ImageUtils.getRGB(bufferedImage, WIDTH - 10, HEIGHT - 10)) ? shades + 1 : shades;
isShadingField = shades > 1;
}
private void setPlaymateSide() {
Stream<Point> players = Stream.concat(playmates.stream(), opposites.stream());
Point mostLeftPlayer = players.min(Comparator.comparing(Point::getX)).orElse(null);
if (playmates.contains(mostLeftPlayer)) {
playmateSide = GameConstantsEnum.LEFT_PLAYMATE_SIDE;
} else if (opposites.contains(mostLeftPlayer)) {
playmateSide = GameConstantsEnum.RIGHT_PLAYMATE_SIDE;
}
}
private void setPlayerPossessionOfBall() {
if (ball == null) {
isNobodyBallPossession = true;
return;
}
Comparator<Point> closestToBall = Comparator.comparing(point -> point.distance(ball));
Point playmate = playmates.stream().min(closestToBall).orElse(new Point(Integer.MAX_VALUE, Integer.MAX_VALUE));
Point opposite = opposites.stream().min(closestToBall).orElse(new Point(Integer.MAX_VALUE, Integer.MAX_VALUE));
double playmateDistance = playmate.distance(ball);
double oppositeDistance = opposite.distance(ball);
if (Math.abs(playmateDistance - oppositeDistance) <= 1
|| (playmateDistance > BALL_POSSESSION_RADIUS && oppositeDistance > BALL_POSSESSION_RADIUS)) {
isNobodyBallPossession = true;
} else if (playmateDistance < oppositeDistance) {
isPlaymateBallPossession = true;
}
}
private void searchOverlayPlayers() {
boolean playersNotEnough = opposites.size() < 11 || playmates.size() < 11;
if (ball != null && playersNotEnough) {
// looking in the vicinity of the ball
searchOverlayPlayersBase(
new SearchConditions(ball, true, false, false),
this::addOverlayPlayersBase
);
}
if (playersNotEnough) {
// if playmate on opposite or vice versa
searchOtherPlayerOverlay();
}
// if ball completely hiding of opposite
if (ball != null) {
searchOverlayPlayersBase(
new SearchConditions(ball, true, false, false),
this::addFullOverlayBallPlayer
);
}
}
private void searchOtherPlayerOverlay() {
if (playmates.size() < 11) {
opposites.forEach(opponent -> searchOverlayPlayersBase(
new SearchConditions(opponent, false, true, false),
this::addOverlayPlayersBase
));
}
if (opposites.size() < 11) {
playmates.forEach(playmate -> searchOverlayPlayersBase(
new SearchConditions(playmate, false, false, true),
this::addOverlayPlayersBase
));
}
}
private void searchOverlayPlayersBase(SearchConditions search, Consumer<SearchConditions> addOverlayPlayer) {
Point leftTopScanPoint = new Point(
search.point.x - OVERLAY_PLAYER_BASE_DISTANCE, search.point.y - OVERLAY_PLAYER_BASE_DISTANCE);
Point bottomRightScanPoint = new Point(
search.point.x + OVERLAY_PLAYER_BASE_DISTANCE, search.point.y + OVERLAY_PLAYER_BASE_DISTANCE);
if (leftTopScanPoint.x >= 0 && leftTopScanPoint.y >= 0
&& bottomRightScanPoint.x < WIDTH && bottomRightScanPoint.y < HEIGHT) {
for (int y = leftTopScanPoint.y; y <= bottomRightScanPoint.y; y++) {
for (int x = leftTopScanPoint.x; x <= bottomRightScanPoint.x; x++) {
int pixel = pixels[x][y];
search.baseData = new SearchConditions.BaseData(x, y, pixel);
addOverlayPlayer.accept(search);
}
}
}
}
private void addOverlayPlayersBase(SearchConditions search) {
if ((search.isOpposite || search.isBall) && opposites.size() < 11
&& (isOverlayOppositePlayerColor(search.baseData.pixel)
|| isOppositeColor(search.baseData.pixel))) {
boolean isExistPoint = opposites.stream()
.anyMatch(p -> p.distance(search.baseData.x, search.baseData.y) < OVERLAY_PLAYER_BASE_DISTANCE);
if (!isExistPoint) {
opposites.add(new Point(search.baseData.x, search.baseData.y));
}
}
if ((search.isPlaymate || search.isBall) && playmates.size() < 11
&& (isOverlayPlaymatePlayerColor(search.baseData.pixel)
|| isPlaymateColor(search.baseData.pixel))) {
boolean isExistPoint = playmates.stream()
.anyMatch(p -> p.distance(search.baseData.x, search.baseData.y) < OVERLAY_PLAYER_BASE_DISTANCE);
if (!isExistPoint) {
playmates.add(new Point(search.baseData.x, search.baseData.y));
}
}
}
private void addFullOverlayBallPlayer(SearchConditions search) {
if (opposites.size() < 11 && search.isBall
&& (isBoundPlayerColor(search.baseData.pixel) || isOverlayBoundPlayerColor(search.baseData.pixel))) {
boolean isExistPoint = opposites.stream()
.anyMatch(p -> p.distance(search.baseData.x, search.baseData.y) < OVERLAY_PLAYER_BASE_DISTANCE);
if (!isExistPoint) {
opposites.add(new Point(search.baseData.x, search.baseData.y));
}
}
}
private int addPlayer(int x, int y, Function<Integer, Boolean> isBoundColor, boolean isActivePlayer) {
int endPlayerBound = getEndPlayerBound(x + 1, y, isBoundColor);
// if players stick together
if (endPlayerBound - x > 7) {
endPlayerBound = endPlayerBound - (endPlayerBound - x) / 2;
}
int middlePlayerBound = endPlayerBound - (endPlayerBound - x) / 2;
// fix double-crossing bound
if (checkDoubleCrossingBound(middlePlayerBound, y + 4, y - 4, isActivePlayer)) {
return x;
}
boolean isAdd = setPlayerCoordinate(middlePlayerBound, y + 4, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y - 4, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y + 3, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y - 3, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y + 2, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y - 2, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y + 1, isActivePlayer)
|| setPlayerCoordinate(middlePlayerBound, y - 1, isActivePlayer);
return isAdd ? endPlayerBound : x;
}
private boolean checkDoubleCrossingBound(int x, int topRange, int bottomRange, boolean isActivePlayer) {
if (isShadingField) {
return false;
}
if (bottomRange > 0 && topRange < HEIGHT) {
for (int y = bottomRange; y <= topRange; y++) {
int pixel = ImageUtils.getRGB(bufferedImage, x, y);
if ((!isActivePlayer && isActivePlayerColor(pixel)) || (isActivePlayer && isBoundPlayerColor(pixel))) {
return true;
}
}
}
return false;
}
// if player already was added
private boolean isExistBottomRightNearPoint(int x, int y) {
if (y + 4 < HEIGHT) {
int pixel = ImageUtils.getRGB(bufferedImage, x, y + 4);
boolean isPlaymate = isPlaymateColor(pixel);
Stream<Point> players = isPlaymate ? playmates.stream() : opposites.stream();
return players.anyMatch(
p -> (p.x >= x && p.x - x < X_DISTANCE_NEARLY_PLAYER) && (p.y >= y && p.y - y < Y_DISTANCE_NEARLY_PLAYER));
}
return false;
}
private int getEndPlayerBound(int x, int y, Function<Integer, Boolean> isBoundColor) {
if (x < WIDTH) {
int pixel = ImageUtils.getRGB(bufferedImage, x, y);
if (Boolean.TRUE.equals(isBoundColor.apply(pixel))) {
return getEndPlayerBound(x + 1, y, isBoundColor);
}
return x - 1;
}
return x - 1;
}
private boolean setPlayerCoordinate(int x, int y, boolean isActivePlayer) {
if (y > 0 && y < HEIGHT) {
int pixel = ImageUtils.getRGB(bufferedImage, x, y);
Predicate<SortedSet<Point>> isExistPoint = players -> players.stream()
.anyMatch(point -> point.distance(x, y) < SEARCH_RADIUS_NEARLY_PLAYER);
if (isActivePlayer) {
activePlayer = new Point(x, y);
return playmates.add(activePlayer);
}
int test1 = ImageUtils.getRGB(bufferedImage, Math.min(x + 1, WIDTH - 1), Math.min(y + 1, HEIGHT - 1));
int test2 = ImageUtils.getRGB(bufferedImage, Math.min(x + 2, WIDTH - 1), Math.min(y + 2, HEIGHT - 1));
int test3 = ImageUtils.getRGB(bufferedImage, Math.min(x + 3, WIDTH - 1), Math.min(y + 3, HEIGHT - 1));
if (isPlaymateColor(pixel)) {
return (isPlaymateColor(test1) || isPlaymateColor(test2) || isPlaymateColor(test3))
&& !isExistPoint.test(playmates) && playmates.add(new Point(x, y));
} else if (isOppositeColor(pixel)) {
return (isOppositeColor(test1) || isOppositeColor(test2) || isOppositeColor(test3))
&& !isExistPoint.test(opposites) && opposites.add(new Point(x, y));
}
return false;
}
return false;
}
private boolean isBoundPlayerColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
if (isShadingField) {
return SHADING_BOUND_OF_PLAYER_COLOR_LOWER.getColor().getRed() <= r && BOUND_OF_PLAYER_COLOR_UPPER.getColor().getRed() >= r
&& SHADING_BOUND_OF_PLAYER_COLOR_LOWER.getColor().getGreen() <= g && BOUND_OF_PLAYER_COLOR_UPPER.getColor().getGreen() >= g
&& SHADING_BOUND_OF_PLAYER_COLOR_LOWER.getColor().getBlue() <= b && BOUND_OF_PLAYER_COLOR_UPPER.getColor().getBlue() >= b;
}
return BOUND_OF_PLAYER_COLOR_LOWER.getColor().getRed() < r && BOUND_OF_PLAYER_COLOR_UPPER.getColor().getRed() > r
&& BOUND_OF_PLAYER_COLOR_LOWER.getColor().getGreen() < g && BOUND_OF_PLAYER_COLOR_UPPER.getColor().getGreen() > g
&& BOUND_OF_PLAYER_COLOR_LOWER.getColor().getBlue() < b && BOUND_OF_PLAYER_COLOR_UPPER.getColor().getBlue() > b;
}
private boolean isPlaymateColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
if (isShadingField) {
return SHADING_PLAYMATE_COLOR_LOWER.getColor().getRed() <= r && SHADING_PLAYMATE_COLOR_UPPER.getColor().getRed() >= r
&& SHADING_PLAYMATE_COLOR_LOWER.getColor().getGreen() <= g && SHADING_PLAYMATE_COLOR_UPPER.getColor().getGreen() >= g
&& SHADING_PLAYMATE_COLOR_LOWER.getColor().getBlue() <= b && SHADING_PLAYMATE_COLOR_UPPER.getColor().getBlue() >= b;
}
return PLAYMATE_COLOR_LOWER.getColor().getRed() < r && PLAYMATE_COLOR_UPPER.getColor().getRed() > r
&& PLAYMATE_COLOR_LOWER.getColor().getGreen() > g && PLAYMATE_COLOR_UPPER.getColor().getGreen() < g
&& PLAYMATE_COLOR_LOWER.getColor().getBlue() > b && PLAYMATE_COLOR_UPPER.getColor().getBlue() < b;
}
private boolean isOppositeColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
if (isShadingField) {
return SHADING_OPPOSITE_COLOR_LOWER.getColor().getRed() <= r && SHADING_OPPOSITE_COLOR_UPPER.getColor().getRed() >= r
&& SHADING_OPPOSITE_COLOR_LOWER.getColor().getGreen() <= g && SHADING_OPPOSITE_COLOR_UPPER.getColor().getGreen() >= g
&& SHADING_OPPOSITE_COLOR_LOWER.getColor().getBlue() <= b && SHADING_OPPOSITE_COLOR_UPPER.getColor().getBlue() >= b;
}
return OPPOSITE_COLOR_LOWER.getColor().getRed() > r && OPPOSITE_COLOR_UPPER.getColor().getRed() < r
&& OPPOSITE_COLOR_LOWER.getColor().getGreen() > g && OPPOSITE_COLOR_UPPER.getColor().getGreen() < g
&& OPPOSITE_COLOR_LOWER.getColor().getBlue() < b && OPPOSITE_COLOR_UPPER.getColor().getBlue() > b;
}
private boolean isActivePlayerColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
if (isShadingField) {
return SHADING_ACTIVE_PLAYER_LOWER.getColor().getRed() <= r && SHADING_ACTIVE_PLAYER_UPPER.getColor().getRed() >= r
&& SHADING_ACTIVE_PLAYER_LOWER.getColor().getGreen() <= g && SHADING_ACTIVE_PLAYER_UPPER.getColor().getGreen() >= g
&& SHADING_ACTIVE_PLAYER_LOWER.getColor().getBlue() <= b && SHADING_ACTIVE_PLAYER_UPPER.getColor().getBlue() >= b;
}
return ACTIVE_PLAYER_LOWER.getColor().getRed() > r && ACTIVE_PLAYER_UPPER.getColor().getRed() <= r
&& ACTIVE_PLAYER_LOWER.getColor().getGreen() < g && ACTIVE_PLAYER_UPPER.getColor().getGreen() > g
&& ACTIVE_PLAYER_LOWER.getColor().getBlue() < b && ACTIVE_PLAYER_UPPER.getColor().getBlue() > b
&& Math.abs(g - b) < 30 && Math.abs(r - g) > 100;
}
private boolean isBallColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
if (isShadingField) {
return SHADING_BALL_COLOR_LOWER.getColor().getRed() <= r && SHADING_BALL_COLOR_UPPER.getColor().getRed() >= r
&& SHADING_BALL_COLOR_LOWER.getColor().getGreen() <= g && SHADING_BALL_COLOR_UPPER.getColor().getGreen() >= g
&& SHADING_BALL_COLOR_LOWER.getColor().getBlue() <= b && SHADING_BALL_COLOR_UPPER.getColor().getBlue() >= b;
}
return BALL_COLOR_LOWER.getColor().getRed() < r && BALL_COLOR_UPPER.getColor().getRed() > r
&& BALL_COLOR_LOWER.getColor().getGreen() < g && BALL_COLOR_UPPER.getColor().getGreen() > g
&& BALL_COLOR_LOWER.getColor().getBlue() >= b && BALL_COLOR_UPPER.getColor().getBlue() <= b;
}
private boolean isOverlayOppositePlayerColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
return OVERLAY_OPPOSITE_PLAYER_COLOR_LOWER.getColor().getRed() <= r && OVERLAY_OPPOSITE_PLAYER_COLOR_UPPER.getColor().getRed() >= r
&& OVERLAY_OPPOSITE_PLAYER_COLOR_LOWER.getColor().getGreen() <= g && OVERLAY_OPPOSITE_PLAYER_COLOR_UPPER.getColor().getGreen() >= g
&& OVERLAY_OPPOSITE_PLAYER_COLOR_LOWER.getColor().getBlue() <= b && OVERLAY_OPPOSITE_PLAYER_COLOR_UPPER.getColor().getBlue() >= b;
}
private boolean isOverlayPlaymatePlayerColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
return OVERLAY_PLAYMATE_PLAYER_COLOR_LOWER.getColor().getRed() <= r && OVERLAY_PLAYMATE_PLAYER_COLOR_UPPER.getColor().getRed() >= r
&& OVERLAY_PLAYMATE_PLAYER_COLOR_LOWER.getColor().getGreen() <= g && OVERLAY_PLAYMATE_PLAYER_COLOR_UPPER.getColor().getGreen() >= g
&& OVERLAY_PLAYMATE_PLAYER_COLOR_LOWER.getColor().getBlue() >= b && OVERLAY_PLAYMATE_PLAYER_COLOR_UPPER.getColor().getBlue() <= b;
}
private boolean isOverlayBoundPlayerColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
return OVERLAY_BOUND_OF_PLAYER_COLOR.getColor().getRed() <= r && BOUND_OF_PLAYER_COLOR_LOWER.getColor().getRed() >= r
&& OVERLAY_BOUND_OF_PLAYER_COLOR.getColor().getGreen() <= g && BOUND_OF_PLAYER_COLOR_LOWER.getColor().getGreen() >= g
&& OVERLAY_BOUND_OF_PLAYER_COLOR.getColor().getBlue() <= b && BOUND_OF_PLAYER_COLOR_LOWER.getColor().getBlue() >= b
&& Math.abs(g - b) < 10 && Math.abs(r - g) < 10 && Math.abs(r - b) < 10;
}
private boolean isShadingFieldColor(int pixel) {
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
return SHADING_FIELD_COLOR_LOWER.getColor().getRed() <= r && SHADING_FIELD_COLOR_UPPER.getColor().getRed() >= r
&& SHADING_FIELD_COLOR_LOWER.getColor().getGreen() <= g && SHADING_FIELD_COLOR_UPPER.getColor().getGreen() >= g
&& SHADING_FIELD_COLOR_LOWER.getColor().getBlue() <= b && SHADING_FIELD_COLOR_UPPER.getColor().getBlue() >= b
&& r < g && r > b && g - r <= 45 && g - b <= 90 && r - b <= 50;
}
}