Comparing RGB Colours

Asked by Dave Marsden

I'm trying to compare some pixels to a set colour to find the extents of some selected text. I'm using the code.

    PixColour = MyRobot.getPixelColor(c,y)
    pColour = PixColour.getRGB()
    print PixColour, hex(pColour), hex(colour)
    if pColour == colour:
        print c

Which is printing out

java.awt.Color[r=49,g=106,b=197] -0xce953b 0x316ac5

By my reckoning the RGB values should give a hex value of 0x316ac5 however they aren't they are give -0xce953b and hence my detection of the colour is failing.

Can anyone explain to me where I'm going wrong

Thanks

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

If you want to compare a pixel color this way using the AWT Robot, you should stay on the Color level:

import java.awt.Robot as JR
import java.awt.Color as JC
rob = JR()
col = rob.getPixelColor(some.x, some.y) # some is some Location()
comp = JC(228, 55, 50)
print redc == comp

But be aware, that this feature is very slow and only acceptable to check a few pixels.

In your case it might be easier to create small targets (e.g. 4x4 Pixels as the corner of the selected area) and search it with something like:

region_left_corner.nearby(3).right(300)

where the nearby(3) creates some extra padding area around.

Can you help with this problem?

Provide an answer of your own, or ask Dave Marsden for more information if necessary.

To post a message you must log in.