# How to play a sound when ( colorFromGet == pas )?

**URL:** https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426
**Category:** Libraries
**Created:** [June 1, 2021, 6:27pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426 "2021-06-01T18:27:18Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [June 1, 2021, 6:27pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/1 "2021-06-01T18:27:18Z")

</div>

```auto
int x=1;
int y=1;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
color pas = color(11, 96, 133);

Robot robot;
void setup() {
  size(1500, 3000);
  try {
    robot = new Robot();
  } 
  catch (Exception e) {
    println(e.getMessage());
  }
}
void draw() {
  background(0);
  Rectangle r = new Rectangle(y, x, width, height);
  BufferedImage img1 = robot.createScreenCapture(r);
  PImage img2 = new PImage(img1);
  image(img2, 0, 0);
  get(x,y);
  x++;
  y++;
  color colorFromGet = get(x,y);

if ( colorFromGet == pas ) //here it checks if they are the same
     println("Same"); 
}

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [June 1, 2021, 7:03pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/2 "2021-06-01T19:03:16Z")

</div>

Hello,

Please format your code as a courtesy to the community helping you:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

Resources:

- [https://processing.org/reference/libraries/](https://processing.org/reference/libraries/) There is a sound library.
- [Sound \ Processing.org](https://processing.org/tutorials/sound/)

And many examples in the Processing application to try:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8003846153f0537430a3c912399ce2fb62efca84.png)

Give it a try!

`:)`

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [June 1, 2021, 7:04pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/3 "2021-06-01T19:04:04Z")

</div>

what did i do wrong in formatting this time i thought i did it proerly?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 1, 2021, 7:31pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/4 "2021-06-01T19:31:21Z")

</div>

go back, edit your post using the pen symbol underneath it.

Select the entire code section and click `</>` symbol in the small command bar

```auto
Looks
like 
this

```

Your code is confusing.

What is your intention?

As has been said please write your goals and questions in the text section and not only in the title.

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [June 1, 2021, 7:32pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/5 "2021-06-01T19:32:33Z")

</div>

i am screen recording my screen, and want it to see if a specific color is on that screen and if it is it has to play a sound

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 1, 2021, 7:37pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/6 "2021-06-01T19:37:12Z")

</div>

with size(1500, 3000); there is hardly much else on your screen than **your Sketch**?

Why do you make so many screenshots? ONE should be enough. In setup().

And then go over it with a nested for loop (2 fors) and check the color.

When you just say x++ y++ in the same frame of draw() you’re only testing the diagonal points of the center line…

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [June 1, 2021, 7:40pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/7 "2021-06-01T19:40:12Z")

</div>

but how do you play a sound when (colorFromGet== pas) with this code?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 1, 2021, 7:41pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/8 "2021-06-01T19:41:33Z")

</div>

seriously? Your code can’t find a color as I just explained. It doesn’t search your screenshot.

Read [Images and Pixels \ Processing.org](https://www.processing.org/tutorials/pixels/)

Sound (WHEN and only when you developed your Sketch, **now it’s of no use** ): [SoundFile \ Language (API) \ Processing 3+](https://www.processing.org/reference/libraries/sound/SoundFile.html)

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [June 1, 2021, 7:44pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/9 "2021-06-01T19:44:44Z")

</div>

doesnt it scan every line one by one if you add 1 to x and y the whole time?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 1, 2021, 7:51pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/10 "2021-06-01T19:51:03Z")

</div>

err… NO

first of all, read the tutorial there is a code how to go over each pixel in an image/screenshot

think about it first time draw() runs both are 0, second time both (!) are 1 etc. - you only scan the diagonal “” of the image

what you need is:

- x is 0 (first row of the image) y goes from 0 to height (x stays 0);
- x is 1 (2nd row of the image) y goes from 0 to height;
- x is 2 (2nd row of the image) y goes from 0 to height;

That’s achieved with a nested for loop

```auto
for (x....................
     for (y........
           COMPARE ---> play sound
     }
}

```

---

<div class="post-metadata">

### Author: ![MoonAlien822](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@MoonAlien822](https://discourse.processing.org/u/MoonAlien822)
#### Post date: [June 1, 2021, 7:53pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/11 "2021-06-01T19:53:24Z")

</div>

what is a nested for loop. im learning with a youtube channel and i havent heard about that yet

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 1, 2021, 7:56pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/12 "2021-06-01T19:56:20Z")

</div>

read the tutorial

it has an example without nested for loop, just ONE for-loop

QUOTE

Take the following simple example. This program sets each pixel in a window to a random grayscale value. The pixels array is just like an other array, the only difference is that we don’t have to declare it since it is a Processing built-in variable.

[Example: Setting Pixels](http://learningprocessing.com/examples/chp15/example-15-05-PixelArray)

```auto

size(200, 200); // Before we deal with pixels loadPixels();
 // Loop through every pixel 

for (int i = 0; i < pixels.length; i++) { 
// Pick a random number, 0 to 255 
float rand = random(255); 

// Create a grayscale color based on random number 
color c = color(rand); 

// Set pixel at that location to random color 
pixels[i] = c; 
} 

// When we are finished dealing with pixels 

updatePixels();

```

(this code changes the pixels, you only want read them. So change this. But it’s the correct way to go through ALL points. Also it works on the Sketch surface and not on an image, but the principle is the same, see below)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 1, 2021, 7:58pm UTC](https://discourse.processing.org/t/how-to-play-a-sound-when-colorfromget-pas/30426/13 "2021-06-01T19:58:37Z")

</div>

as explained here: [PImage \ Language (API) \ Processing 3+](https://processing.org/reference/PImage.html)

just search the screenshot image and NOT display the screenshot and DON’T search the screen using get directly.

use [loadPixels()](https://processing.org/reference/PImage_loadPixels_.html) and **get** on the image directly

- see [PImage \ Language (API) \ Processing 3+](https://processing.org/reference/PImage.html) for both
