Skip to content

Commit d9dc834

Browse files
committed
"color" field
1 parent 609f90c commit d9dc834

File tree

8 files changed

+38
-4
lines changed

8 files changed

+38
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ v0.3.0 (in development)
33
- `--font` now accepts names of builtin fonts
44
- When an input filename is given but no output filename, write to the input
55
filename with the extension set to `.pdf` instead of to stdout
6+
- Events can now have a "color" field to set the background color
67

78
v0.2.0 (2019-04-16)
89
-------------------

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ represents a single weekly event and must contain the following keys:
110110
are specified in 24-hour format, the minutes being optional (and optionally
111111
separated from the hour by a colon or period).
112112

113+
``color``
114+
*(optional)* The background color of the event's box, given as six
115+
hexadecimal digits. The default background color is either grey or, if
116+
``--color`` is in effect, taken from a small palette of basic colors based
117+
on the event's index.
118+
113119

114120
Example
115121
=======
@@ -119,6 +125,7 @@ The following input file::
119125
- name: Garfield impersonation
120126
days: M
121127
time: 7-9
128+
color: "FFB04E"
122129

123130
- name: Work to live
124131
days: MTWRF
@@ -129,18 +136,22 @@ The following input file::
129136
(The one on Main Street)
130137
days: M, W, F
131138
time: 17:00 - 18:00
139+
color: "29FF65"
132140

133141
- name: Have they brought back my favorite show yet?
134142
days: R
135143
time: 19-19.30
144+
color: "FF84DF"
136145

137146
- name: Poor decisions
138147
days: F
139148
time: 22-23.59
149+
color: "000000"
140150

141151
- name: Sleep in
142152
days: SatSun
143153
time: 7-12
154+
color: "4226C4"
144155

145156
produces (using the default options) an output file that looks like this:
146157

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
- Handle events that go past midnight
33

44
- Input format:
5-
- Support an optional "color" field in event input dicts
65
- Change the input format to a YAML dict of which the list of event dicts
76
is one field, the others being font, font size, min & max times, etc.?
87
- Add support for 12-hour times

examples/example01.pdf

93 Bytes
Binary file not shown.

examples/example01.png

177 Bytes
Loading

examples/example01.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- name: Garfield impersonation
22
days: M
33
time: 7-9
4+
color: "FFB04E"
45

56
- name: Work to live
67
days: MTWRF
@@ -11,15 +12,19 @@
1112
(The one on Main Street)
1213
days: M, W, F
1314
time: 17:00 - 18:00
15+
color: "29FF65"
1416

1517
- name: Have they brought back my favorite show yet?
1618
days: R
1719
time: 19-19.30
20+
color: "FF84DF"
1821

1922
- name: Poor decisions
2023
days: F
2124
time: 22-23.59
25+
color: "000000"
2226

2327
- name: Sleep in
2428
days: SatSun
2529
time: 7-12
30+
color: "4226C4"

pdfschedule.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def render(self, canvas, width, height, x, y, font_size, show_times=True,
185185
canvas.rect(*ebox.rect(), stroke=1, fill=1)
186186
canvas.setFillColorRGB(0,0,0)
187187

188+
if ev.color[1] <= 0.33333:
189+
# Background color is too dark; print text in white
190+
canvas.setFillColorRGB(1,1,1)
191+
188192
# Event text:
189193
### TODO: Use PLATYPUS or whatever for this part:
190194
text = sum((wrap(t, line_width) for t in ev.text), [])
@@ -332,14 +336,28 @@ def read_events(infile, colors):
332336
m = re.match(r'^\s*(\d{1,2})(?:[:.]?(\d{2}))?\s*'
333337
r'-\s*(\d{1,2})(?:[:.]?(\d{2}))?\s*$', timestr)
334338
if not m:
335-
raise click.UsageError('Invalid time: ' + repr(timestr), err=True)
339+
raise click.UsageError('Invalid time: ' + repr(timestr))
336340
start = time(int(m.group(1)), int(m.group(2) or 0))
337341
end = time(int(m.group(3)), int(m.group(4) or 0))
342+
if "color" in entry:
343+
m = re.fullmatch(
344+
r'\s*#?\s*([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})\s*',
345+
entry["color"],
346+
)
347+
if not m:
348+
raise click.UsageError('Invalid color: ' + repr(entry["color"]))
349+
color = (
350+
int(m.group(1), 16) / 255,
351+
int(m.group(2), 16) / 255,
352+
int(m.group(3), 16) / 255,
353+
)
354+
else:
355+
color = colors[i % len(colors)]
338356
yield Event(
339357
start_time = start,
340358
end_time = end,
341359
text = text,
342-
color = colors[i % len(colors)],
360+
color = color,
343361
days = [d for d,rgx in DAY_REGEXES if re.search(rgx, days)],
344362
)
345363

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ project_urls =
4343
py_modules = pdfschedule
4444
python_requires = ~=3.4
4545
install_requires =
46-
attrs ~= 18.2
46+
attrs >= 18.2
4747
click ~= 7.0
4848
PyYAML == 5.*
4949
reportlab ~= 3.4

0 commit comments

Comments
 (0)