From 14ae1a679e341fa03d8b606feb4a65eb15527e35 Mon Sep 17 00:00:00 2001 From: Gordon Shumway <39967334+oddluck@users.noreply.github.com> Date: Thu, 25 Apr 2019 23:10:31 -0400 Subject: [PATCH] speed up color matching --- ASCII/plugin.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ASCII/plugin.py b/ASCII/plugin.py index d49d500..b50094e 100644 --- a/ASCII/plugin.py +++ b/ASCII/plugin.py @@ -42,6 +42,8 @@ class ASCII(callbacks.Plugin): self.__parent = super(ASCII, self) self.__parent.__init__(irc) self.colors = 83 + self.matches = {} + self.matches16 = {} self.ircColors= { (11.5497, 31.8768, 18.1739):16, (17.5866, 15.7066, 25.9892):17, @@ -202,18 +204,23 @@ class ASCII(callbacks.Plugin): """ Given PIL Image, return average RGB value """ + pixel = tuple(pixel) if self.colors == 16: speed = speed colors = list(self.colors16.keys()) - closest_colors = sorted(colors, key=lambda color: self.distance(self.rgb2lab(color), self.rgb2lab(pixel), speed)) - closest_color = closest_colors[0] - return self.colors16[closest_color] + if pixel not in self.matches16: + closest_colors = sorted(colors, key=lambda color: self.distance(self.rgb2lab(color), self.rgb2lab(pixel), speed)) + closest_color = closest_colors[0] + self.matches16[pixel] = self.colors16[closest_color] + return self.matches16[pixel] else: speed = speed colors = list(self.ircColors.keys()) - closest_colors = sorted(colors, key=lambda color: self.distance(color, self.rgb2lab(pixel), speed)) - closest_color = closest_colors[0] - return self.ircColors[closest_color] + if pixel not in self.matches: + closest_colors = sorted(colors, key=lambda color: self.distance(color, self.rgb2lab(pixel), speed)) + closest_color = closest_colors[0] + self.matches[pixel] = self.ircColors[closest_color] + return self.matches[pixel] def rgb2lab (self, inputColor) : num = 0