Changing the color of the first or second pixel

This is a clone from KITT. How can I make the first one or two pixels be a different color compared to the rest of the pattern?

leader = 0
direction = 1
pixels = array(pixelCount)

speed = pixelCount / 2500
fade = .0007
export function beforeRender(delta) {
  leader += direction * delta * speed
  if (leader >= pixelCount) {
    direction = -direction
    leader = pixelCount -1
  }
  
  if (leader < 0) {
    direction = -direction
    leader = 0
  }
  pixels[floor(leader)] = 1
  for (i = 0; i < pixelCount; i++) {
    pixels[i] -= delta * fade
    pixels[i] = max(0, pixels[i])
  }
}

export function render(index) {
  v = pixels[index]
  v = v*v*v
  hsv(.0, 1, v)
}

Hi @Wildersrobles,
See this post for discussion and code:

The idea is that you’d check the index in the render function and do something different. e.g.

export function render(index) {
  if (index < 2) {
    hsv(.75,1,1);
  } else {
    v = pixels[index]
    v = v*v*v
    hsv(.0, 1, v)
  }
}
1 Like

That worked, but I guess I didnt ask correctly.

I want the first pixel of the animation to be a different color.

In other words the LEDs move left on the very first LED that is moving, I want it to be white for example.

Keep in mind I have zero knowledge of programming and I am just copying pasting all over the place. :slight_smile:

Hi @Wildersrobles - try this:

export function render(index) {
  v = pixels[index]
  v = v*v*v

  if (abs(index - leader) < 3) h = 0.66 // blue up front
  else if (abs(index - leader) < 10) h = 0.1 // yellow next
  else h = 0 // red for the rest
  
  hsv(h, 1, v)
}

This works because abs(index - leader) is how far the current pixel is from the pixel that’s the leader. Notice leader is a decimal and index is whole number. Since the leader can move in either direction, technically the pixels right in front of the leader (the ones who are about to become bright) are also given these special colors, but they probably have faded their v intensity to 0 by that time, so you won’t see those colors unless you also set the fade speed so slow that it starts to “eat it’s own tail”.

If you play with this, just be careful of the ordering of your ifs and else ifs to think through making sure every value of index will set h to something.

To make it white, use my example but set s to 0 instead of changing h to other hues.

Hope this helps!

2 Likes

Super helped! thanks

A post was split to a new topic: How can the whole strip have a certain colour, without affecting the animated pixels?