toList method

List toList({
  1. List? template,
  2. bool asDouble = false,
  3. int? range,
  4. int rows = 1,
})

Output color as List object By default, the list will be output in rgb or rgba format, depending on the format of this object. You can also specify an arbitrary number and order of the components are specify in the template. Default values rgb component output as int. You can change this bypassing a true argument asDouble. All rgb components of the default set in the range of 0-255, but you can change this by specifying a range argument range from 0-range (eg from 0 to 1). It is also possible to deduce the components several times, specify the argument rows. This may be necessary when you need to set the same color for each vertex in the polygon 3D model.

Implementation

List toList({List? template, bool asDouble: false, int? range, int rows: 1}) {
  var values = [red, green, blue, alpha],
      defEnd = 255,
      end = range is int ? range : defEnd,
      comps = template == null && alpha == null
          ? [Component.RED, Component.GREEN, Component.BLUE]
          : [Component.RED, Component.GREEN, Component.BLUE, Component.ALPHA],
      target = template == null ? List.from(comps) : List.from(template);

  for (var index = 0; index < comps.length;) {
    var pos = target.indexOf(comps[index]),
        isAlpha = comps[index] == Component.ALPHA,
        value = values[index];
    if (pos is int && pos >= 0) {
      if (isAlpha) {
        value = value == null ? 1.0 : value;
      }
      if (value is int && asDouble) {
        value = value.toDouble();
      }
      if (value is int && end != defEnd && !isAlpha) {
        value = range! / defEnd * value;
      }
      target[pos] = value;
    } else {
      index++;
    }
  }

  if (rows > 1) {
    var _target = List.from(target);
    while (rows-- > 1) {
      target.addAll(_target);
    }
  }

  return target;
}