Get values from a gradient component

Hi all, I’m looking for a way to retrieve a list of colors and placements of these from the Gradient component.

So like a “De Gradient” component.

I’ve been playing around with the Grasshopper.GUI.Gradient.GH_Gradient class which works quite nicely. But how can I derive this class from a Gradient Component on my canvas?

Best, Mathias

private void RunScript(object gradient, ref object isLinear, ref object isLocked, ref object gripCount , ref object gripsParameters, ref object gripsColourLeft, ref object gripsColourRight)
  {
    try
    {
      var gc = (GH_GradientControl) Component.Params.Input[0].Sources[0].Attributes.GetTopLevel.DocObject;
      var g = gc.Gradient;
      isLinear = g.Linear;
      isLocked = g.Locked;
      gripCount = g.GripCount;
      var parameters = new List<double>();
      var colourLeft = new List<Color>();
      var colourRight = new List<Color>();
      for(var i = 0; i < g.GripCount; i++)
      {
        parameters.Add(g[i].Parameter);
        colourLeft.Add(g[i].ColourLeft);
        colourRight.Add(g[i].ColourRight);
      }
      gripsParameters = parameters;
      gripsColourLeft = colourLeft;
      gripsColourRight = colourRight;

    }
    catch
    {
    }

Gradient.gh (7.2 KB)

3 Likes

Wow this is exactly what I was looking for. I think this can be used for a variety of things.

Thank you,
Mathias

Would there be a way that I can internalize a number 1 into the gradient as soon as it connects to the component?

I was thinking something like:

this.Params.Input[0].Sources[0].Sources[0] = 1;

but im unsure how to internalize it.

private void RunScript(object gradient, ref object isLinear, ref object isLocked, ref object gripCount , ref object gripsParameters, ref object gripsColourLeft, ref object gripsColourRight, ref object A)
{
  try
  {
    var gc = (GH_GradientControl) Component.Params.Input[0].Sources[0].Attributes.GetTopLevel.DocObject;
    var param = (GH_PersistentParam<GH_Number>) gc.Params.Input[2];
    if (param.VolatileData.IsEmpty)
    {
      param.PersistentData.Append(new GH_Number(1.0));
      param.ExpireSolution(true);
    }
    var g = gc.Gradient;
    isLinear = g.Linear;
    isLocked = g.Locked;
    gripCount = g.GripCount;
    var parameters = new List<double>();
    var colourLeft = new List<Color>();
    var colourRight = new List<Color>();
    for(var i = 0; i < g.GripCount; i++)
    {
      parameters.Add(g[i].Parameter);
      colourLeft.Add(g[i].ColourLeft);
      colourRight.Add(g[i].ColourRight);
    }
    gripsParameters = parameters;
    gripsColourLeft = colourLeft;
    gripsColourRight = colourRight;
   }
  catch
  {
  }
}

PersistentParam.gh (6.7 KB)

2 Likes

Wow this is awesome. Should be default in everyones toolbox.