It’s a rectangle filled with a rainbow. What’s not to like?
-- Rainbow Rect
-- Use this function to perform your initial setup
function setup()
a = rainbow_rect(0,0, WIDTH,HEIGHT)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
a:draw()
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
end
-- x,y specify bottom left corner
function rainbow_rect(x,y,w,h)
local rectangle = mesh()
local h2,h3,h4,h5 = h*.75,h*.5,h*.25,0
local verts = {vec2(w,h), vec2(0,h), vec2(0,h2), -- h is red
vec2(w,h), vec2(0,h2), vec2(w,h2), -- h2 is yellow
vec2(w,h2), vec2(0,h2), vec2(0,h3),
vec2(w,h2), vec2(0,h3), vec2(w,h3), -- h3 is green
vec2(w,h3), vec2(0,h3), vec2(0,h4),
vec2(w,h3), vec2(0,h4), vec2(w,h4), -- h4 is blue
vec2(w,h4), vec2(0,h4), vec2(0,h5),
vec2(w,h4), vec2(0,h5), vec2(w,h5)} --h5 is purple
local colors = {}
local c
for i,v in ipairs(verts) do
if v.y == h then c = color(255,0,0)
elseif v.y == h2 then c = color(255,255,0)
elseif v.y == h3 then c = color(0,255,0)
elseif v.y == h4 then c = color(0,0,255)
elseif v.y == h5 then c = color(141, 0, 255, 255)
end
colors[i] = c
end
for i,v in ipairs(verts) do
v.x = v.x + x
v.y = v.y + y
end
rectangle.vertices = verts
rectangle.colors = colors
return rectangle
end
Instead of returning rectangle you could return an image instead
local image = image(w,h)
setContext(image)
rectangle:draw()
return image
Originally I was trying to make a rainbow gradient shader for a sphere/ellipse but i couldn’t figure it out.