Change the color of the Custom component

Hi @DavidRutten

Basically, as stated in the Title - I would like to change the main color of my Custom components (non-editable)
It would be easier to navigate inside the Canvas.

See below:

image

The easiest way to do this is a bit hacky, but involves the following:

  1. Create a new attributes which inherits from GH_ComponentAttributes
  2. Initiate this class from within the CreateAttributes method of your GH_Component and assign it to the m_attributes field.
  3. Inside the Render() method of the attributes, special case the Objects channel. In here, change the palette style in the GH_Skin class, then let the attributes render themselves, then undo your changes.
  public class CustomAttributes : GH_ComponentAttributes
  {
    public CustomAttributes(IGH_Component component)
      : base(component)
    { }

    protected override void Render(GH_Canvas canvas, Graphics graphics, GH_CanvasChannel channel)
    {
      if (channel == GH_CanvasChannel.Objects)
      {
        // Cache the existing style.
        GH_PaletteStyle style = GH_Skin.palette_normal_standard;

        // Swap out palette for normal, unselected components.
        GH_Skin.palette_normal_standard = new GH_PaletteStyle(Color.DeepPink, Color.Teal, Color.PapayaWhip);

        base.Render(canvas, graphics, channel);

        // Put the original style back.
        GH_Skin.palette_normal_standard = style;
      }
      else
        base.Render(canvas, graphics, channel);
    }
  }
1 Like

Thanks, @DavidRutten
Works perfectly.

Hi @DavidRutten
It seems a bit strange but I cannot repeat same behavior for all components. Some of them change color and some not. I am overriding CreateAttributes in every component though.

What can be the problem?
Thanks.

Are some components using a different palette? Disabled and Hidden components use palettes that are stored in parallel with the palette_normal_standard. Also warnings and errors cause components to switch to different palettes.

No, in all components I am referring to the same Class which inherits GH_ComponentAttributes. So, it should be same color for all my components.
I understand that disabled and Hidden as well as Error containing components are displayed differently.
Hm…

Hi, I was wondering if this script works with c# script component, or if iit works for the grasshopper templates with in visual studio?. Thanks

You cannot affect the display of the script components, only custom components that you create and can assign your own attributes to.

1 Like

For reference, this works in your component class:


public override void CreateAttributes()
        {
            base.CreateAttributes();
            m_attributes = new GH_LinkajouAttributes(this);
            
        }

this doesnt


public override void CreateAttributes()
        {
            m_attributes = new GH_LinkajouAttributes(this);
            base.CreateAttributes();
            
            
        }

This is not right because you are creating the default attributes and then creating others. In the next one the same thing happens but in reverse, base.CreateAttributes() creates the default attributes.

You don’t need to, nor should you, create them twice. you just need to do this:

        public override void CreateAttributes()
        { 
            m_attributes = new GH_LinkajouAttributes(this); 
        }
1 Like

Gotcha, thanks!