def test__hash__(self): assert { Color(0, 100, 200, 250), Color(0, 100, 200, 250), Color(0, 0, 0, 0), } == {Color(0, 100, 200, 250), Color(0, 0, 0, 0)}
def setUp(self): neoscore.setup() self.pen = PenInterface( Color("#000000"), GraphicUnit(0), PenPattern.SOLID, PenJoinStyle.BEVEL, PenCapStyle.SQUARE, ) self.brush = BrushInterface(Color("#000000"), BrushPattern.SOLID)
def test_interface_generation(self): pen = Pen( Color("#eeddcc"), Unit(2), PenPattern.DASH, PenJoinStyle.MITER, PenCapStyle.ROUND, ) assert pen.interface == PenInterface( Color("#eeddcc"), Unit(2), PenPattern.DASH, PenJoinStyle.MITER, PenCapStyle.ROUND, )
def test_qt_object_creation(self): brush = BrushInterface(Color(0, 100, 200, 250), BrushPattern.SOLID) assert brush.qt_object.color().red() == 0 assert brush.qt_object.color().green() == 100 assert brush.qt_object.color().blue() == 200 assert brush.qt_object.color().alpha() == 250 assert brush.qt_object.style() == BrushPattern.SOLID.value
def __init__( self, color: ColorDef = Color("#000000"), thickness: Optional[Unit] = None, pattern: PenPattern = PenPattern.SOLID, join_style: PenJoinStyle = PenJoinStyle.BEVEL, cap_style: PenCapStyle = PenCapStyle.SQUARE, ): """ Args: color (Color or init tuple): The stroke color thickness (Unit): The stroke thickness. A value of `0` indicates Args cosmetic pixel width. Defaults to `constants.DEFAULT_PEN_THICKNESS`. pattern (PenPattern): The stroke pattern. Defaults to a solid line. join_style (PenJoinStyle): Defaults to a bevel join cap_style (PenCapStyle): Defaults to a square cap """ self._color = color_from_def(color) self._thickness = ( thickness if thickness is not None else GraphicUnit(constants.DEFAULT_PEN_THICKNESS) ) self._pattern = pattern self._join_style = join_style self._cap_style = cap_style self._regenerate_interface()
def __init__(self, pos, pen=None, brush=None): super().__init__() self._pos = pos if pen: self._pen = pen else: self._pen = PenInterface( Color("#000000"), GraphicUnit(0), PenPattern.SOLID, PenJoinStyle.BEVEL, PenCapStyle.SQUARE, ) if brush: self._brush = brush else: self._brush = BrushInterface(Color("#000000"), BrushPattern.SOLID)
def test_setters_update_interface(self): pen = Pen( Color("#eeddcc"), Unit(2), PenPattern.DASH, PenJoinStyle.MITER, PenCapStyle.ROUND, ) pen.color = Color("#ffffff") assert pen.interface.color == Color("#ffffff") pen.thickness = Unit(1) assert pen.interface.thickness == Unit(1) pen.pattern = PenPattern.SOLID assert pen.interface.pattern == PenPattern.SOLID pen.join_style = PenJoinStyle.MITER assert pen.interface.join_style == PenJoinStyle.MITER pen.cap_style = PenCapStyle.ROUND assert pen.interface.cap_style == PenCapStyle.ROUND
def q_color_to_color(q_color: QColor) -> Color: """Create a `Color` from a `QColor` Args: q_color (QColor): The source `QColor` Returns: Color """ return Color(q_color.red(), q_color.green(), q_color.blue(), q_color.alpha())
def __init__( self, color: ColorDef = Color("#000000"), pattern: BrushPattern = BrushPattern.SOLID, ): """ Args: color: The brush color pattern: The brush fill pattern. Defaults to a solid color. """ self._color = color_from_def(color) self._pattern = pattern self._regenerate_interface()
def test_from_existing(self): original = Pen( Color("#eeddcc"), Unit(2), PenPattern.DASH, PenJoinStyle.MITER, PenCapStyle.ROUND, ) clone = Pen.from_existing(original) assert id(original) != id(clone) assert original.color == clone.color assert original.thickness == clone.thickness assert original.pattern == clone.pattern assert original.join_style == clone.join_style assert original.cap_style == clone.cap_style
def test_color_is_immutable(self): color = Color("#ffffff") with pytest.raises(AttributeError): color.red = 123 with pytest.raises(AttributeError): color.green = 123 with pytest.raises(AttributeError): color.blue = 123 with pytest.raises(AttributeError): color.alpha = 123
def test_qt_object_creation(self): pen = PenInterface( Color(0, 100, 200, 250), Mm(1), PenPattern.SOLID, PenJoinStyle.BEVEL, PenCapStyle.SQUARE, ) assert pen.qt_object.color().red() == 0 assert pen.qt_object.color().green() == 100 assert pen.qt_object.color().blue() == 200 assert pen.qt_object.color().alpha() == 250 self.assertAlmostEqual(pen.qt_object.widthF(), Mm(1).base_value) assert pen.qt_object.style() == PenPattern.SOLID.value assert pen.qt_object.joinStyle() == PenJoinStyle.BEVEL.value assert pen.qt_object.capStyle() == PenCapStyle.SQUARE.value
def test_from_existing(self): original = Brush(Color("#ffffff"), BrushPattern.DENSE_1) clone = Brush.from_existing(original) assert id(original) != id(clone) assert original.color == clone.color assert original.pattern == clone.pattern
def test_color_from_def(self): assert color_from_def("#ffffff") == Color("#ffffff") assert color_from_def(Color("#ffffff")) == Color("#ffffff") assert color_from_def((255, 255, 255)) == Color("#ffffff")
def test_init_with_hex_string(self): color = Color("eeddcc") assert color.red == 238 assert color.green == 221 assert color.blue == 204 assert color.alpha == 255
def setUp(self): neoscore.setup() self.brush = BrushInterface(Color("#000000"), BrushPattern.SOLID)
def test__ne__(self): assert Color(1, 100, 200, 250) != Color(0, 100, 200, 250) assert Color(0, 101, 200, 250) != Color(0, 100, 200, 250) assert Color(0, 100, 201, 250) != Color(0, 100, 200, 250) assert Color(0, 100, 200, 251) != Color(0, 100, 200, 250) assert Color(0, 100, 200, 250) != "nonsense"
def test__eq__(self): assert Color(0, 100, 200, 250) == Color(0, 100, 200, 250)
def test__repr__(self): color = Color(0, 100, 200, 250) assert color.__repr__() == "Color(0, 100, 200, 250)"
def test_bad_alpha_color_value(self): with pytest.raises(ColorBoundsError): Color(0, 0, 0, 256) with pytest.raises(ColorBoundsError): Color(0, 0, 0, -1)
def test_bad_blue_color_value(self): with pytest.raises(ColorBoundsError): Color(0, 0, 256, 0) with pytest.raises(ColorBoundsError): Color(0, 0, -1, 0)
def test_bad_red_color_value(self): with pytest.raises(ColorBoundsError): Color(256, 0, 0, 0) with pytest.raises(ColorBoundsError): Color(-1, 0, 0, 0)
def test_init_with_rgba(self): color = Color(0, 100, 200, 250) assert color.red == 0 assert color.green == 100 assert color.blue == 200 assert color.alpha == 250
def test_setters_update_interface(self): brush = Brush(Color("#000000"), BrushPattern.DENSE_1) brush.color = Color("#ffffff") assert brush.interface.color == Color("#ffffff") brush.pattern = BrushPattern.SOLID assert brush.interface.pattern == BrushPattern.SOLID
def render_image( rect: RectDef, image_path: str, dpi: int = 600, quality: int = -1, bg_color: Optional[ColorDef] = None, autocrop: bool = False, ): """Render a section of the document to an image. The following file extensions are supported: * `.bmp` * `.jpg` * `.png` * `.pbm` * `.pgm` * `.ppm` * `.xbm` * `.xpm` Args: rect: The part of the document to render, in document coordinates. image_path: The path to the output image. This must be a valid path relative to the current working directory. dpi: The pixels per inch of the rendered image. quality: The quality of the output image for compressed image formats. Must be either `-1` (default compression) or between `0` (most compressed) and `100` (least compressed). bg_color: The background color for the image. Defaults to solid white. Use a Color with `alpha=0` for a fully transparent background. autocrop: Whether or not to crop the output image to tightly fit the contents of the frame. If true, the image will be cropped such that all 4 edges have at least one pixel not of `bg_color`. Raises: FileNotFoundError: If the given `image_path` does not point to a valid location for a new file. InvalidImageFormatError: If the given `image_path` does not have a supported image format file extension. ImageExportError: If low level Qt image export fails for unknown reasons. """ global document global _app_interface _clear_interfaces() if not ((0 <= quality <= 100) or quality == -1): warn("render_image quality {} invalid; using default.".format(quality)) quality = -1 if not file_system.is_valid_file_path(image_path): raise FileNotFoundError("Invalid image_path: " + image_path) if not os.path.splitext(image_path)[1] in images.supported_formats: raise InvalidImageFormatError( "image_path {} is not in a supported format.".format(image_path)) rect = rect_from_def(rect) if bg_color is None: bg_color = Color(255, 255, 255, 255) else: bg_color = color_from_def(bg_color) dpm = int(images.dpi_to_dpm(dpi)) document._render() _app_interface.render_image(rect, image_path, dpm, quality, bg_color, autocrop)
def test_init_with_hex_color(self): brush = Brush("#eeddcc") assert brush.color == Color(238, 221, 204, 255)
def test_init_with_hex_color(self): test_pen = Pen("#eeddcc") assert test_pen.color == Color(238, 221, 204, 255)
def test_interface_generation(self): brush = Brush(Color("#ffffff"), BrushPattern.DENSE_1) assert brush.interface == BrushInterface(Color("#ffffff"), BrushPattern.DENSE_1)
from neoscore.core.pen_pattern import PenPattern from neoscore.utils.color import Color from neoscore.utils.units import ZERO, Inch, Mm def _resolve_bool_env_variable(var): value = os.environ.get(var) return not (value is None or value == "0" or value.lower() == "false") # Directories BROWN_ROOT_DIR = os.path.join(os.path.dirname(__file__)) RESOURCES_DIR = os.path.join(BROWN_ROOT_DIR, "resources") # Default pen properties DEFAULT_PEN_COLOR = Color(0, 0, 0) DEFAULT_PEN_THICKNESS = ZERO DEFAULT_PEN_PATTERN = PenPattern.SOLID # Default brush properties DEFAULT_BRUSH_COLOR = Color(0, 0, 0, 255) DEFAULT_PATH_BRUSH_COLOR = Color(0, 0, 0, 0) DEFAULT_BRUSH_PATTERN = BrushPattern.SOLID # Visual gap between pages on the canvas # (This has no effect on exported documents) PAGE_DISPLAY_GAP = Mm(150) # Text Font DEFAULT_TEXT_FONT_NAME = "Lora" DEFAULT_TEXT_FONT_SIZE = Mm(2)