-
Notifications
You must be signed in to change notification settings - Fork 22
Allow alternate colors for the pens #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asciipip
wants to merge
10
commits into
rschroll:master
Choose a base branch
from
asciipip:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
656f2d4
Allow alternate colors for the pens
asciipip 49cd6c3
Use single quotes to avoid escaping internal double quotes
asciipip e1d1d32
Use the colour library for color management
asciipip 6dfd576
Use a namedtuple to carry the colors around internally
asciipip 28e3de8
Error handling for the pen colors
asciipip 7333eaf
Add gray parameter
asciipip bd4c52d
Add parameter for highlighter color
asciipip f523395
CLI docs about the color formats
asciipip 82d3271
Don't need in __main__ anymore
asciipip e55e72f
Don't need `Color` in __main__ anymore
asciipip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,25 +19,38 @@ | |
| from pathlib import Path | ||
| import json | ||
| import re | ||
| from collections import namedtuple | ||
|
|
||
| from colour import Color | ||
|
|
||
| from pdfrw import PdfReader, PdfWriter, PageMerge, PdfDict, PdfArray, PdfName, \ | ||
| IndirectPdfDict, uncompress, compress | ||
|
|
||
| from reportlab.pdfgen import canvas | ||
|
|
||
| from . import document, sources | ||
| from .constants import PDFHEIGHT, PDFWIDTH, PTPERPX, SPOOL_MAX | ||
| from .constants import PDFHEIGHT, PDFWIDTH, PTPERPX, SPOOL_MAX, HIGHLIGHT_DEFAULT_COLOR | ||
|
|
||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| Colors = namedtuple('Colors', ['black', 'white', 'gray', 'highlight']) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's call this |
||
|
|
||
| class InvalidColor(Exception): | ||
| """Raised when an invalid string is passed as a color.""" | ||
| pass | ||
|
|
||
|
|
||
| def render(source, *, | ||
| progress_cb=lambda x: None, | ||
| expand_pages=True, | ||
| template_alpha=0.3, | ||
| only_annotated=False): | ||
| """ | ||
| Render a source document as a PDF file. | ||
| only_annotated=False, | ||
| black='black', | ||
| white='white', | ||
| gray=None, | ||
| highlight=HIGHLIGHT_DEFAULT_COLOR): | ||
| """Render a source document as a PDF file. | ||
|
|
||
| source: The reMarkable document to be rendered. This may be | ||
| - A filename or pathlib.Path to a zip file containing the | ||
|
|
@@ -59,8 +72,19 @@ def render(source, *, | |
| makes the templates invisible, 1 makes them fully dark. | ||
| only_annotated: Boolean value (default False) indicating whether only | ||
| pages with annotations should be output. | ||
| black: A string giving the color to use as "black" in the document. | ||
| Can be a color name or a hex string. Default: 'black' | ||
| white: A string giving the color to use as "white" in the document. | ||
| See `black` parameter for format. Default: 'white' | ||
| gray: A string giving the color to use as "gray" in the document. | ||
| See `black` parameter for format. Default: None, which means to | ||
| pick an average between the "white" and "black" values. | ||
| highlight: A string giving the color to use for the highlighter. | ||
| See `black` parameter for format. | ||
| """ | ||
|
|
||
| colors = parse_colors(black, white, gray, highlight) | ||
|
|
||
| vector=True # TODO: Different rendering styles | ||
| source = sources.get_source(source) | ||
|
|
||
|
|
@@ -89,7 +113,7 @@ def render(source, *, | |
| changed_pages = [] | ||
| annotations = [] | ||
| for i in range(0, len(pages)): | ||
| page = document.DocumentPage(source, pages[i], i) | ||
| page = document.DocumentPage(source, pages[i], i, colors=colors) | ||
| if source.exists(page.rmpath): | ||
| changed_pages.append(i) | ||
| page.render_to_painter(pdf_canvas, vector, template_alpha) | ||
|
|
@@ -177,6 +201,35 @@ def render(source, *, | |
| return stream | ||
|
|
||
|
|
||
| def parse_colors(black, white, gray, highlight): | ||
| black_color = parse_color(black, 'black') | ||
| white_color = parse_color(white, 'white') | ||
| highlight_color = parse_color(highlight, 'highlight') | ||
|
|
||
| if gray is not None: | ||
| # Use the explicit gray value. | ||
| gray_color = parse_color(gray, 'gray') | ||
| elif black_color.saturation == 0 or white_color.saturation == 0: | ||
| # One or the other of the color endpoints is a shade of gray (or | ||
| # white or black). Use average in RGB space. This keeps the hue | ||
| # from the saturated endpoint and just lets the other endpoint | ||
| # either darken or lighten it. | ||
| gray_color = Color(rgb=((b + w) / 2 for b, w in zip(black_color.rgb, white_color.rgb))) | ||
| else: | ||
| # Both "black" and "white" have color elements to them. Use | ||
| # Color.range_to, which more or less averages in HSL space. | ||
| gray_color = list(black_color.range_to(white_color, 3))[1] | ||
|
|
||
| return Colors(black=black_color, white=white_color, gray=gray_color, highlight=highlight_color) | ||
|
|
||
|
|
||
| def parse_color(color_string, name): | ||
| try: | ||
| return Color(color_string) | ||
| except Exception as e: | ||
| raise InvalidColor('"{}" color was passed an invalid string: {}'.format(name, str(e))) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps |
||
|
|
||
|
|
||
| def do_apply_ocg(basepage, rmpage, i, uses_base_pdf, ocgprop, annotations): | ||
| ocgpage = IndirectPdfDict( | ||
| Type=PdfName('OCG'), | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Namedtuples allow position-based indexing, so as long as we get it in the right order, we could just pass
self.colors = colorshere, and then docolors[i].rgbin the pen itself.I'm not completely sold on this idea. I does decrease the amount of fussing needed here, but it also locks our color structure to the details of the lines files from remarkable. I could go either way here, so I'll let you make the decision.