Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Image Tuner

edited July 2018 in Questions Posts: 31

Hello, i want to make a image tuner but i got an error when a green pixel is in my image is says that my tune color is nil!
It only works if no green pixel is in the image!??
Try it out your self, you will see what i mean
Just click on the screen to tune!

Heres my code:

--# Main

-- Use this function to perform your initial setup
function setup()
    makedImage = nil
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    collectgarbage()
    lifeImg = image(CAMERA)
    if lifeImg ~= nil then
        sprite(lifeImg:copy(lifeImg.width/2, lifeImg.height/2, 200, 200), WIDTH/2, HEIGHT/2, HEIGHT)
    end
    if makedImage ~= nil then
        sprite(makedImage, WIDTH/2, HEIGHT/2, HEIGHT)
        fill(0, 255, 0)
        rect(0, 0, 100, 100)
    end
end

function touched()
    collectgarbage()
    local i = image(CAMERA)
    if i ~= nil then
        makeImage(i)
    end
end

--# imagePerfect
RED =       color(255, 0, 0)
GREEN =     color(0, 255, 0)
BLUE =      color(0, 0, 255)
PURPLE =  color(255, 0, 255)
YELLOW =  color(255, 255, 0)
WHITE = color(255, 255, 255)
BLACK =       color(0, 0, 0)

function makeImage()
    local outp = ""
    local img = lifeImg:copy(lifeImg.width/2, lifeImg.height/2, 200, 200)
    for x=1,img.width do
        for y=1,img.height do
            local c = color(img:get(x, y))
            -- d# = Distance
            local _,d1 = isNear(c, RED)
            local _,d2 = isNear(c, GREEN)
            local _,d3 = isNear(c, BLUE)
            local _,d4 = isNear(c, PURPLE)
            local _,d5 = isNear(c, YELLOW)
            local _,d6 = isNear(c, BLACK)
            local _,d7 = isNear(c, WHITE)
            local _s = min({{d1, "1"}, {d2, "2"}, {d3, "3"}, {d4, "4"}, {d5, "5"}, {d6, "6"}, {d7, "7"}})
            if _s == "1" then
                c = RED
            elseif _s == "2" then
                c = GREEEN
            elseif _s == "3" then
                c = BLUE
            elseif _s == "4" then
                c = PURPLE
            elseif _s == "5" then
                c = YELLOW
            elseif _s == "6" then
                c = BLACK
            elseif _s == "7" then
                c = WHITE
            end
            img:set(x, y, c.r, c.g, c.b)
        end
    end
    makedImage = img:copy()
end

function isNear(c1, c2, md)
    local dr = math.abs(c1.r-c2.r)
    local dg = math.abs(c1.g-c2.g)
    local db = math.abs(c1.b-c2.b)
    local d = math.max(dr, dg, db)
    if d > (md or 0) then
        return false, d
    else
        return true, d
    end
end



--# min_max


function min(t1)
    local min = math.huge
    minName = ""
    for _,t2 in ipairs(t1) do
        if t2[1] <= min then
            min = t2[1]
            minName = t2[2]
        end
    end
    return minName
end

function max(t1)
    local max = 0
    maxName = ""
    for _,t2 in ipairs(t1) do
        if t2[1] >= max then
            max = t2[1]
            maxName = t2[2]
        end
    end
    return maxName
end
Tagged:

Comments

  • Posts: 31

    Sorry for code, you have to re-indent it!

  • dave1707dave1707 Mod
    Posts: 7,533

    @Silvio2400 When you post code, put 3 ~'s on a line before and after your code. I added them above for you. As for your error, in one place you have GREEN, in another place you have GREEEN, one too many E's. You knew you had a problem with GREEN, you just didn’t look close enough.

  • Posts: 31

    Oh, LOL! i did not see it!
    Thanks! :smiley:

Sign In or Register to comment.