def __init__(self): self.ctm = Matrix.identity_matrix() self.text_matrix = Matrix.identity_matrix() self.text_line_matrix = Matrix.identity_matrix() self.text_rise = Decimal(0) self.character_spacing = Decimal(0) self.word_spacing = Decimal(0) self.horizontal_scaling = Decimal(100) self.leading = Decimal(0) self.font = None self.font_size = None self.path: typing.List["LineSegment"] = [] self.clipping_path: typing.List["LineSegment"] = [] self.non_stroke_color_space = None self.non_stroke_color = RGBColor(Decimal(0), Decimal(0), Decimal(0)) self.stroke_color_space = None self.stroke_color = RGBColor(Decimal(0), Decimal(0), Decimal(0)) self.line_width = Decimal(1) self.line_cap = None self.line_join = None self.miter_limit = Decimal(10) self.dash_pattern = None self.rendering_intent = None self.stroke_adjustment = None self.blend_mode = None self.soft_mask = None self.alpha_constant = None self.alpha_source = None
def get_rgb(self, x: int, y: int) -> RGBColor: c = self.scaled_image.getpixel((x, y)) if self.scaled_image.mode == "RGB": return RGBColor(r=Decimal(c[0]), g=Decimal(c[1]), b=Decimal(c[2])) if self.scaled_image.mode == "RGBA": return RGBColor(r=Decimal(c[0]), g=Decimal(c[1]), b=Decimal(c[2])) if self.scaled_image.mode == "CMYK": r = int((1 - c[0]) * (1 - c[3]) / 255) g = int((1 - c[1]) * (1 - c[3]) / 255) b = int((1 - c[2]) * (1 - c[0]) / 255) return RGBColor(r=Decimal(r), g=Decimal(g), b=Decimal(b)) return RGBColor(Decimal(0), Decimal(0), Decimal(0))
def _get_rgb_from_image(img: Image, x: int, y: int) -> RGBColor: c = img.getpixel((x, y)) if img.mode == "RGB": return RGBColor(r=Decimal(c[0]), g=Decimal(c[1]), b=Decimal(c[2])) if img.mode == "RGBA": return RGBColor(r=Decimal(c[0]), g=Decimal(c[1]), b=Decimal(c[2])) if img.mode == "CMYK": r = int((1 - c[0]) * (1 - c[3]) / 255) g = int((1 - c[1]) * (1 - c[3]) / 255) b = int((1 - c[2]) * (1 - c[0]) / 255) return RGBColor(r=Decimal(r), g=Decimal(g), b=Decimal(b)) return RGBColor(Decimal(0), Decimal(0), Decimal(0))
def __init__( self, text: str, font: typing.Union[Font, str] = "Courier", font_size: Decimal = Decimal(12), font_color: Color = RGBColor(Decimal(36), Decimal(41), Decimal(46)), horizontal_alignment: Alignment = Alignment.LEFT, vertical_alignment: Alignment = Alignment.TOP, border_top: bool = False, border_right: bool = False, border_bottom: bool = False, border_left: bool = False, border_color: Color = X11Color("Black"), border_width: Decimal = Decimal(1), padding_top: Decimal = Decimal(5), padding_right: Decimal = Decimal(5), padding_bottom: Decimal = Decimal(5), padding_left: Decimal = Decimal(5), background_color: typing.Optional[Color] = RGBColor( Decimal(246), Decimal(248), Decimal(250) ), parent: typing.Optional[LayoutElement] = None, ): # format string using black if able_to_import_black: text = black.format_str(text, mode=black.Mode()) # call super super().__init__( text=text, font=font, font_size=font_size, font_color=font_color, horizontal_alignment=horizontal_alignment, vertical_alignment=vertical_alignment, border_top=border_top, border_right=border_right, border_bottom=border_bottom, border_left=border_left, border_color=border_color, border_width=border_width, padding_top=padding_top, padding_right=padding_right, padding_bottom=padding_bottom, padding_left=padding_left, background_color=background_color, respect_newlines_in_text=True, respect_spaces_in_text=True, parent=parent, )
def _register_color(self, amount: int, color: RGBColor): mod_step = int(256 / (self.maximum_number_of_colors**(1.0 / 3))) r = int(color.to_rgb().red) r = r - r % mod_step g = int(color.to_rgb().green) g = g - g % mod_step b = int(color.to_rgb().blue) b = b - b % mod_step t = (r, g, b) if t not in self.colors_per_page[self.current_page]: self.colors_per_page[self.current_page][t] = amount else: self.colors_per_page[self.current_page][t] += amount
def invoke( self, canvas: "Canvas", operands: List[AnyPDFType] = [] ) -> None: # type: ignore [name-defined] non_stroke_color_space = self.canvas.graphics_state.non_stroke_color_space if non_stroke_color_space == "DeviceCMYK": assert isinstance(operands[0], Decimal) assert isinstance(operands[1], Decimal) assert isinstance(operands[2], Decimal) assert isinstance(operands[3], Decimal) canvas.graphics_state.stroke_color = CMYKColor( operands[0], operands[1], operands[2], operands[3], ) return if non_stroke_color_space == "DeviceGray": assert isinstance(operands[0], Decimal) canvas.graphics_state.stroke_color = GrayColor(operands[0]) return if non_stroke_color_space == "DeviceRGB": assert isinstance(operands[0], Decimal) assert isinstance(operands[1], Decimal) assert isinstance(operands[2], Decimal) canvas.graphics_state.stroke_color = RGBColor( operands[0], operands[1], operands[2], ) return
def invoke(self, canvas: "Canvas", operands: List[AnyPDFType]): assert isinstance(operands[0], Decimal) assert isinstance(operands[1], Decimal) assert isinstance(operands[2], Decimal) r = operands[0] g = operands[1] b = operands[2] canvas.graphics_state.stroke_color = RGBColor(r, g, b)
def invoke(self, canvas: "Canvas", operands: List[AnyPDFType] = []) -> None: # type: ignore [name-defined] assert isinstance(operands[0], Decimal) assert isinstance(operands[1], Decimal) assert isinstance(operands[2], Decimal) r = operands[0] g = operands[1] b = operands[2] canvas.graphics_state.stroke_color = RGBColor(r, g, b)
def test_hsv_to_rgb(self): # red TestColorConversion.assert_hsv_is_rgb( HSVColor(Decimal(0), Decimal(1), Decimal(1)), RGBColor(Decimal(255), Decimal(0), Decimal(0)), ) # green TestColorConversion.assert_hsv_is_rgb( HSVColor(Decimal(120.0 / 360.0), Decimal(1), Decimal(1)), RGBColor(Decimal(0), Decimal(255), Decimal(0)), ) # blue TestColorConversion.assert_hsv_is_rgb( HSVColor(Decimal(240.0 / 360.0), Decimal(1), Decimal(1)), RGBColor(Decimal(0), Decimal(0), Decimal(255)), )
def get_colors_per_page(self, page_number: int, limit: Optional[int] = None): if limit is None: limit = 32 tmp = sorted( [(RGBColor(Decimal(k[0]), Decimal(k[1]), Decimal(k[2])), int(v)) for k, v in self.colors_per_page[page_number].items()], key=lambda x: x[1], )[-limit:] return tmp
def get_colors_per_page(self, page_number: int, limit: Optional[int] = None ) -> typing.List[typing.Tuple[RGBColor, int]]: """ This function returns the colors used on a given page of a PDF """ if limit is None: limit = 32 tmp = sorted( [(RGBColor(Decimal(k[0]), Decimal(k[1]), Decimal(k[2])), int(v)) for k, v in self.colors_per_page[page_number].items()], key=lambda x: x[1], )[-limit:] return tmp