beautypg.com

A simple gradient, Listing 11, Listing 12 – ETC Unison Mosaic Designer v1.11.0 User Manual

Page 233: Working with gradients

background image

Custom Preset Programming Guide

-- adjust y to be relative to the center of the effect

y = y-(height/2)+0.5

-- decide if this pixel is inside the band

if (math.abs(y)/height<=band_height) then

return band_colour

else

return background_colour

end

end

We have added two variables, band_colour (red) and background_colour (blue) and are now returning
those values rather than the r,g,b values that we were using previously. You should now see red bands rippling
over a blue background.

A simple gradient

The colour library also includes an interpolate function, which takes two colours and a fraction and returns a
new colour that is linearly interpolated between the two colours. For example:

Listing 11

local red = colour.new(255,0,0)

local blue = colour.new(0,0,255)

function pixel(frame,x,y)

-- interpolate between red and blue using the horizontal dis-

placement of x

-- note that we use (width-1) so the rightmost pixel is completely

blue

return colour.interpolate(red,blue,x/(width-1))

end

This creates a horizontal red to blue gradient. We could have created the same gradient without the colour library
as follows:

Listing 12

function pixel(frame,x,y)

local f = x/(width-1)

return 255*(1-f),0,(255*f)

end

However, if you changed your mind about the colours that you wanted for your gradient, it would be significantly
harder to alter Listing 12 than it would be to change the colours in the first two lines of Listing 11.

Working with gradients

The gradient library adds support for more complicated gradients that cannot be achieved by interpolating
between two colours.

To create a new variable of type gradient, call gradient.new(), passing in two colours, i.e:

- 233 -