lightActor function

void lightActor(
  1. Painter p,
  2. ActorDefinition? a,
  3. int c,
  4. double k, [
  5. double rim = 0.0,
  6. double t = 0.0,
])

Light a character: warm additive light over its body and bright rim inside its dark outline.

Implementation

void lightActor(
  Painter p,
  ActorDefinition? a,
  int c,
  double k, [
  double rim = 0.0,
  double t = 0.0,
]) {
  if (a == null || (k <= 0.01 && rim <= 0.01)) return;
  final m = a.mask;
  final pix = a.pixels;
  bool has(int i, int j) =>
      i >= 0 && j >= 0 && i < m.w && j < m.h && m.data[j * m.w + i] != 0;
  for (var j = 0; j < m.h; j++) {
    for (var i = 0; i < m.w; i++) {
      if (m.data[j * m.w + i] == 0) continue;
      if (k > 0.01) p.light(a.x + i, a.y + j, c, k * (1.1 - (0.5 * j) / m.h));
      if (rim <= 0.01 || !has(i - 1, j) || !has(i + 1, j) || !has(i, j - 1)) {
        continue;
      }
      if (has(i - 2, j) && has(i + 2, j) && has(i, j - 2)) continue;
      final f = rim * (0.75 + 0.35 * noise(j * 0.6 - t * 8.0, i * 3));
      if (f > 0.25) {
        final basePx = pix[j * m.w + i];
        p.px(a.x + i, a.y + j, mix(basePx != 0 ? basePx : c, c, math.min(0.85, f)));
      }
    }
  }
}