I’m trying to control the background color values with on-screen sliders.
Starting with red amount first, I have it working partially. Adjusting the color wheel in parameters changes the on-screen slider(amount of red) value but not the other way around. I want to be able to change the red amount value with the on-screen slider.
displayMode(OVERLAY)
function setup()
background(0)
backingMode(RETAINED)
parameter.color("col", color(255,0,0,128), colorChanged)
end
function draw()
background(col)
slider:draw()
end
function colorChanged(c)
tab = {}
table.insert(tab,c)
print(tab[1].x) --amount of red
print(tab[1].y) --amount of blue
print(tab[1].z) --amount of green
print(tab[1].w) --amount of alpha
slider = Slider(tab[1].x, 1, 250, 1, WIDTH/2, HEIGHT/2+200)
parameter.integer(tab[1].x, 1, 250, 1)
end
function touched(touch)
slider:touched(touch)
end
Slider = class()
function Slider:init(variable, min, max, init, x, y, callback)
self.variable = variable
self.x, self.y = x, y
self.min, self.max = min, max
self.prevValue, self.curValue = init, init
self.callback = callback
self.length = 250
self.sliderX = (self.x - self.length/2) + (self.length / (self.max - 1)) * (self.curValue - 1)
end
function Slider:draw()
pushStyle()
strokeWidth(2) stroke(127)
line(self.x - self.length/2, self.y, self.x + self.length/2, self.y)
strokeWidth(5) stroke(255)
line(self.x - self.length/2, self.y, self.sliderX, self.y)
strokeWidth(2) stroke(255) if self.touching then fill(255) else fill(0) end
ellipse(self.sliderX, self.y, 20)
fill(255, 151, 0, 255)
local w,h = textSize(self.curValue)
text(self.curValue, self.x + self.length/2 + 5 - w/2, self.y + 10 + h/2)
local w,h = textSize(self.variable)
text(self.variable, self.x - self.length/2 - 5 + w/2, self.y + 10 + h/2)
popStyle()
if self.touching then
self.sliderX = math.max(math.min(CurrentTouch.x, self.x + self.length/2), self.x - self.length/2)
self.curValue = math.floor((((self.sliderX - (self.x - self.length/2)) / self.length)
* (self.max - 1) + 1) + 0.49)
if self.callback ~= nil and self.curValue ~= self.prevValue then
_G[self.variable] = self.curValue
self.callback()
self.prevValue = self.curValue
end
end
end
function Slider:touched(touch)
if touch.x > self.sliderX - 10 and touch.x < self.sliderX + 10
and touch.y > self.y - 10 and touch.y < self.y + 10 then
self.touching = true
end
if touch.state == ENDED then
self.touching = false
self.sliderX = (self.x - self.length/2) +
(self.length / (self.max - 1)) * (self.curValue - 1)
end
end