Here is my code for a color selector (alpha not included yet)
Touch and slide on a color to change it, and tap on the top or bottom of a color to increment it by one.
--# Main
-- RGBSlider
-- Use this function to perform your initial setup
function setup()
watch("slider.col")
slider = RGBSlider(WIDTH / 2, HEIGHT / 2)
end
-- This function gets called once every frame
function draw()
background(slider.col)
slider:draw()
end
function touched(touch)
slider:touched(touch)
end
--# RGBSlider
RGBSlider = class()
function RGBSlider:init(x, y, col)
self.tapped = false
self.x, self.y = x, y
self.col = col or color(127, 127, 127, 255)
self.m = mesh()
self.m.vertices = {
vec2(-50, -127), vec2(-25, -127), vec2(-25, 127),
vec2(-50, -127), vec2(-50, 127), vec2(-25, 127),
vec2(-12.5, -127), vec2(12.5, -127), vec2(12.5, 127),
vec2(-12.5, -127), vec2(-12.5, 127), vec2(12.5, 127),
vec2(25, -127), vec2(50, -127), vec2(50, 127),
vec2(25, -127), vec2(25, 127), vec2(50, 127),
}
self.triangle = mesh()
self.triangle.vertices = { vec2(0, 5), vec2(5, 0), vec2(0, -5), vec2(25, 5), vec2(20, 0), vec2(25, -5) }
self.triangle:setColors(0,0,0)
self:color()
self.selected = "n"
end
function RGBSlider:draw()
pushMatrix()
pushStyle()
rectMode(CENTER)
translate(self.x, self.y)
fill(0, 0, 0, 255)
rect(0, 0, 125, 270)
self.m:draw()
for i = 0, 2 do
x, y = -50 + 37.5 * i, -127 + self.col[i + 1]
pushMatrix()
translate(x, y)
self.triangle:draw()
popMatrix()
end
popStyle()
popMatrix()
end
function RGBSlider:color()
local r, g, b = self.col.r, self.col.g, self.col.b
local rt, rb = color(255, g, b), color(0, g, b)
local gt, gb = color(r, 255, b), color(r, 0, b)
local bt, bb = color(r, g, 255), color(r, g, 0)
self.m.colors = {
rb, rb, rt, rb, rt, rt,
gb, gb, gt, gb, gt, gt,
bb, bb, bt, bb, bt, bt
}
end
function RGBSlider:touched(touch)
local t = vec2(touch.x - self.x, touch.y - self.y)
if touch.state == BEGAN and math.abs(t.x) < 65 and math.abs(t.y) <= 127 then
self.tapped = true
if t.x < -20 then
self.selected = "r"
return
elseif t.x < 20 then
self.selected = "g"
return
else
self.selected = "b"
return
end
end
if touch.state == MOVING and self.selected ~= "n" then
self.tapped = false
self.col[self.selected] = math.min(math.max(self.col[self.selected] + touch.deltaY, 0), 255)
self:color()
end
if touch.state == ENDED then
if self.tapped then
self.col[self.selected] = self.col[self.selected] + t.y/math.abs(t.y)
self.tapped = false
end
self.selected = "n"
end
end