Color Chooser

In the set command, you have to do an integer divide by 1 ( //1 ) to get an integer passed to set. I didn’t download the code so I don’t know the whole command giving you the error.

I downloaded the code and it turns out there are a lot of (number has no integer representation) errors.

@dave1707 @AxiomCrux
This is because either @John or @Simeon changed the functions image:set() and image:get(), they used to allow floats, but now they can only take integers, which means that they made this code before it was changed.

Heres a version that corrects the integer errors.

-- Color Chooser

supportedOrientations(LANDSCAPE_ANY)

function setup()
    cc = ColorChooser()
end

function draw()
    background(40, 40, 50)    
    cc:draw()    
    fill(cc.pickedColor)    
    rect(0, 0, 100, 100)
end

function touched(touch)
    cc:touched(touch)
end

ColorChooser = class()

function ColorChooser:init()
    self.DIST = 90    
    local size = 361
    local offset = 182    
    self.circle = image(size, size)    
    for hue = 1, 360, .5 do
        for len = 0, 0.06, 0.01 do
            for dist = 0, self.DIST do
                self.circle:set(((math.sin((hue/180 * math.pi)+ len)*(180-dist))+offset)//1,
                    ((math.cos((hue/180 * math.pi)+ len)*(180-dist))+offset)//1,
                    color(self:HSVtoRGB(hue / 360, 1, 1)))                
            end
        end
    end    
    self.triangle = mesh()
    local len = 179 - self.DIST    
    self.triangle.vertices = {vec2(math.sin((2 * math.pi) / 3) * len, 
        math.cos((2 * math.pi) / 3) * len),vec2(math.sin(2 * ((2 * math.pi) / 3)) * len, 
        math.cos(2 * ((2 * math.pi) / 3)) * len),vec2(math.sin(2 * math.pi) * len, math.cos(2 * math.pi) * len)}    
    self.triangle.colors = {color(0, 0, 0),color(255, 255, 255),color(255, 0, 0),}    
    self.alpha = mesh()
    self.w, self.h = 50, 360
    self.x, self.y = 7 * (WIDTH / 8), HEIGHT / 2    
    self.alpha.vertices = {vec2(-(self.w / 2), -(self.h / 2)), vec2(-(self.w / 2), self.h / 2), 
        vec2(self.w / 2, -(self.h / 2)),vec2(self.w / 2, self.h / 2), vec2(self.w / 2, -(self.h / 2)), 
        vec2(-(self.w / 2), self.h / 2),}    
    self.alpha.colors = {color(0, 0, 0, 0), color(255, 0, 0, 255), color(0, 0, 0, 0),
        color(255, 0, 0, 255), color(0, 0, 0, 0), color(255, 0, 0, 255),}    
    self.ANGLE = 0
    self.ON_RING = false
    self.RING_ID = 0
    self.ON_TRIANGLE = false
    self.TRIANGLE_ID = 0
    self.ON_ALPHA = false
    self.ALPHA_ID = 0
    self.screen = image(WIDTH, HEIGHT)
    self.alphaScreen = image(self.w, self.h)
    self.pickedColor = color(255, 0, 0, 255)
end

function ColorChooser:draw()
    pushStyle()
    pushMatrix()    
    noStroke()    
    self.triangle.colors = {color(0, 0, 0),color(255, 255, 255),
        color(self.circle:get(((math.sin(-self.ANGLE) * 170) + 181)//1, 
        ((math.cos(-self.ANGLE) * 170) + 181)//1)),}    
    local r, g, b = self.pickedColor.r, self.pickedColor.g, self.pickedColor.b
    local col = color(r, g, b, 255)    
    self.alpha.colors = {color(0, 0, 0, 0), col, color(0, 0, 0, 0),col, color(0, 0, 0, 0), col,}    
    translate(WIDTH / 2, HEIGHT / 2)
    rotate(math.deg(self.ANGLE))
    self.triangle:draw()
    resetMatrix()    
    setContext(self.screen)    
    background(0, 0, 0, 0)
    translate(WIDTH / 2, HEIGHT / 2)
    rotate(math.deg(self.ANGLE))
    self.triangle:draw()
    setContext()    
    resetMatrix()    
    sprite(self.circle, WIDTH / 2, HEIGHT / 2)    
    translate(self.x, self.y)    
    self.alpha:draw()
    resetMatrix()    
    setContext(self.alphaScreen)
    background(255, 255, 255, 0)    
    translate(self.w / 2, self.h / 2)    
    self.alpha:draw()    
    resetMatrix()    
    setContext()    
    popStyle()
    popMatrix()
end

function ColorChooser:touched(touch)
    local tx, ty = touch.x, touch.y
    local xc, yc = WIDTH / 2, HEIGHT / 2
    local a, b, c, d = 90, 90, 180, 180
    if touch.state == BEGAN and touch.id ~= self.RING_ID and touch.id ~= self.ALPHA_ID and 
            not self.ON_TRIANGLE and (ty-yc)^2/b^2+(tx-xc)^2/a^2 <= 1 then
        self.ON_TRIANGLE = true
        self.TRIANGLE_ID = touch.id        
        if color(self.screen:get(touch.x//1, touch.y//1)).a ~= 0 then
            self.pickedColor = color(self.screen:get(touch.x//1, touch.y//1))
        end
    elseif touch.state == BEGAN and touch.id ~= self.TRIANGLE_ID and touch.id ~= self.ALPHA_ID and 
            not self.ON_RING and (ty-yc)^2/d^2+(tx-xc)^2/c^2 <= 1 then
        self.ON_RING = true
        self.RING_ID = touch.id
        self.ANGLE = math.atan2(touch.y - (HEIGHT / 2), touch.x - (WIDTH / 2)) - (math.pi / 2)
    elseif touch.state == BEGAN and touch.id ~= self.RING_ID and touch.id ~= self.TRIANGLE_ID and 
            not self.ON_ALPHA and touch.x >= self.x - (self.w / 2) and touch.y >= self.y - (self.h / 2) and 
            touch.x <= self.x + (self.w / 2) and touch.y <= self.y + (self.h / 2) then
        self.ON_ALPHA = true
        self.ALPHA_ID = touch.id
        local r, g, b, a = self.alphaScreen:get(
            (math.max(1, math.min(self.w, touch.x - (self.x - (self.w / 2)))))//1, 
            (math.max(1, math.min(self.h, touch.y - (self.y - (self.h / 2)))))//1)
        self.pickedColor.a = a
    end    
    if touch.state == MOVING and self.ON_RING and self.RING_ID == touch.id then
        self.ANGLE = math.atan2(touch.y - (HEIGHT / 2), touch.x - (WIDTH / 2)) - (math.pi / 2)
    elseif touch.state == MOVING and self.ON_TRIANGLE and self.TRIANGLE_ID == touch.id then
        if color(self.screen:get(touch.x//1, touch.y//1)).a ~= 0 then
            self.pickedColor = color(self.screen:get(touch.x//1, touch.y//1))
        end
    elseif touch.state == MOVING and self.ON_ALPHA and self.ALPHA_ID == touch.id then
        local r, g, b, a = self.alphaScreen:get(
            (math.max(1, math.min(self.w, touch.x - (self.x - (self.w / 2)))))//1, 
            (math.max(1, math.min(self.h, touch.y - (self.y - (self.h / 2)))))//1)
        self.pickedColor.a = a
    end    
    if self.ON_RING and self.RING_ID == touch.id and (touch.state == ENDED or touch.state == CANCELLED) then
        self.ON_RING = false
        self.RING_ID = 0
    elseif self.ON_TRIANGLE and self.TRIANGLE_ID==touch.id and (touch.state==ENDED or touch.state==CANCELLED) then
        self.ON_TRIANGLE = false
        self.TRIANGLE_ID = 0
    elseif self.ON_ALPHA and self.ALPHA_ID==touch.id and (touch.state==ENDED or touch.state==CANCELLED) then
        self.ON_ALPHA = false
        self.ALPHA_ID = 0
    end
end

-- From a guest on pastebin.com.
-- Slightly modified indentations to look prettier.
-- URL: http://pastebin.com/NrcJgL1d

----------------------------- 
-- HSV > RGB color conversion
----------------------------- 
-- adapted from: 
-- http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
-----------------------------

function ColorChooser:HSVtoRGB(h, s, v)
    local r, g, b    
    local i = math.floor(h * 6)
    local f = h * 6 - i
    local p = v * (1 - s)
    local q = v * (1 - f * s)
    local t = v * (1 - (1 - f) * s)
    local switch = i % 6    
    if switch == 0 then
        r,g,b=v,t,p
    elseif switch == 1 then
        r,g,b=q,v,p
    elseif switch == 2 then
        r,g,b=p,v,t
    elseif switch == 3 then
        r,g,b=p,q,v
    elseif switch == 4 then
        r,g,b=t,p,v
    elseif switch == 5 then
        r,g,b=v,p,q
    end   
    return math.floor(r * 255), math.floor(g * 255), math.floor(b * 255)
end

is there a reason to not allow both? is it a substantial optimization / speed increase?

seems like a precompiler pass type conversion could achieve the same thing?
why not allow 32 bit image buffers/color space? It is very useful for certain techniques and especially high end visual quality to have float32 buffer option for shaders and image processing techniques, and seems in the spirit of Lua to provide implicit type management.

@dave1707 @John @Simeon