Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / getting the colour which is under the mouse pointer

Author
Message
Chrissell
11
Years of Service
User Offline
Joined: 2nd Apr 2013
Location:
Posted: 5th Apr 2020 14:55
Hi,
Im trying to convert a program I made in DBpro. What I need to do is get the colour which is under the mouse pointer?

Here is the code from DBpro.

if mouseclick()=1 then colour=point(mousex(),mousey()) : ink colour,rgb(255,255,255)

How can it be done in AGK2?.

Many thanks

Chris
C.R.Sellen
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 5th Apr 2020 15:06
The below function will return the colour (you may need to modify how it returns the colour for your program)

fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
Chrissell
11
Years of Service
User Offline
Joined: 2nd Apr 2013
Location:
Posted: 5th Apr 2020 15:55
Hi,
Thanks for the reply,
I tried this and was unable to get the color?

global x as integer
global y as integer
global color as integer
global red as integer
red = MakeColor(255,0,0)
do
DrawBox(100,100,120,120,red,red,red,red,1)
Print( ScreenFPS() )
if GetPointerPressed()=1
PickColor(x,y)
endif
PrintC(color)
Sync()
loop

Function pickColor(X,Y)

//color as integer
rem prepare image grab area
clearScreen()
setScissor(X,Y,X+1,Y+1)
render()

img = getImage(X,Y,X+1,Y+1)

rem create memblock

mem = createMemblockfromImage(img)

rem get memblock data
r = getMemblockbyte(mem,12):rem gets red channel data
g = getMemblockbyte(mem,13):rem gets green channel data
b = getMemblockbyte(mem,14):rem gets blue channel data


rem tidy up
deletememblock(mem)
deleteimage(img)
setScissor(0,0,getDeviceWidth(),getDeviceHeight())
clearScreen()
color = makeColor(r,g,b)
//color=0
endfunction color


Thanks
Chris
C.R.Sellen
JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 5th Apr 2020 18:05
I believe when 'clearscreen' is called in the function "PickColor(X,Y)", everything is deleted, so the only color returned is black, the color of the screen, which is the color that is returned with the function. You also need to identify the X and Y coordinates when you press the pointer. Try this:

Chrissell
11
Years of Service
User Offline
Joined: 2nd Apr 2013
Location:
Posted: 5th Apr 2020 18:18
Thankyou so much for the quick replies fubarpk and JosephB,

It works a treat

I will study the code and learn.

Take care

Chris
C.R.Sellen
Virtual Nomad
Moderator
18
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Apr 2020 18:22 Edited at: 5th Apr 2020 18:26
running your code (which should be placed in code tags):

produces a value for color when you click on the red square. in this case, (if i remember correctly) a compound color: -16777216.

i think i've got code somewhere to pull the RGB values out of that. until then, someone else might be able to provide it?

interestingly, while you're using:

which produced -16777216, when i ran your code, this:


produces -16776961, for me...

ack:
Quote: "so the only color returned is black"

that explains the difference. i expected it to be pulling color from the red box while i only glanced at the code
Virtual Nomad
Moderator
18
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 5th Apr 2020 18:52
Quote: "i think i've got code somewhere to pull the RGB values out of that"

for completion's sake, i thought it required more work but we've already got commands to do that:

JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 6th Apr 2020 17:56
I added the information that Virtual Nomad has provided to the previous color picker code to show how that additional commands work:

Chrissell
11
Years of Service
User Offline
Joined: 2nd Apr 2013
Location:
Posted: 6th Apr 2020 20:14
Thanks for the help guys

Really appreciate the help.

Its very interesting how to do it, to get the RGB values.

C.R.Sellen
Virtual Nomad
Moderator
18
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 6th Apr 2020 23:39 Edited at: 6th Apr 2020 23:40
Chrissell, would you care to share why you need to find the color beneath the pointer?

i ask because the GetImage() command comes with a (relative) lot of overhead. it's not a command you would want to call each frame, for example, in any "serious" app.

if you need similar functionality each frame (and, does the screen change every frame?), there are probably better methods.

IE, if it's a color picker for a "paint" program, you could determine the color values via the same math that generated the palette, instead.

otherwise, if it's "occasional", you can probably "get away" with the method offered above.
Chrissell
11
Years of Service
User Offline
Joined: 2nd Apr 2013
Location:
Posted: 8th Apr 2020 12:44
Hi Virtual Nomad,
Yes im working on a paint program, so needed to select a colour which was under the mouse when clicked?

How would you suggest selecting a colour instead?

Yes its really occasional to select a colour so, I guess it depends on which platform used speed wise?

Thanks
Chris
C.R.Sellen
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 22nd Apr 2020 21:59
Create a render image the same as your screen, render the screen you want to get the colour of to this - this is your sample source. Create a render image that is 1x1 pixels - this is your output sample. Create a quad, assign it the sample source. You can then pass in the X & Y coords you want sampled (your mouse coords), then use these as frag coords (use texelfetch) to render only that pixel colour to the output render image. You can then make this a memblock and read the value.

Can't promiss that it will be quicker than detailed above, but it puts the load more on the GPU.
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 22nd Apr 2020 22:21 Edited at: 22nd Apr 2020 22:22
So actually, I decided to knock my example up....gives about 1300-1400 FPS on my system.

Attachments

Login to view attachments
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 22nd Apr 2020 23:53 Edited at: 23rd Apr 2020 00:00
What's interesting in the original code, is that the closer you get to the bottom left hand corner the slower it gets. At a resolution of 1920*1080, and removing the mouse check so it is continual sampling, I get as low as 30fps in the bottom right most corner, but nearly 1000fps in the top left most. The original solution is completely fine if you only need it to function when the mouse is pressed though.

It did occur to me though that the basic function will work just as well without a shader with some tweaking, so here is a non shader version based on the original code that still gives about 1000-1100 fps at 1920*1080

Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 22nd Apr 2020 23:59 Edited at: 23rd Apr 2020 00:09
And here's a merger of the original code, using the full image as the sample again. Puts the frame rate at about 1000-1100 fps with no extrnal shader needed and no slow down at any part of the screen.

EDIT: so doing some testing and adding an average FPS counter, the shader version balances out at 1360fps for me, while the non shader version sits at 1200fps, both with sampling every frame. Would love to see what you get on a mobile.

Attachments

Login to view attachments
JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 23rd Apr 2020 00:55
I get around 400FPS on my Samsung S8. Just changed the getrawmousex() and getrawmousey() to getpointerx() and getpointery(), respectively to obtain results on the phone. Very nice.
Santman
13
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 23rd Apr 2020 01:52
This technique has all sorts of functions - good to see it peforms at speed on mobile too.

Login to post a reply

Server time is: 2024-11-22 04:35:57
Your offset time is: 2024-11-22 04:35:57