def draw(self, ctx: Context, square: bool) -> None: point_diameter_current = self.point_diameter_initial ctx.set_source_rgb(self.color[0], self.color[1], self.color[2]) points_per_row = math.floor(self.target_width / self.point_margin) i = 0 while ((self.point_diameter_check_is_greater and point_diameter_current > self.point_diameter_final) or (not self.point_diameter_check_is_greater and point_diameter_current < self.point_diameter_final)): col = i % points_per_row row = math.floor(i / points_per_row) x = self.position[ 0] + col * self.point_margin + self.point_margin / 2 y = self.position[ 1] + row * self.point_margin + self.point_margin / 2 radius = point_diameter_current / 2 if square: ctx.move_to(x - radius, y - radius) ctx.line_to(x + radius, y - radius) ctx.line_to(x + radius, y + radius) ctx.line_to(x - radius, y + radius) ctx.fill() else: ctx.arc(x, y, radius, 0, 2 * math.pi) ctx.fill() i += 1 point_diameter_current += self.point_diameter_delta
def draw_rounded_rectangle(ctx: Context, rectangle: Rectangle, radius: float): """ Draw rectangles with rounded (circular arc) corners. :param ctx: Cairo render context. :param rectangle: Extent of the rectangle. :param radius: Radius of the corners. """ from math import pi a, c = rectangle.position(Anchor.TOP_LEFT) b, d = rectangle.position(Anchor.BOTTOM_RIGHT) ctx.arc(a + radius, c + radius, radius, 2 * (pi / 2), 3 * (pi / 2)) ctx.arc(b - radius, c + radius, radius, 3 * (pi / 2), 4 * (pi / 2)) ctx.arc(b - radius, d - radius, radius, 0 * (pi / 2), 1 * (pi / 2)) # ;o) ctx.arc(a + radius, d - radius, radius, 1 * (pi / 2), 2 * (pi / 2)) ctx.close_path()
def __draw_sector (self: 'HueSatWheelWidget', cr: cairocffi.Context, center_x: float, center_y: float) -> None: cr.save () cr.set_line_width (1) offset = self.rotation for idx, sector in enumerate (self.sector[0]): half_angle = 2 * numpy.pi * sector / 2 offset += self.sector[1][idx] * 2 * numpy.pi cr.set_source_rgba (0, 0, 0, 1) cr.move_to (center_x, center_y) cr.arc (center_x, center_y, (self.size - 2) / 2., offset - half_angle, half_angle + offset) cr.line_to (center_x, center_y) cr.stroke_preserve () cr.set_source_rgba (0, 0, 0, 0.25) cr.fill () cr.restore ()
def draw_rect(self, g: Graphics, area: Bounds, radius: float) -> None: (x, y, w, h) = area.tuple if w == 0 or h == 0: return degrees = pi / 180.0 g.new_sub_path() g.arc(x + w - radius, y + radius, radius, -90 * degrees, 0) g.arc(x + w - radius, y + h - radius, radius, 0, 90 * degrees) g.arc(x + radius, y + h - radius, radius, 90 * degrees, 180 * degrees) g.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees) g.close_path()
def draw_point(ctx: Context, point: Point, size): ctx.arc(point.x, point.y, size / 2, 0, 2 * 3.1416) ctx.fill()
def draw_circle(ctx: Context, point: Point, size): ctx.arc(point.x, point.y, size / 2, 0, 2 * 3.1416)
def _(d: Arc, ctx: cairo.Context, scale: float): ctx.save() ctx.arc(d.xc * scale, d.yc * scale, d.radius * scale, d.theta0, d.theta1) ctx.stroke() ctx.restore()
def __draw_ring (self: 'HueSatWheelWidget', cr: cairocffi.Context, width: int, height: int, center_x: float, center_y: float, outer: float, inner: float) -> None: self.__redraw = False stride = cairocffi.ImageSurface.format_stride_for_width (cairocffi.FORMAT_ARGB32, width) buf = numpy.empty (int (height * stride), dtype = numpy.uint8) for y in range (height): idx = y * width * 4 dy = -(y - center_y) for x in range (width): dx = x - center_x dist = dx * dx + dy * dy angle = math.atan2 (dy, dx) if angle < 0: angle += 2 * numpy.pi hue = angle / (2 * numpy.pi) hue_idx = int ((angle + 2 * numpy.pi / 3) / (2 * numpy.pi) * 255) hue_idx = hue_idx % 256 if dist < ((inner - 1) ** 2) * (1 - self.__hist[255 - hue_idx]) or \ dist > ((outer + 1) ** 2): buf[idx + 0] = 0 buf[idx + 1] = 0 buf[idx + 2] = 0 buf[idx + 3] = 0 idx += 4 continue r, g, b = colorsys.hsv_to_rgb (hue, 1.0, 1.0) a = 255 buf[idx + 0] = int (math.floor (r * 255 + 0.5)) buf[idx + 1] = int (math.floor (g * 255 + 0.5)) buf[idx + 2] = int (math.floor (b * 255 + 0.5)) buf[idx + 3] = a idx += 4 source = cairocffi.ImageSurface.create_for_data ( memoryview (buf), cairocffi.FORMAT_ARGB32, width, height, stride ) fg_color = self.get_style_context ().get_color (Gtk.StateFlags.NORMAL) cr.save () cr.set_source_rgba (0, 0, 0, 0) cr.paint () cr.set_source_surface (source, 0, 0) cr.paint () cr.set_line_width (1) cr.new_path () cr.set_source_rgba (*list (fg_color)) cr.arc (center_x, center_y, (self.size - 4) / 2. - self.ring_width, 0, 2 * numpy.pi) cr.stroke () cr.arc (center_x, center_y, (self.size - 2) / 2, 0, 2 * numpy.pi) cr.stroke () cr.arc (center_x, center_y, 5, 0, 2 * numpy.pi) cr.fill () cr.restore ()