# Code for Task #5: Rainbow Codenections!

**URL:** <https://forum.electromage.com/t/code-for-task-5-rainbow-codenections/1042>\
**Category:** Pixelblaze Academy of Wizardry and Enchantment\
**Created:** [February 26, 2021, 9:00pm UTC](https://forum.electromage.com/t/code-for-task-5-rainbow-codenections/1042 "2021-02-26T21:00:15Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![zranger1](https://forum.electromage.com/user_avatar/forum.electromage.com/zranger1/32/301_2.png) [@zranger1](https://forum.electromage.com/u/zranger1)\
**Post date:** [March 6, 2021, 9:02pm UTC](https://forum.electromage.com/t/code-for-task-5-rainbow-codenections/1042/2 "2021-03-06T21:02:04Z")

</div>

To celebrate the coming matrix tasks, here’s a rainbow pattern that uses many sliders and works in 2D as well as on a strip. It’s based on code that @scruffynerf and I put together for this thread:

> [@2d rainbow patterns?](https://forum.electromage.com/t/2d-rainbow-patterns/922):
>
> Hey Guys, First I want to give major props to all that can figure out the coding. I’ve spent hours trying to grasp it and the only thing I accomplish is a migraine. I tried to edit some of the existing patterns and just not coming close. I’ve used editors like LEDEDIT software in the past and they all have these rainbow presets that start in the middle of the matrix expanding outwards in a really pretty rainbow hue. I’m looking for the same patterns, no black or white. just a pretty run thro…

Without further ado, the code:

```auto
// Rainbow and radial rainbow for 1D and 2D displays
export var speed = 0.25
export var direction = 1
export var scale = 2.9

// UI sliders

// higher is faster
export function sliderSpeed(v) {
  speed = 1-v;  
}

// left = inward, right = outward
export function sliderDirection(v) {
  direction = (v < 0.5) ? 1 : -1;  
}

export function sliderBandwidth(v) {
  scale = sqrt(0.5) * 5 * (1-v);
}

// pythagorean distance from center of display for 2D. Pixelblaze
// provides normalized x,y coords, so center is always going
// to be (0.5,0.5) regardless of real world display dimensions
function getRadius(x, y) {
  x -= 0.5; y -= 0.5;
  return sqrt(x*x + y*y);
}

// generate a timer - a sawtooth wave that we can
// use to animate color -- the direction flag makes
// it positive or negative, depending on the UI
// slider setting
export function beforeRender(delta) {
  t1 = direction * time(0.08 * speed);
}

// use wave starting from center and our timer to color every pixel
export function render(index) {
  hsv(t1+(wave(0.5*index/pixelCount)*scale), 1, 1);
}

// use radius and our timer to color every pixel
export function render2D(index, x, y) {
  hsv(t1+(getRadius(x, y)*scale), 1, 1);
}

```

---

_[View the full topic](https://forum.electromage.com/t/code-for-task-5-rainbow-codenections/1042)._
