def _create_curve_arrow(scene: Drawing, start: tuple, point1: tuple, point2: tuple, end: tuple, color: tuple): """Create an curved path (with cubic Bezier curve) around the given points in a scene. The path starts at `start` and ends at `end`. Points control_point1 and c2 are used as bezier control points. Args: scene (Scene): The scene where the path should be created. start: The start point. point1: The first control point. point2: The second control point. end: The end point. color: The arrow's color. Return: The modified scene """ middle = (point1[0] + (point2[0] - point1[0]) // 2, point1[1]) scene.add( Path(d=['M', start, 'C', point1, point1, middle], stroke=rgb(*color), stroke_width=1, fill='none')) scene.add( Path(d=['M', middle, 'C', point2, point2, end], stroke=rgb(*color), stroke_width=1, fill='none'))
def _create_rect_arrow(scene: Drawing, start: tuple, point1: tuple, point2: tuple, end: tuple, color: tuple): """Create an rectangular path through the given points. The path starts at p1 the goes to point1, p2 and finally to end. Args: scene (Scene): The scene where the path should be created. start: The first point. point1: The second point. point2: The third point. end: The last point. color: The arrow's color. Returns: The modified scene """ scene.add( Line(start, point1, shape_rendering='inherit', stroke=rgb(*color), stroke_width=1)) scene.add( Line(point1, point2, shape_rendering='inherit', stroke=rgb(*color), stroke_width=1)) scene.add( Line(point2, end, shape_rendering='inherit', stroke=rgb(*color), stroke_width=1))
def draw_rectangle_around_text(scene: Drawing, origin: tuple, width: int, height: int, fill_color: tuple, line_color: tuple, line_width: int, rounded: int, text: str, font_size: int, font_family: str): scene.add( Rect(insert=origin, size=(width, height), shape_rendering='inherit', fill=rgb(*fill_color), stroke=rgb(*line_color), stroke_width=line_width, rx=rounded, ry=rounded)) # write label in the middle under labelx = origin[0] + width // 2 labely = origin[ 1] + height // 2 + 4 # TODO: Should be drawn in the vertical center, so + 4 not needed! scene.add( Text(text, insert=(labelx, labely), fill=rgb(*line_color), font_family=font_family, font_size=font_size, text_rendering='inherit', alignment_baseline='central', text_anchor='middle') ) # TODO: alignment_baseline should be hanging or baseline! return origin[0], origin[1], width, height
def draw_line(scene: Drawing, start: tuple, ctrl1: tuple, ctrl2: tuple, end: tuple, is_curved: bool, edge_color: tuple): if is_curved: # cubic Bezier curve scene.add( Path(d=['M', start, 'C', ctrl1, ctrl2, end], stroke=rgb(*edge_color), stroke_width=1, fill='none')) else: scene.add( Line(start, end, shape_rendering='inherit', stroke=rgb(*edge_color), stroke_width=1))
def draw_arrow_w_text_middle(self, scene: Drawing, start: tuple, point1: tuple, point2: tuple, end: tuple, height: int, arrowsize: int, is_curved: bool, text: str, font_size: int, font_family: str, over: bool, color: tuple): # Store the appropriate function ouside of the loop if is_curved: self._create_curve_arrow(scene, start, point1, point2, end, color) else: self._create_rect_arrow(scene, start, point1, point2, end, color) # Draw arrow x_coord = (end[0] - arrowsize, end[1] - arrowsize) z_coord = (end[0] + arrowsize, end[1] - arrowsize) y_coord = (end[0], end[1]) # Draw the arrow head scene.add( Line(x_coord, y_coord, shape_rendering='inherit', stroke=rgb(*color), stroke_width=1)) scene.add( Line(z_coord, y_coord, shape_rendering='inherit', stroke=rgb(*color), stroke_width=1)) direction = 1 if over: direction = -1 # Write label in the middle under labelx = min(start[0], point2[0]) + abs(start[0] - point2[0]) // 2 labely = height + direction * font_size # TODO: Should be font height! scene.add( Text(text, insert=(labelx, labely), fill=rgb(*color), font_family=font_family, font_size=font_size, text_rendering='inherit', alignment_baseline='central', text_anchor='middle') ) # TODO: alignment_baseline should be hanging or baseline!
def draw_text(self, scene: Drawing, origin: tuple, text: str, font_size: int, font_family: str, color: tuple = (0, 0, 0)): # TODO: Here was TextToken (must align to left) scene.add( Text(text, insert=origin, fill=rgb(*color), font_family=font_family, font_size=font_size, text_rendering='inherit')) return self.get_text_dims(text, font_size, font_family) # Should return bounding box
def test_rgb_percent(self): self.assertEqual(rgb(50, 50, 50, '%'), "rgb(50%,50%,50%)") self.assertEqual(rgb(mode='%'), "rgb(0%,0%,0%)") # value overflow self.assertEqual(rgb(101, -1, 101, '%'), "rgb(100%,0%,100%)")
def test_rgb_8bit(self): self.assertEqual(rgb(128, 128, 128), "rgb(128,128,128)") self.assertEqual(rgb(), "rgb(0,0,0)") # value overflow self.assertEqual(rgb(256, 256, 256), "rgb(0,0,0)") self.assertEqual(rgb(257, 257, 257), "rgb(1,1,1)")
offset4 += delta4 axis4.add_node(nd, offset4) if d >= bins[1] and d < bins[2]: offset3 += delta3 axis3.add_node(nd, offset3) if d >= bins[2] and d < bins[3]: offset2 += delta2 axis2.add_node(nd, offset2) degree = float(nx.degree(g, n)) / 3.0 nd.dwg = nd.dwg.rect(insert = (nd.x - (degree/2.0), nd.y - (degree/2.0)), size = (degree, degree), fill = rgb(a2_color.r*100, a2_color.g*100, a2_color.b*100, mode="%"), stroke_width = 0) a2_color = a2_color.lighter(amt=0.02) if d >= bins[3] and d < bins[4]: offset1 += delta1 axis1.add_node(nd, offset1) if random.choice([True, False]): nd.dwg = nd.dwg.circle(center = (nd.x, nd.y), r = float(nx.degree(g, n)) / 4.3, fill = rgb(a3_color.r*100, a3_color.g*100, a3_color.b*100, mode="%"), stroke = 'red', stroke_width = 1.6) else: