Skip to content

Commit 2bd4836

Browse files
committed
Handle Highlighter explicit pen colours
The explicit pen colours from the .rm file are now available in rmscene >0.8.0 output. Also handle more gracefully unknown pen colours to prevent future crashes.
1 parent ecf80ba commit 2bd4836

3 files changed

Lines changed: 46 additions & 32 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ readme = "README.md"
88

99
[tool.poetry.dependencies]
1010
python = "^3.10"
11-
rmscene = ">=0.6.0, <0.7.0"
11+
rmscene = ">=0.8.0, <0.9.0"
1212
click = "^8.0"
1313

1414
[tool.poetry.dev-dependencies]

src/rmc/exporters/svg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from rmscene import scene_items as si
1414
from rmscene.text import TextDocument
1515

16-
from .writing_tools import Pen
16+
from .writing_tools import Pen, lookup_pen_color
1717

1818
_logger = logging.getLogger(__name__)
1919

@@ -206,7 +206,8 @@ def draw_stroke(item: si.Line, output):
206206
f'color: {item.color.name} thickness_scale: {item.thickness_scale} -->\n')
207207

208208
# initiate the pen
209-
pen = Pen.create(item.tool.value, item.color.value, item.thickness_scale)
209+
pen_color = lookup_pen_color(item.color.value, item.color_rgba)
210+
pen = Pen.create(item.tool.value, pen_color, item.thickness_scale)
210211

211212
last_xpos = -1.
212213
last_ypos = -1.

src/rmc/exporters/writing_tools.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,30 @@ def clamp(value):
4242
return min(max(value, 0), 1)
4343

4444

45+
def lookup_pen_color(pen_color_value, explicit_color_rgba):
46+
if explicit_color_rgba is not None:
47+
return explicit_color_rgba
48+
if pen_color_value in RM_PALETTE:
49+
color = RM_PALETTE[pen_color_value]
50+
else:
51+
_logger.warning("Unknown pen color: %s", pen_color_value)
52+
color = RM_PALETTE[PenColor.BLACK]
53+
if len(color) == 3:
54+
return (*color, 255)
55+
else:
56+
return color
57+
58+
4559
class Pen:
46-
def __init__(self, name, base_width, base_color_id):
60+
def __init__(self, name, base_width, base_color):
4761
self.base_width = base_width
48-
self.base_color = RM_PALETTE[base_color_id]
62+
self.base_color = base_color # rgba
4963
self.name = name
5064
self.segment_length = 1000
5165
self.base_opacity = 1
5266
# initial stroke values
5367
self.stroke_linecap = "round"
5468
self.stroke_opacity = 1
55-
self.stroke_width = base_width
56-
self.stroke_color = base_color_id
5769

5870
# note that the units of the points have had their units converted
5971
# in scene_stream.py
@@ -75,7 +87,7 @@ def get_segment_width(self, speed, direction, width, pressure, last_width):
7587
return self.base_width
7688

7789
def get_segment_color(self, speed, direction, width, pressure, last_width):
78-
return "rgb" + str(tuple(self.base_color))
90+
return "rgba" + str(tuple(self.base_color))
7991

8092
def get_segment_opacity(self, speed, direction, width, pressure, last_width):
8193
return self.base_opacity
@@ -122,13 +134,13 @@ def create(cls, pen_nr, color_id, width):
122134

123135

124136
class Fineliner(Pen):
125-
def __init__(self, base_width, base_color_id):
126-
super().__init__("Fineliner", base_width * 1.8, base_color_id)
137+
def __init__(self, base_width, base_color):
138+
super().__init__("Fineliner", base_width * 1.8, base_color)
127139

128140

129141
class Ballpoint(Pen):
130-
def __init__(self, base_width, base_color_id):
131-
super().__init__("Ballpoint", base_width, base_color_id)
142+
def __init__(self, base_width, base_color):
143+
super().__init__("Ballpoint", base_width, base_color)
132144
self.segment_length = 5
133145

134146
def get_segment_width(self, speed, direction, width, pressure, last_width):
@@ -151,8 +163,8 @@ def get_segment_color(self, speed, direction, width, pressure, last_width):
151163

152164

153165
class Marker(Pen):
154-
def __init__(self, base_width, base_color_id):
155-
super().__init__("Marker", base_width, base_color_id)
166+
def __init__(self, base_width, base_color):
167+
super().__init__("Marker", base_width, base_color)
156168
self.segment_length = 3
157169

158170
def get_segment_width(self, speed, direction, width, pressure, last_width):
@@ -161,8 +173,8 @@ def get_segment_width(self, speed, direction, width, pressure, last_width):
161173

162174

163175
class Pencil(Pen):
164-
def __init__(self, base_width, base_color_id):
165-
super().__init__("Pencil", base_width, base_color_id)
176+
def __init__(self, base_width, base_color):
177+
super().__init__("Pencil", base_width, base_color)
166178
self.segment_length = 2
167179

168180
def get_segment_width(self, speed, direction, width, pressure, last_width):
@@ -180,14 +192,14 @@ def get_segment_opacity(self, speed, direction, width, pressure, last_width):
180192

181193

182194
class MechanicalPencil(Pen):
183-
def __init__(self, base_width, base_color_id):
184-
super().__init__("Mechanical Pencil", base_width ** 2, base_color_id)
195+
def __init__(self, base_width, base_color):
196+
super().__init__("Mechanical Pencil", base_width ** 2, base_color)
185197
self.base_opacity = 0.7
186198

187199

188200
class Brush(Pen):
189-
def __init__(self, base_width, base_color_id):
190-
super().__init__("Brush", base_width, base_color_id)
201+
def __init__(self, base_width, base_color):
202+
super().__init__("Brush", base_width, base_color)
191203
self.segment_length = 2
192204
self.stroke_linecap = "round"
193205
self.opacity = 1
@@ -205,44 +217,45 @@ def get_segment_color(self, speed, direction, width, pressure, last_width):
205217
rev_intensity = abs(intensity - 1)
206218
segment_color = [int(rev_intensity * (255 - self.base_color[0])),
207219
int(rev_intensity * (255 - self.base_color[1])),
208-
int(rev_intensity * (255 - self.base_color[2]))]
220+
int(rev_intensity * (255 - self.base_color[2])),
221+
self.base_color[3]]
209222

210-
return "rgb" + str(tuple(segment_color))
223+
return "rgba" + str(tuple(segment_color))
211224

212225

213226
class Highlighter(Pen):
214-
def __init__(self, base_width, base_color_id):
215-
super().__init__("Highlighter", base_width, base_color_id)
227+
def __init__(self, base_width, base_color):
228+
super().__init__("Highlighter", base_width, base_color)
216229
self.stroke_linecap = "square"
217230
self.base_opacity = 0.3
218231
self.stroke_opacity = 0.2
219232

220233

221234
class Shader(Pen):
222235

223-
def __init__(self, base_width, base_color_id):
224-
super().__init__("Shader", base_width, base_color_id)
236+
def __init__(self, base_width, base_color):
237+
super().__init__("Shader", base_width, base_color)
225238
self.stroke_linecap = "round"
226239
self.base_opacity = 0.1
227240
# self.stroke_opacity = 0.2
228241
self.name = "Shader"
229242

230243
class Eraser(Pen):
231-
def __init__(self, base_width, base_color_id):
232-
super().__init__("Eraser", base_width * 2, base_color_id)
244+
def __init__(self, base_width, base_color):
245+
super().__init__("Eraser", base_width * 2, base_color)
233246
self.stroke_linecap = "square"
234247

235248

236249
class EraseArea(Pen):
237-
def __init__(self, base_width, base_color_id):
238-
super().__init__("Erase Area", base_width, base_color_id)
250+
def __init__(self, base_width, base_color):
251+
super().__init__("Erase Area", base_width, base_color)
239252
self.stroke_linecap = "square"
240253
self.base_opacity = 0
241254

242255

243256
class Calligraphy(Pen):
244-
def __init__(self, base_width, base_color_id):
245-
super().__init__("Calligraphy", base_width, base_color_id)
257+
def __init__(self, base_width, base_color):
258+
super().__init__("Calligraphy", base_width, base_color)
246259
self.segment_length = 2
247260

248261
def get_segment_width(self, speed, direction, width, pressure, last_width):

0 commit comments

Comments
 (0)