# How to random color a Component?

**URL:** <https://forums.sketchup.com/t/how-to-random-color-a-component/38881>\
**Category:** Ruby API\
**Created:** [February 10, 2017, 12:06pm UTC](https://forums.sketchup.com/t/how-to-random-color-a-component/38881 "2017-02-10T12:06:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![I\_Alain\_I](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@I\_Alain\_I](https://forums.sketchup.com/u/I_Alain_I)\
**Post date:** [February 10, 2017, 12:06pm UTC](https://forums.sketchup.com/t/how-to-random-color-a-component/38881/1 "2017-02-10T12:06:23Z")

</div>

Hi

I wanna apply random colors to all Components in the Scene (all faces of the Component should have the same random color).  
I searched in the forum and in the API but I didn’t found anything I understand.

Isn’t there something like "… entity.material = ‘color’ " ?

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [February 10, 2017, 12:22pm UTC](https://forums.sketchup.com/t/how-to-random-color-a-component/38881/2 "2017-02-10T12:22:55Z")

</div>

If ‘entity’ is a reference to your ComponentInstance then `entity.material='color'` will work, but only if the material of that name ‘color’ existing in the model or ‘color’ is one of the named colors.  
Try it out manually…  
Select the ComponentInstance and use this snippet

```
entity=Sketchup.active_model.selection[0]
entity.material='Red'

```

A new material named ‘Red’ will be added to the model if it doesn’t already exist…  
You can of course get the reference to the ComponentInstance in several ways…  
The list of ‘built-in’ colors is here:

> **[Class: Sketchup::Color](https://ruby.sketchup.com/Sketchup/Color.html#names-class_method)**
>
> The Color class is used to create and manipulate colors within SketchUp Models.

This code will generate an array of all available colors by name…

```
array = Sketchup::Color.names

```

There are 140 listed color-names, so using something like

`entity.material=array[rand(140)]`

would use a random one of those color-names…

---

<div class="post-metadata">

**Author:** ![I\_Alain\_I](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@I\_Alain\_I](https://forums.sketchup.com/u/I_Alain_I)\
**Post date:** [February 10, 2017, 1:09pm UTC](https://forums.sketchup.com/t/how-to-random-color-a-component/38881/3 "2017-02-10T13:09:33Z")

</div>

Thanks alot TIG, this is something my little brain understands 🙂
