Esempio n. 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 ArrayIndexOutOfBoundsException:
         raise IndexError('pixel index out of range')
     return None
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 4
0
File: draw.py Progetto: jggatc/pyj2d
def polygon(surface, color, pointlist, width=0):
    """
    Draw polygon shape, and returns bounding Rect.
    Argument include surface to draw, color, and pointlist.
    Optional width argument of outline, which defaults to 0 for filled shape.
    """
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON)
    xpts = [int(pt[0]) for pt in pointlist]
    ypts = [int(pt[1]) for pt in pointlist]
    npts = len(pointlist)
    if width:
        g.setStroke(BasicStroke(width))
        g.drawPolygon(xpts, ypts, npts)
    else:
        g.fillPolygon(xpts, ypts, npts)
    g.dispose()
    if not _return_rect:
        return None
    xmin = min(xpts)
    xmax = max(xpts)
    ymin = min(ypts)
    ymax = max(ypts)
    rect = Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)
    return surface.get_rect().clip(rect)
Esempio n. 5
0
File: draw.py Progetto: jggatc/pyj2d
def arc(surface, color, rect, start_angle, stop_angle, width=1):
    """
    Draw arc shape, and returns bounding Rect.
    Argument include surface to draw, color, rect, start_angle, stop_angle.
    Optional width argument of outline.
    """
    if not hasattr(rect, 'width'):
        rect = Rect(rect)
    start_angle = int(start_angle * _rad_deg)
    stop_angle = int(stop_angle * _rad_deg)
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON)
    if width:
        g.setStroke(BasicStroke(width))
        g.drawArc(rect.x, rect.y, rect.width - 1, rect.height - 1, start_angle,
                  stop_angle)
    else:
        g.fillArc(rect.x, rect.y, rect.width - 1, rect.height - 1, start_angle,
                  stop_angle)
    g.dispose()
    if not _return_rect:
        return None
    return surface.get_rect().clip(rect)
Esempio n. 6
0
File: draw.py Progetto: jggatc/pyj2d
def lines(surface, color, closed, pointlist, width=1):
    """
    Draw interconnected lines, and returns Rect bound.
    Argument include surface to draw, color, closed, and pointlist.
    Optional width argument of line.
    """
    xpoints = [int(p[0]) for p in pointlist]
    ypoints = [int(p[1]) for p in pointlist]
    if closed:
        xpoint, ypoint = xpoints[0], ypoints[0]
        xpoints.append(xpoint)
        ypoints.append(ypoint)
    npoints = len(xpoints)
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON)
    g.setStroke(BasicStroke(width))
    g.drawPolyline(xpoints, ypoints, npoints)
    g.dispose()
    if not _return_rect:
        return None
    xmin = min(xpoints)
    xmax = max(xpoints)
    ymin = min(ypoints)
    ymax = max(ypoints)
    rect = Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)
    return surface.get_rect().clip(rect)
Esempio n. 7
0
File: draw.py Progetto: jggatc/pyj2d
def line(surface, color, point1, point2, width=1):
    """
    Draw line, and returns bounding Rect.
    Argument include surface to draw, color, point1, point2.
    Optional width argument of line.
    """
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON)
    g.setStroke(BasicStroke(width))
    g.drawLine(int(point1[0]), int(point1[1]), int(point2[0]), int(point2[1]))
    g.dispose()
    if not _return_rect:
        return None
    xpts = [pt[0] for pt in (point1, point2)]
    ypts = [pt[1] for pt in (point1, point2)]
    xmin = min(xpts)
    xmax = max(xpts)
    ymin = min(ypts)
    ymax = max(ypts)
    rect = Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)
    return surface.get_rect().clip(rect)
Esempio n. 8
0
 def get_at(self, pos):
     """
     Return color of a surface pixel.
     The pos argument represents x,y position of pixel.
     """
     try:
         return Color(self.getRGB(pos[0], pos[1]))
     except ArrayIndexOutOfBoundsException:
         raise IndexError('pixel index out of range')
Esempio n. 9
0
def create_cursor(size, data, mask):
    """
    Create cursor image from binary data.
    Arguments cursor size and its binary data and mask.
    Return surface, can be used with mouse.set_cursor.
    """
    surface = Surface(size, Const.SRCALPHA)
    black = Color(0, 0, 0, 255).getRGB()
    white = Color(255, 255, 255, 255).getRGB()
    x = y = 0
    for i in range(len(data)):
        if data[i] or mask[i]:
            for j in range(8):
                if data[i] & 0x01 << 7 - j:
                    surface.setRGB(x + j, y, black)
                elif mask[i] & 0x01 << 7 - j:
                    surface.setRGB(x + j, y, white)
        x += 8
        if x >= size[0]:
            x = 0
            y += 1
    return surface
Esempio n. 10
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
Esempio n. 11
0
 def fill(self, color=(0,0,0), rect=None):
     """
     Fill surface with color.
     """
     g2d = self.createGraphics()
     color = Color(color)
     g2d.setColor(color)
     if not rect:
         rect = Rect(0, 0, self.width, self.height)
     else:
         rect = Rect(rect)
     g2d.fillRect(rect.x, rect.y, rect.width, rect.height)
     g2d.dispose()
     return rect
Esempio n. 12
0
 def set_colorkey(self, color, flags=None):
     """
     Set surface colorkey.
     """
     if self._colorkey:
         r = self._colorkey.r
         g = self._colorkey.g
         b = self._colorkey.b
         self.replace_color((r,g,b,0), self._colorkey)
         self._colorkey = None
     if color:
         self._colorkey = Color(color)
         self.replace_color(self._colorkey)
     return None
Esempio n. 13
0
 def render(self, text, antialias, color, background=None):
     """
     Render text onto surface.
     Arguments:
     text to render (string)
     antialias of text (bool)
     color of text (R,G,B)
     background color (R,G,B)
     """
     w, h = self.size(text)
     surf = Surface((w, h), BufferedImage.TYPE_INT_ARGB)
     g2d = surf.createGraphics()
     if background:
         g2d.setColor(Color(background))
         g2d.fillRect(0, 0, w, h)
     g2d.setFont(self.font)
     if antialias:
         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                              RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
     g2d.setColor(Color(color))
     y = ((h // 2) -
          ((self.fontMetrics.getAscent() + self.fontMetrics.getDescent()) //
           2) + self.fontMetrics.getAscent())
     if not self.underline:
         g2d.drawString(text, 0, y)
     else:
         try:
             text = AttributedString(text)
             text.addAttribute(TextAttribute.FONT, self.font)
             text.addAttribute(TextAttribute.UNDERLINE,
                               TextAttribute.UNDERLINE_ON)
             g2d.drawString(text.getIterator(), 0, y)
         except IllegalArgumentException:
             pass
     g2d.dispose()
     return surf
Esempio n. 14
0
 def __init__(self, *arg):
     """
     Return Surface that is subclassed from java.awt.image.BufferedImage.
     
     Alternative arguments:
     
     * Size (w,h) of surface, optional second argument of flags (SRCALPHA)
     * Bufferedimage to convert to Surface
     
     Module initialization places pyj2d.Surface in module's namespace.
     """
     try:
         width, height = arg[0]
         try:
             if arg[1] & (BufferedImage.TYPE_INT_ARGB | Const.SRCALPHA):
                 BufferedImage.__init__(self, width, height,
                                        BufferedImage.TYPE_INT_ARGB)
             else:
                 BufferedImage.__init__(self, width, height,
                                        BufferedImage.TYPE_INT_RGB)
         except IndexError:
             BufferedImage.__init__(self, width, height,
                                    BufferedImage.TYPE_INT_ARGB)
             graphics2D = self.createGraphics()
             graphics2D.setColor(Color(0,0,0))
             graphics2D.fillRect(0, 0, width, height)
             graphics2D.dispose()
     except TypeError:
         try:
             cm = arg[0].getColorModel()
             raster = arg[0].getRaster()
             isRasterPremultiplied = arg[0].isAlphaPremultiplied()
             properties = Hashtable()
             keys = arg[0].getPropertyNames()
             if keys != None:
                 for key in keys:
                     properties.put(key, arg[0].getProperty(key))
         except AttributeError:
             cm, raster, isRasterPremultiplied, properties = arg
         BufferedImage.__init__(self, cm, raster,
                                isRasterPremultiplied, properties)
     self._display = None    #display surface
     self._super_surface = None
     self._offset = (0,0)
     self._colorkey = None
     self._nonimplemented_methods()
Esempio n. 15
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)
     if hasattr(color, 'a'):
         color1 = color
     else:
         color1 = Color(color)
     if new_color is None:
         color2 = Color(color1.r, color1.g, color1.b, 0)
     else:
         if hasattr(new_color, 'a'):
             color2 = new_color
         else:
             color2 = Color(new_color)
     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
Esempio n. 16
0
File: mask.py Progetto: jggatc/pyj2d
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
Esempio n. 17
0
File: draw.py Progetto: jggatc/pyj2d
def rect(surface, color, rect, width=0):
    """
    Draw rectangle shape, and returns bounding Rect.
    Argument include surface to draw, color, Rect.
    Optional width argument of outline, which defaults to 0 for filled shape.
    """
    if not hasattr(rect, 'width'):
        rect = Rect(rect)
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    if width:
        g.setStroke(BasicStroke(width))
        g.drawRect(rect.x, rect.y, rect.width, rect.height)
    else:
        g.fillRect(rect.x, rect.y, rect.width, rect.height)
    g.dispose()
    if not _return_rect:
        return None
    return surface.get_rect().clip(rect)
Esempio n. 18
0
File: draw.py Progetto: jggatc/pyj2d
def circle(surface, color, position, radius, width=0):
    """
    Draw circular shape, and returns bounding Rect.
    Argument include surface to draw, color, position and radius.
    Optional width argument of outline, which defaults to 0 for filled shape.
    """
    rect = Rect(position[0] - radius, position[1] - radius, 2 * radius,
                2 * radius)
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON)
    if width:
        g.setStroke(BasicStroke(width))
        g.drawOval(rect.x, rect.y, rect.width, rect.height)
    else:
        g.fillOval(rect.x, rect.y, rect.width, rect.height)
    g.dispose()
    if not _return_rect:
        return None
    return surface.get_rect().clip(rect)
Esempio n. 19
0
File: draw.py Progetto: jggatc/pyj2d
def ellipse(surface, color, rect, width=0):
    """
    Draw ellipse shape, and returns bounding Rect.
    Argument include surface to draw, color, and rect.
    Optional width argument of outline, which defaults to 0 for filled shape.
    """
    if not hasattr(rect, 'width'):
        rect = Rect(rect)
    g = surface.createGraphics()
    if hasattr(color, 'a'):
        g.setColor(color)
    else:
        g.setColor(Color(color))
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON)
    ellipse = Ellipse2D.Double(rect.x, rect.y, rect.width, rect.height)
    if width:
        g.draw(ellipse)
    else:
        g.fill(ellipse)
    g.dispose()
    if not _return_rect:
        return None
    return surface.get_rect().clip(rect)