How to create a C# code similar to the Gradient component?

You can override DrawViewportWires method to draw your lines with custom colors directly:

private void RunScript(Line inputLine, double angle, double minLength, ref object Lines, ref object Lengths, ref object Domain, ref object Remap)
  var lines = new List<Line>(){inputLine};
  DivideAndRotate(inputLine, lines, angle, minLength);
  Lines = lines;
  //Substituting the length component
  var lengths = lines.Select(line => line.Length).ToList();
  Lengths = lengths;
  //substituting the Bounds component
  var bounds = new Interval(lengths.Min(), lengths.Max());
  Domain = bounds;
  //substituting the remap component
  var remap = lengths.Select(len => RemapValues(len, bounds.Min, bounds.Max)).ToList();
  Remap = remap;
  _lines = lines;
  _parameters = remap;

// <Custom additional code> 
private static void DivideAndRotate(Line line, List<Line> lines, double angle, double minLength)
  if(line.Length < minLength) return;
  var endpt = line.PointAt(0.95);
  line.To = endpt;
  var xform = Transform.Rotation(angle, Vector3d.ZAxis, line.From);
  DivideAndRotate(line, lines, angle, minLength);
private static double RemapValues (double aValue, double leftMin, double leftMax)
  var leftSpan = leftMax - leftMin;
  return (aValue - leftMin) / leftSpan;
//Draw lines with custom colors
List<double> _parameters;
List<Line> _lines;
public override void DrawViewportWires(IGH_PreviewArgs args)
  var gradiant = Grasshopper.GUI.Gradient.GH_Gradient.Heat();
  if (_lines == null) return;
  for (var i = 0; i < _lines.Count; i++)
    args.Display.DrawLine(_lines[i], gradiant.ColourAt(_parameters[i]), 2);
// </Custom additional code> (15.7 KB)