From 09b9dd94e6591e0e74b8896c8cb8fdf173e32f5b Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 1 Jan 2018 15:26:18 -0500 Subject: [PATCH] adjust colors in the simulator to make them visible Colors too close to white are darkened just a bit to make them stand out against the white background. --- embroider_simulate.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/embroider_simulate.py b/embroider_simulate.py index 31d3cd752..f3f9e5e2d 100644 --- a/embroider_simulate.py +++ b/embroider_simulate.py @@ -4,6 +4,7 @@ import numpy import wx import inkex import simplestyle +import colorsys from embroider import patches_to_stitches, stitches_to_polylines @@ -102,6 +103,24 @@ class EmbroiderySimulator(wx.Frame): return string + def color_to_pen(self, color): + # python colorsys module uses floats from 0 to 1.0 + color = [value / 255.0 for value in color] + + hls = list(colorsys.rgb_to_hls(*color)) + + # Our background is white. If the color is too close to white, then + # it won't be visible. Capping lightness should make colors visible + # without changing them too much. + hls[1] = min(hls[1], 0.85) + + color = colorsys.hls_to_rgb(*hls) + + # convert back to values in the range of 0-255 + color = [value * 255 for value in color] + + return wx.Pen(color) + def _patches_to_segments(self, patches): stitches = patches_to_stitches(patches) @@ -117,7 +136,7 @@ class EmbroiderySimulator(wx.Frame): if stitch.color == last_color: segments.append(((last_pos, pos), pen)) else: - pen = wx.Pen(simplestyle.parseColor(stitch.color)) + pen = self.color_to_pen(simplestyle.parseColor(stitch.color)) last_pos = pos last_color = stitch.color @@ -132,7 +151,7 @@ class EmbroiderySimulator(wx.Frame): segments = [] pos = (0, 0) - color = wx.Brush('black') + pen = wx.Brush('black') cut = True with open(stitch_file_path) as stitch_file: @@ -144,7 +163,7 @@ class EmbroiderySimulator(wx.Frame): if symbol == "$": red, green, blue = fields[2:5] - color = wx.Pen((int(red), int(green), int(blue))) + color = color_to_pen((int(red), int(green), int(blue))) elif symbol == "*": if command == "COLOR": # change color @@ -157,7 +176,7 @@ class EmbroiderySimulator(wx.Frame): new_pos = (int(float(x) * 10), int(float(y) * 10)) if not cut: - segments.append(((pos, new_pos), color)) + segments.append(((pos, new_pos), pen)) cut = False pos = new_pos