Finding Co-ordinates of a colour

Discussion in 'Programming General' started by iJava, Nov 21, 2011.

Finding Co-ordinates of a colour
  1. Unread #1 - Nov 21, 2011 at 5:17 PM
  2. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Finding Co-ordinates of a colour

    Hi,

    I'm wondering if anyone has any good method of getting the x and y co-ordinates of a rgb colour?

    Thanks
    iJava
     
  3. Unread #2 - Nov 21, 2011 at 7:10 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Finding Co-ordinates of a colour

    Found this in some code I wrote a long time ago. Not super efficient but it works.

    Code:
       public Point findColorTolerance(int x, int y, int width, int height, int color, int tolerance) {
          contentArea = robot.createScreenCapture(area);
          
          Color searchColor = new Color(color);
          
          int searchRed = searchColor.getRed();
          int searchGreen = searchColor.getGreen();
          int searchBlue = searchColor.getBlue();
          
          for(int i = y; i < y + height; i++) {
             for(int j = x; j < x + width; j++) {
                int pixel = contentArea.getRGB(j, i);
                
                Color foundColor = new Color(pixel);
                int red = foundColor.getRed();
                int green = foundColor.getGreen();
                int blue = foundColor.getBlue();
                
                if( Math.abs(red-searchRed) <= tolerance && Math.abs(green-searchGreen) <= tolerance && Math.abs(blue-searchBlue) <= tolerance ) {
                   return new Point(j + area.x, i + area.y);
                }
             }
          }
          return new Point(-1, -1);
       }
    
    Also, found a function I wrote to move the mouse using a bezier curve which looks more random/human-like than just moving in a straight line.

    Code:
       public void mouseMove(int x, int y) {
          if(x < 0 || y < 0)
             return;
             
          Random r = new Random(System.currentTimeMillis());
          
          Point[] points = new Point[4];
          double k = .010;
          double x2 ,y2;
          
          Point current = getCursor();
          
          points[0] = new Point(current.x, current.y);
          points[3] = new Point(x, y);
          
          if(r.nextInt(100) < 5) {
             points[1] = new Point(r.nextInt(1000), r.nextInt(1000));
             points[2] = new Point(r.nextInt(1000), r.nextInt(1000));
          }
          else {
             points[1] = new Point(current.x + r.nextInt(Math.abs(x - current.x)), r.nextInt(Math.abs(y - current.y)));
             points[2] = new Point(current.x + r.nextInt(Math.abs(x - current.x)), r.nextInt(Math.abs(y - current.y)));      
          }
          
          for(double t = k; t <= 1+k; t+=k) {
             x2 = (points[0].x + t*(-points[0].x*3 + t*(3*points[0].x - points[0].x*t))) + 
                t*(3*points[1].x + t*(-6*points[1].x + points[1].x*3*t)) + t*t*(points[2].x*3 - points[2].x*3*t) +
                points[3].x*t*t*t;
             
             y2 = (points[0].y + t*(-points[0].y*3 + t*(3*points[0].y - points[0].y*t))) +
                t*(3*points[1].y + t*(-6*points[1].y + points[1].y*3*t)) + t*t*(points[2].y*3 - points[2].y*3*t) +
                points[3].y*t*t*t;
             
             robot.mouseMove((int)x2, (int)y2);
             
             try{
                Thread.sleep(20);
             }
             catch(Exception e) {
             }
          }
       }
    
     
  5. Unread #3 - Nov 21, 2011 at 8:11 PM
  6. iJava
    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Finding Co-ordinates of a colour


    Thanks for replying, however if I don't have the x, y and height then how would the usage of the first method go?
     
  7. Unread #4 - Nov 21, 2011 at 11:29 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Finding Co-ordinates of a colour

    Sorry, shouldn't have assumed you would understand the usage.

    The parameters let you define a square of pixels to search in for the color where the point (x, y) is the top left corner of the box and the width and height specify its dimensions. The method returns a Point object with the x and y coordinates of the first pixel in the square that matches the color within the specified tolerance.

    The color tolerance of course lets you take into account that often matching an exact color isn't always possible or desirable so there can be some offset (tolerance) in each of the R, G, and B components.
     
< Flash game programing | Happy Programming >

Users viewing this thread
1 guest


 
 
Adblock breaks this site