Grasshopper

algorithmic modeling for Rhino

I am trying to convert a String type to a Color type in Grasshopper using the C# component.

The input is a list of Strings in this format: 

e.g. 255, 255, 255

I would like to convert these values into a color object, and thus, I used a function in C# called System.Drawing.Color.FromArgb(int red, int green, int blue);

Attached is my script:

My main function is simply: 

I am taking the list of strings, converting it to an array. For each index of the array, I am splitting the strings into three integers, and taking those three integers and converting them into a Color object using the FromArgb() method, which takes in three integers red, green, and blue, respectively.

My C# Scripting Component's input is set to List Access and the Type Hint is string. 

The error I have been getting is this:

The script works when used in Visual Studio, however, does not work when I try to use it in Grasshopper. Could someone help me debug what the issue could be, and perhaps:

1. Suggest a better way than to use static methods to do the conversion

2. Rewrite this so I'm dealing with Lists instead of an Array.

Thank you for the help.

Views: 1837

Replies to This Discussion

you can also just use a param color component for this without writing a c# component, are you working on something specific?

yes i mean this component

Hi!

This should work:

public static Color[] convertToColor(String[] stringArray)
{
  var colorArray = new Color[stringArray.Length];

  for (var i = 0; i < stringArray.Length; i++)
  {
    var splitString = stringArray[i].Split(',');
    var splitInts = splitString.Select(item => int.Parse(item)).ToArray();

    colorArray[i] = Color.FromArgb(splitInts[0], splitInts[1], splitInts[2]);
  }

  return colorArray;
}

Greetings!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service