actorEyes function
Implementation
List<ActorEye> actorEyes(ActorDefinition a) {
var list = _eyesCache[a.mask];
if (list != null) return list;
final explicit = a.eyes;
if (explicit != null) {
list = [
for (final (x, y) in explicit)
ActorEye(x, x, y, y + 1, [(x, y), (x, y + 1)], colorAt(a, x, y)),
];
_eyesCache[a.mask] = list;
return list;
}
final s = actorShape(a);
final w = s.w;
int col(int x, int y) => s.on(x, y) ? colorAt(a, x, y) : 0;
bool dark(int x, int y) =>
s.on(x, y) &&
y < s.h * 0.4 &&
luma(col(x, y)) < 0.15 &&
s.on(x - 1, y) &&
s.on(x + 1, y) &&
s.on(x, y - 1) &&
s.on(x, y + 1);
final seen = <int>{};
list = [];
for (final (x0, y0) in s.body) {
if (seen.contains(y0 * w + x0) || !dark(x0, y0)) continue;
final px = <(int, int)>[];
final q = <(int, int)>[(x0, y0)];
seen.add(y0 * w + x0);
while (q.isNotEmpty) {
final (x, y) = q.removeLast();
px.add((x, y));
for (final (dx, dy) in const [(1, 0), (-1, 0), (0, 1), (0, -1)]) {
final nx = x + dx;
final ny = y + dy;
final k = ny * w + nx;
if (seen.contains(k) ||
!(dark(nx, ny) || (s.on(nx, ny) && luma(col(nx, ny)) > 0.95))) {
continue;
}
seen.add(k);
q.add((nx, ny));
}
}
if (px.length < 2 || px.length > 14) continue;
var minX = 1 << 30;
var maxX = -1 << 30;
var minY = 1 << 30;
var maxY = -1 << 30;
for (final (x, y) in px) {
if (x < minX) minX = x;
if (x > maxX) maxX = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
}
final counts = <int, int>{};
for (final (x, y) in px) {
for (final (dx, dy)
in const [(1, 0), (-1, 0), (0, 1), (0, -1), (2, 0), (-2, 0)]) {
final c = col(x + dx, y + dy);
final l = luma(c);
if (c != 0 &&
l > 0.5 &&
l < 0.95 &&
!px.any((p) => p.$1 == x + dx && p.$2 == y + dy)) {
counts[c] = (counts[c] ?? 0) + 1;
}
}
}
if (counts.isEmpty) continue;
final sorted = counts.entries.toList()
..sort((p, q) => q.value.compareTo(p.value));
final skin = sorted.first.key;
if (maxX - minX <= 4 && maxY - minY <= 5) {
list.add(ActorEye(minX, maxX, minY, maxY, px, skin));
}
}
list.sort((p, q) => p.x0.compareTo(q.x0));
if (list.length > 2) list = [];
_eyesCache[a.mask] = list;
return list;
}