PdfBaseFunction.colorsAndStops constructor

PdfBaseFunction.colorsAndStops(
  1. PdfDocument pdfDocument,
  2. List<PdfColor?> colors, [
  3. List<double>? stops
])

Implementation

factory PdfBaseFunction.colorsAndStops(
  PdfDocument pdfDocument,
  List<PdfColor?> colors, [
  List<double>? stops,
]) {
  if (stops == null || stops.isEmpty) {
    return PdfFunction.fromColors(pdfDocument, colors);
  }

  final _colors = List<PdfColor>.from(colors);
  final _stops = List<double>.from(stops);

  final fn = <PdfFunction>[];
  var lc = _colors.first;

  if (_stops[0] > 0) {
    _colors.insert(0, lc);
    _stops.insert(0, 0);
  }

  if (_stops.last < 1) {
    _colors.add(_colors.last);
    _stops.add(1);
  }

  if (_stops.length != _colors.length) {
    throw Exception(
        'The number of colors in a gradient must match the number of stops');
  }

  for (final c in _colors.sublist(1)) {
    fn.add(PdfFunction.fromColors(pdfDocument, <PdfColor>[lc, c]));
    lc = c;
  }

  return PdfStitchingFunction(
    pdfDocument,
    functions: fn,
    bounds: _stops.sublist(1, _stops.length - 1),
    domainStart: 0,
    domainEnd: 1,
  );
}