What is returned by this function? (colors)

I'm trying to write a program where the color of the pixel the mouse is over is returned. I'm getting a number back, but what does it mean? black seems to be -16777216 and white returns -1

color c = get(mouseX, mouseY);
println(c);

Answers

  • edited January 2016 Answer ✓

    Yes, color is just an integer (32 bits in the RAM). Processing repurposed it as a "color" but when you see "color" in Processing it is just an int. The preprocessor even replaces color in your code with int to make it valid java, they are absolutely equal. The first eight bits are alpha, the next eight bits are red, the following eight bits are green and the last eight bits are blue. Try doing println(binary(c)) for various colours to get more familiar with the structure.

    The reason white is -1 is because red, green and blue are 255, and 255 is 1111 1111 in binary so you get 1111 1111 1111 1111 1111 1111 1111 1111 which would be -1 I believe.

    If you truly had black it would be something like 1111 1111 0000 0000 0000 0000 0000 0000 which indeed turns out to be -16777216.

    If you want to retrieve the colour components, the easiest way is to do red(c), green(c) and blue(c).

  • Thank you very much!

  • Answer ✓

    BTW, if you add println(binary(c)); right under the previous println, it will print out the binary representation as well. In case your curious. Cheers

  • This is for the same project and I didn't want to create a new post, so ill ask here. I now know which pixels are a certain color, and want to now send the coordinates of those pixels to an arduino. How do I get the coordinate value of a pixel if pixels[] is one dimensional? Is there something that is like the opposite of get() ? Thanks!

  • Well, you used two coordinates in the get() method. In the example above that would be mouseX and mouseY. If you managed to find a colour with get() you also have the coordinates of that colour, because you needed those coordinates to find the colour in the first place! pixels[] is more or less similar to get() except that pixels[] is one dimensional so if you wanted to find a colour at a certain position (x, y) you would need to do pixels[image.width*y+x].

  • Sorry, I should have explained better. I now have it so that the program automatically goes through all the pixels in the window, and highlights the ones of a color I'm interested in (black pixels, for example, may be highlighted in red, and all other colored pixels are set to white). I now only have the pixels I'm interested in, and want their coordinates to be sent to an arduino. Basically, how can I convert their pixels[] number into x,y coordinates? can this be done?

  • edited January 2016 Answer ✓
    /**
     * Coords-Index Conversions (v1.3)
     * GoToLoop (2016-Jan-25)
     * forum.Processing.org/two/discussion/14609/what-is-returned-by-this-function
     */
    
    void setup() {
      PImage img = createImage(300, 200, ARGB);
      int w = img.width, h = img.height;
      println(w + "x" + h, "\tPixels:", w*h);
    
      int x = (int) random(w), y = (int) random(h);
      println("[" + x + ", " + y + "]");
    
      int idx = xy_to_idx(w, x, y);
      println("Index:", idx);
    
      int[] xy = idx_to_xy(w, idx);
      println(xy);
    
      assert xy[0] == x & xy[1] == y
      :
      "Coords. [" + x + ", " + y + "] don't match!!!";
    
      exit();
    }
    
    static final int xy_to_idx(final int w, final int... xy) {
      return w != 0 && xy != null && xy.length >= 2
        ? abs(w) * abs(xy[1]) + abs(xy[0]) : -1;
    }
    
    static final int[] idx_to_xy(int w, int idx, int... xy) {
      if (xy == null || xy.length < 2)  xy = new int[2];
    
      if ((w = abs(w)) != 0) {
        idx = abs(idx);
        xy[0] = idx % w;
        xy[1] = idx / w; // for Java only.
        //xy[1] = idx / w | 0; // for both Java & JS.
      } else xy[0] = xy[1] = -1;
    
      return xy;
    }
    
  • edited February 2016 Answer ✓

    Also a "Python Mode" version: :D

    """
     * Coords-Index Conversions (v1.4)
     * GoToLoop (2016-Jan-25)
     * forum.Processing.org/two/discussion/14609/what-is-returned-by-this-function
    """
    
    def setup():
        img = createImage(300, 200, ARGB)
        w, h = img.width, img.height
        print `w` + 'x' + `h` + '\tPixels: ' + `w*h`
    
        x, y = int(random(w)), int(random(h))
        print '[' + `x` + ', ' + `y` + ']'
    
        idx = xy_to_idx(w, x, y)
        print 'Index: ' + `idx`
    
        xy = idx_to_xy(w, idx)
        print xy
    
        assert xy[0] == x and xy[1] == y,\
               "Coords. [" + `x` + ", " + `y` + "] don't match!!!"
    
        exit()
    
    
    def xy_to_idx(w, x, y): return w and abs(int(w)) * abs(int(y)) + abs(int(x)) or -1
    
    
    def idx_to_xy(w, idx, xy=None):
        if type(xy) is not list or len(xy) < 2: xy = 2 * [None]
    
        if w and isinstance(w, Number) and isinstance(idx, Number):
            w, idx = abs(int(w)), abs(int(idx))
            xy[:] = idx % w, idx // w
    
        else: xy[0] = xy[1] = -1
    
        return xy
    
Sign In or Register to comment.