speed up color matching

This commit is contained in:
Gordon Shumway 2019-04-25 23:10:31 -04:00 committed by GitHub
parent bc114af113
commit 14ae1a679e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 6 deletions

View File

@ -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