It looks like you're new here. If you want to get involved, click one of these buttons!

I'm having trouble figuring out the math involved in changing a line's stroke color based on its variable length. The goal is that when the line gets shorter it will be gradually drawn red and when it gets longer it gradually draws blue. And there is a middle ground where in between shorter and longer it is just white. For example:

```
-- line test
supportedOrientations(LANDSCAPE_ANY)
function setup()
parameter.integer( "length", 100, 600, 350 )
parameter.watch( "i" )
RED_MIN = 200
RED_MAX = 100
BLUE_MIN = 500
BLUE_MAX = 600
end
function draw()
if length <= RED_MIN then
-- i = 255 - (255 * a value between 0 and 1, where 0 is f(length=RED_MIN) and 1 is f(length=RED_MAX)
c = color(255, i, i, 255)
elseif length >= BLUE_MIN then
-- i = 255 - (255 * a value between 0 and 1, where 0 is f(length=BLUE_MIN) and 1 is f(length=BLUE_MAX)
c = color(i, i, 255, 255)
else
c = color(255,255,255,255)
end
background(40, 40, 50)
strokeWidth(5)
stroke(c)
line(70, HEIGHT/2, 70+length, HEIGHT/2)
end
```

Thanks again guys!

## Comments

call as

`interp(length,RED_MIN,RED_MAX)`

and you get out a number between 0 and 1 so that if`length`

is in between then it is the correct proportion, but it is "clamped" to the end points in that it never goes below 0 or above 1.Check this:

@Andrew_Stacey and @Jmv38

Excellent solutions guys! Exactly what I was hoping for from this forum! Thanks so much!