Page 2 of 3

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 5:03 pm
by nice
artofwork wrote: love.graphics.setColor() doesn't return a value so your local variable Red would be nil.
huh, it made sense to me and I guess that it would still be nil if I exchanged red with the number 1?
Zilarrezko wrote:

Code: Select all

Screenshot = love.graphics.newScreenshot(true) --Takes a screen shot, and returns an image data. The parameter is a boolean "Copy Alpha"

local r, g, b, a = Screenshot:getPixel(mouseX - 1, mouseY - 1) --image data is 0 based, so we minus 1 as Lua and love2d is 1 based.

--Use r, g, b, a however you want. (Not sure what it will do if you don't copy the alpha though. I didn't test it.)

--After storing the variable or something... Delete the image object, or just make it local so it will delete itself after a function ends.
If you wanted to pick a color with your mouse.
I appreciate the thought but it's not what I'm currently looking for, I'm still into the idea of having the colours accessed by the numbers on the keyboard.

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 6:05 pm
by Zilarrezko
Well I just spent 5 minutes putting this together. In case you didn't get your question answered.

Code: Select all

function love.load()
        daColors = {{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255}}
	currentColor = 1
end

function love.draw()
        for i = 1, #daColors do
		love.graphics.setColor(daColors[i])
		if i == currentColor then
			love.graphics.rectangle("fill", 10, 10 + i*50, 100, 50)
		else
			love.graphics.rectangle("line", 10, 10 + i*50, 100, 50)
		end
	end
end

function love.mousepressed(x, y, button)
       if button == "l" then
		local screenshot = love.graphics.newScreenshot()
		local r, g, b, a = screenshot:getPixel(x - 1, y - 1)
		daColors[currentColor] = {r, g, b, a}
	end
end

function love.keypressed(key) --You could do some string magic. But it's simpler to do this, although more tedious -_-
        if key == "kp1" then
		currentColor = 1
	elseif key == "kp2" then
		currentColor = 2
	elseif key == "kp3" then
		currentColor = 3
	elseif key == "kp4" then
		currentColor = 4
	elseif key == "kp5" then
		currentColor = 5
	elseif key == "kp6" then
		currentColor = 6
	elseif key == "kp7" then
		currentColor = 7
	elseif key == "kp8" then
		currentColor = 8
	elseif key == "kp9" then
		currentColor = 9
	end
end
Sorry if the code is a bit wonky. I just copy and pasted.

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 6:20 pm
by nice
Zilarrezko wrote:Well I just spent 5 minutes putting this together. In case you didn't get your question answered.

Code: Select all

function love.load()
        daColors = {{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255}}
	currentColor = 1
end

function love.draw()
        for i = 1, #daColors do
		love.graphics.setColor(daColors[i])
		if i == currentColor then
			love.graphics.rectangle("fill", 10, 10 + i*50, 100, 50)
		else
			love.graphics.rectangle("line", 10, 10 + i*50, 100, 50)
		end
	end
end

function love.mousepressed(x, y, button)
       if button == "l" then
		local screenshot = love.graphics.newScreenshot()
		local r, g, b, a = screenshot:getPixel(x - 1, y - 1)
		daColors[currentColor] = {r, g, b, a}
	end
end

function love.keypressed(key) --You could do some string magic. But it's simpler to do this, although more tedious -_-
        if key == "kp1" then
		currentColor = 1
	elseif key == "kp2" then
		currentColor = 2
	elseif key == "kp3" then
		currentColor = 3
	elseif key == "kp4" then
		currentColor = 4
	elseif key == "kp5" then
		currentColor = 5
	elseif key == "kp6" then
		currentColor = 6
	elseif key == "kp7" then
		currentColor = 7
	elseif key == "kp8" then
		currentColor = 8
	elseif key == "kp9" then
		currentColor = 9
	end
end
Sorry if the code is a bit wonky. I just copy and pasted.
Wow! I didn't really expect this, I will check this code out (not right now as it's late where I am) and see if it works.

Code: Select all

function love.load()
        daColors = {{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255},{255, 255, 255, 255}}
	currentColor = 1
end
Could you elaborate why this part of the code looks like it does? does it have a purpose or is it just an example?

Also thank you for the help, it's appreciated! :awesome:

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 6:30 pm
by Zilarrezko
Yeah, "daColors" is the table that holds the colors. I made them all 255,255,255,255 (white). Because it was simple haha. Current color is the color that is currently selected, so that way when you click on a pixel to pick a color from the screen, it will copy that pixel's color onto the currently selected index in the daColors table.

And it's in the love.load function because I only want it to initialize it once.

Unless you are asking why I have tables inside tables. And that's just to make things easier so I can draw everything in an easy for loop, and access/change those colors easier.

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 6:31 pm
by artofwork
Its a table of colors @Nice

@Zilarrezko your a great coder but what you wrote is overkill :p

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 6:35 pm
by Zilarrezko
artofwork wrote: @Zilarrezko your a great coder but what you wrote is overkill :p
:awesome:

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 6:45 pm
by nice
Zilarrezko wrote:Yeah, "daColors" is the table that holds the colors. I made them all 255,255,255,255 (white). Because it was simple haha. Current color is the color that is currently selected, so that way when you click on a pixel to pick a color from the screen, it will copy that pixel's color onto the currently selected index in the daColors table.

And it's in the love.load function because I only want it to initialize it once.

Unless you are asking why I have tables inside tables. And that's just to make things easier so I can draw everything in an easy for loop, and access/change those colors easier.
Ah okay, I just wanted to make it clear for myself :)

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 7:29 pm
by DaedalusYoung
artofwork wrote:
nice wrote:
I started thinking wouldn't it be easier if I gave my colours a local statement instead for example:

Code: Select all

 local Red = love.graphics.setColor(235, 0, 0) 
I don't really know how the other part of the code would look like but my theory would be that I would write the code needed and give it my local statement.
Is this more complicated or am I going the wrong way?
love.graphics.setColor() doesn't return a value so your local variable Red would be nil.
You could of course do something like:

Code: Select all

local Red = { 235, 0, 0 }
But it's easier to use an indexed table rather than individual colours.

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 7:39 pm
by nice
DaedalusYoung wrote:
artofwork wrote:
nice wrote:
I started thinking wouldn't it be easier if I gave my colours a local statement instead for example:

Code: Select all

 local Red = love.graphics.setColor(235, 0, 0) 
I don't really know how the other part of the code would look like but my theory would be that I would write the code needed and give it my local statement.
Is this more complicated or am I going the wrong way?
love.graphics.setColor() doesn't return a value so your local variable Red would be nil.
You could of course do something like:

Code: Select all

local Red = { 235, 0, 0 }
But it's easier to use an indexed table rather than individual colours.
Why is it easier? I might sound, I don't know, repetitive? I'm asking because I want to know and learn.

Re: Help: Picking colours and see what current colour you ha

Posted: Wed Sep 24, 2014 7:50 pm
by DaedalusYoung
Because you need to have a variable to remember the currently selected colour. In Zilarrezko's code, you can see this, it is called currentColor. If you use numbers here, you can directly use that number to set the selected colour in love.draw.

If you use unique names for each colour, you can't do this, you'd have to make some strange and long if-block, like this:

Code: Select all

if currentColor == 1 then
    love.graphics.setColor(Red)
elseif currentColor == 2 then
    love.graphics.setColor(Orange)
elseif currentColor == 3 then
    love.graphics.setColor(Yellow)
    -- and so on
end
You can see how that is ugly and bulky. In addition, if you ever decide you want to change the colours; shuffle them up, or make a whole new palette, then you're either stuck with these old names, or you have to go through your entire program and change the variable names everywhere.