We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I had some quick conceptual questions regarding pixels() which I'm having trouble grasping.
So basically I want to get the r, g and b values of my current mouse position. Now, I want this sketch to work irrespective of the window size (which happens to be 600x900 at the moment) - so I'm resizing using the resize function. Do i need to use loc = x + y*width differently now? And a little confused how to translate that to my current mouse position as well. Haven't really used PImage till now in Processing even though I've been using P5 for generative art for a while now. Some nice links would also be welcome, while the snippets in the reference give some ideas it's not really conceptually enlightening for bigger projects.
Answers
If you have a PImage object of width W and height H then it's colour data is stored in a one dimensional integer array of size W*H and the index of the array element corresponding to the position x, y is calculated from idx = x + y * W
If you resize the image to width W1 and height H1 then the array length will be changed to W1*H1 and the formula becomes idx = x + y *W1
Since PImage is an offscreen buffer then your talking about the mouse position does not make a lot of sense. You need to clarify what you mean.
Think that helps me a little, but not completely. Here's what I'm hoping to achieve, in plain text.
The convolution filter example helps me out a little, but as I said, conceptually I can't grasp how to achieve this in a processing sketch.
https://forum.Processing.org/two/discussion/18033/how-to-set-processing-to-automatically-detect-an-image-size-and-set-it-as-a-background
https://forum.Processing.org/two/discussion/16705/null-pointer-exception-when-loading-image-in-settings-p3-1-1#Item_1
@GoToLoop, no that doesn't entirely work as I want to keep the canvas (sketch?) size constant.
@quark + @GoToLoop let me post my updated code once I write it. Pretty sure it's not going to work so then you can help me out :)
Much appreciated!
So you have a sketch of size
width
andheight
. You also have a PImage that you have resized to fit the display while maintaining the aspect ratio. Assume that the of size this imagew
andh
.then because it fits the display either one or both of these statements must be true-
Since the image is centred in the display we can calculate the top-left corner (
tlX
andtlY
) of the image with these statementsWe can calculate the mouse position relative to the top-left corner of the image
Since the image may not fill the whole screen we need to test posX and posY
@quark seems right at first glance! I'll be testing over tonight and tomorrow, please give me some time to get back to you! Thanks!
@quark, okay so here I am at the moment. 2 problems, basically,
Can't seem to proportionally scale the image if the width and height are different (marked in comments). Math seems to beat me here, cannot come up with a function to change h or w accordingly.
And once I run it, the value of idx doesn't seem to change, or change slowly/not in sync with my mouse movement. What am I doing wrong here?
These lines
Should be done a bit different. The values of width and height are defined by size within your setup function call. Consider defining those values after the call to setup:
This does not answer your question, but more of keeping sane code.
Related to your question, you should try screenX and screenY:
https://processing.org/reference/screenX_.html
I hope this helps,
Kf
First of all you should not be loading images inside the draw() method because this is executed 60 times a second.
The solution to getting the new image size is to
1) calculate the scale factor needed to just fill the display horizontally
2) calculate the scale factor needed to just fill the display vertically
3) use the smaller of the 2 scale factors calculated.
It doesn't matter whether the image is smaller or larger that the display as this algorithm works with both.
The sketch below demonstrates how to do this. It also gets the pixel colour under the mouse and uses it for the background colour. Change line 1 with your filename.
@quark Thank you so so much!