Color by distance

l1=(Line.ByStartPointEndPoint((elm.BoundingBox).MaxPoint,
(elm.BoundingBox).MinPoint)).PointAtParameter(0.5);
crnBb=BoundingBox.ByGeometry(crn.Geometry());
crnCp=(Line.ByStartPointEndPoint(crnBb.MaxPoint,
crnBb.MinPoint)).PointAtParameter(0.5);
l2=Line.ByStartPointEndPoint(Point.ByCoordinates
(crnCp.X,crnCp.Y,crnBb.MinPoint.Z),Point.ByCoordinates
(crnCp.X,crnCp.Y,crnBb.MaxPoint.Z));
//max and min distances of each distance group
d1=Flatten(l1<1>.DistanceTo(l2));
d2=List.MinimumItem(d1)..List.MaximumItem(d1)..~inc;
i1=List.DropItems(d2,-1);
i2=List.DropItems(d2,1);
//Filteration criteria
b1=List.Flatten(d1>i1<1><2>,1);
b2=List.Flatten(d1<=(i2+100)<1><2>,1);
b1&&b2;
//Sorting elements into groups
g1=List.FilterByBoolMask(elm,(b1&&b2)<1>);
g2=List.GetItemAtIndex(g1<1>,0);
//Colors
c=List.TakeItems(Transpose(col)[1],List.Count(d2)-1);
//Override color in Revit view
g=g2<1><2>.OverrideColorInView(c<1>);
//Legend
lg1=Math.Round(i1,1);
lg2=Math.Round(i2,1);
lg3=List.TakeItems(Transpose(col)[0],List.Count(d2)-1);
lg=[lg1,lg2,lg3];
lg1+" to "+lg2+" ["+lg3+"]";
[["Red",Color.ByARGB(255,255,0,0)],
["Green",Color.ByARGB(255,0,255,0)],
["Blue",Color.ByARGB(255,0,0,255)],
["Yellow",Color.ByARGB(255,255,255,0)],
["Cyan",Color.ByARGB(255,0,255,255)],
["Magenta",Color.ByARGB(255,255,0,255)],
["Violet",Color.ByARGB(255,238,130,238)],
["Orange",Color.ByARGB(255,255,165,0)],
["Brown",Color.ByARGB(255,165,42,42)],
["Gray",Color.ByARGB(255,128,128,128)]];