Code for Task #5: Rainbow Codenections!

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:

Without further ado, the code:

// 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);
}
2 Likes