← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #272460]: detecting colors

 

Question #272460 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/272460

    Status: Open => Answered

Eugene Maslov proposed the following answer:
Hello Entity,
I can help with distinguishing the colors.
My findWithColor procedure finds an image with at least one pixel of exact color in the probe rectangle inside the image. So you may set, for example, gray target color for disabled menus and black color for active menus.
Debug mode allows seeing in the output the colors to select the target from.

import java.awt.Robot as jRobot
import java.awt.Color as Color
from sikuli import *

class RegionM(Region):
    def findWithColor(self, img, color=Color(255,255,255), probe=[0,0,1,1],debug=False): #probe=[top,left,width,height,0] from topleft
        myRobot=jRobot()
        res=False
        found=self.findAll(img)
        if not found:
            return False
        foundcolor=False 
        for but in found:
            for myy in range(but.getTopLeft().y+probe[1],but.getTopLeft().y+probe[1]+probe[3]):
                for myx in range(but.getTopLeft().x+probe[0],but.getTopLeft().x+probe[0]+probe[2]):
                    myColor=myRobot.getPixelColor(myx, myy)
                    if debug:                 
                        print "color at "+str(myx)+":" +str(myy)+":",myColor                        
                    if myColor==color:
                        foundcolor=True
                        res=but
                        if not debug:
                            break
                if foundcolor:
                    if not debug:
                        break
            if foundcolor:
                    if not debug:
                        break        
        return(res) 

#usage:
myreg=RegionM(SCREEN).findWithColor("ie1.png", color=Color(240,240,231), probe=[1,1,2,2], debug=True) #then set debug=False
mouseMove(myreg)


This piece of code is a part of an extended RegionM object, that I use for for simplicity of scripts. Please just remove the object notation if you don't need it.

And, partially concerning the other question, as far as I tried before,
there is no sense to make whole background or foreground of letters
transparent: then solid areas, filled with a color similar to remaining
non-transparent color, are selected instead of the letters.

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.