# Generating random colours (unicornhat)

**URL:** <https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857>\
**Category:** Support\
**Created:** [August 13, 2016, 8:01pm UTC](https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857 "2016-08-13T20:01:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bootnut](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@Bootnut](https://forums.pimoroni.com/u/Bootnut)\
**Post date:** [August 13, 2016, 8:01pm UTC](https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857/1 "2016-08-13T20:01:20Z")

</div>

I am trying to generate random colours on my unicornhat using the following code

```
from random import randint
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
unicornhat.set_pixel(6, 0, r, g, b)

```

the trouble with this is every now and then I get a bad colour that doesn’t display on the unicornhat, I assume it is generating “0, 0, 0”

is there a better way, maybe only generate safe / approved colours

---

<div class="post-metadata">

**Author:** ![RogueM](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/roguem/32/311_2.png) [@RogueM](https://forums.pimoroni.com/u/RogueM)\
**Post date:** [August 13, 2016, 9:27pm UTC](https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857/2 "2016-08-13T21:27:29Z")

</div>

pixels below a certain brightness won’t display. I would add a conditional that sums up the rgb tuple randomly generated and discard those below a certain threshold.

---

<div class="post-metadata">

**Author:** ![RogueM](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/roguem/32/311_2.png) [@RogueM](https://forums.pimoroni.com/u/RogueM)\
**Post date:** [August 13, 2016, 9:42pm UTC](https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857/3 "2016-08-13T21:42:30Z")

</div>

actually, I’m talking from my back side - you need to check for each colour separately and regenerate any random value below the minimum threshold required for the pixel to light up.

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [August 13, 2016, 10:02pm UTC](https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857/4 "2016-08-13T22:02:23Z")

</div>

Instead of generating random R, G, B values, perhaps you could use a different colour space- HSV for example- and generate random hues? Keeping the saturation and value constant.

You can use the `hsv_to_rgb` method in `colorsys` to convert 3 floating point values- between 0.0 and 1.0- representing Hue, Saturation and Value into RGB:

```auto
import colorsys
hue = randint(0, 359) # (In degrees)
sat = 1.0
val = 1.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue/359.0, sat, val)]

```

The `int(c * 255)` converts the 0.0-1.0 float values returned by hsv\_to\_rgb into the 0-255 range understood by Unicorn HAT.

If you want variable saturation and brightness you could also randomise those between known good values.

HSV has other benefits, too, by adjusting your random range you can pick portions of the hue wheel that contain complementary colours to randomise. For example you might just want shades of pink and blue, ie: `randint(190,320)`

Googling ‘hue wheel’ will give you some guidance about what colours appear where, for example;

 ![](https://us1.discourse-cdn.com/flex016/uploads/pimoroni/original/2X/8/8179910a23bbdbf7da0085076e74b0cb8bccdeb5.png)

---

<div class="post-metadata">

**Author:** ![Bootnut](https://avatars.discourse-cdn.com/v4/letter/b/e47c2d/32.png) [@Bootnut](https://forums.pimoroni.com/u/Bootnut)\
**Post date:** [August 14, 2016, 9:09am UTC](https://forums.pimoroni.com/t/generating-random-colours-unicornhat/2857/5 "2016-08-14T09:09:52Z")

</div>

Perfect! That is exactly what I wanted, worked right away!
