fillTopDownGradient method

  1. @override
void fillTopDownGradient(
  1. num x,
  2. num y,
  3. num width,
  4. num height,
  5. PColor colorFrom,
  6. PColor colorTo,
)
override

Fill a rectangle (x,y , width x height) with a top down linear gradient. See fillBottomUpGradient.

Implementation

@override
void fillTopDownGradient(
    num x, num y, num width, num height, PColor colorFrom, PColor colorTo) {
  x = transform.x(x);
  y = transform.y(y);

  var clip = _clip;
  if (clip != null) {
    var box = PRectangle(x, y, width, height);
    var r = clip.intersection(box);
    if (r.isZeroDimension) return;

    x = r.x;
    y = r.y;
    width = r.width;
    height = r.height;
  }

  var cFrom = colorFrom.toPColorRGBA();
  var cTo = colorTo.toPColorRGBA();

  var r1 = cFrom.r;
  var g1 = cFrom.g;
  var b1 = cFrom.b;
  var a1 = cFrom.alpha;

  var r2 = cTo.r;
  var g2 = cTo.g;
  var b2 = cTo.b;
  var a2 = cTo.alpha;

  var rd = r2 - r1;
  var gd = g2 - g1;
  var bd = b2 - b1;
  var ad = a2 - a1;

  var w = width;
  var h = height;

  var steps = 256;

  var s = h ~/ steps;
  while (s == 0 && steps > 1) {
    --steps;
    s = h ~/ steps;
  }

  var end = (h ~/ s) - 1;

  if (a1 == 1 && a1 == a2) {
    for (var y = 0; y < h; y += s) {
      var ratio = y / end;

      var r = (r1 + rd * ratio).toInt();
      var g = (g1 + gd * ratio).toInt();
      var b = (b1 + bd * ratio).toInt();

      var c = PColorRGB(r, g, b);
      fillRect(0, y, w, s, PStyle(color: c));
    }
  } else {
    for (var y = 0; y < h; y += s) {
      var ratio = y / end;

      var r = (r1 + rd * ratio).toInt();
      var g = (g1 + gd * ratio).toInt();
      var b = (b1 + bd * ratio).toInt();
      var a = (a1 + ad * ratio);

      var c = PColorRGBA(r, g, b, a);
      fillRect(x, y, w, s, PStyle(color: c));
    }
  }
}