Beispiel #1
0
 def set_at(self, pos, color):
     """
     Set color of a surface pixel.
     The arguments represent position x,y and color of pixel.
     """
     color = Color(color)
     try:
         self.setRGB(pos[0],pos[1],color.getRGB())
     except:     #ArrayOutOfBoundsException
         raise IndexError
     return None
Beispiel #2
0
 def replace_color(self, color, new_color=None):
     """
     Replace color with with new_color or with alpha.
     """
     pixels = self.getRGB(0,0,self.width,self.height,None,0,self.width)
     color1 = Color(color)
     if new_color:
         color2 = Color(new_color)
     else:
         color2 = Color(color1.r,color1.g,color1.b,0)
     for i, pixel in enumerate(pixels):
         if pixel == color1.getRGB():
             pixels[i] = color2.getRGB()
     self.setRGB(0,0,self.width,self.height,pixels,0,self.width)
     return None
Beispiel #3
0
def from_threshold(surface, color, threshold=(0,0,0,255)):
    """
    **pyj2d.mask.from_threshold**
    
    Return Mask from surface using a given color.
    Optional threshold argument to set color range and alpha threshold.
    """
    mask = Mask((surface.width, surface.height))
    pixels = surface.getRGB(0,0,surface.width,surface.height,None,0,surface.width)
    if threshold == (0,0,0,255):
        color = Color(color)
        if color.a != 255:
            color = Color(color.r,color.g,color.b,255)
        icolor = color.getRGB()
        i = 0
        for y in range(surface.height):
            for x in range(surface.width):
                if pixels[i] == icolor:
                    mask.set_at((x,y))
                i += 1
    else:
        color = Color(color)
        col = {}
        for i, c in enumerate(('r','g','b')):
            if threshold[i]:
                col[c+'1'] = color[i] - threshold[i] - 1
                col[c+'2'] = color[i] + threshold[i] + 1
            else:
                col[c+'1'] = color[i] - 1
                col[c+'2'] = color[i] + 1
        col['a'] = threshold[3] - 1
        i = 0
        for y in range(surface.height):
            for x in range(surface.width):
                if ( col['r1'] < ((pixels[i]>>16) & 0xff) < col['r2'] ) and ( col['g1'] < ((pixels[i]>>8) & 0xff) < col['g2'] ) and ( col['b1'] < ((pixels[i]) & 0xff) < col['b2'] ) and ( ((pixels[i]>>24) & 0xff) > col['a'] ):
                    mask.set_at((x,y))
                i += 1
    return mask