MCPcopy Create free account
hub / github.com/PyQt5/PyQtClient / CMap

Class CMap

Utils/ColorThief.py:359–398  ·  view source on GitHub ↗

Color map

Source from the content-addressed store, hash-verified

357
358
359class CMap(object):
360 """Color map"""
361
362 def __init__(self):
363 self.vboxes = PQueue(lambda x: x['vbox'].count * x['vbox'].volume)
364
365 @property
366 def palette(self):
367 return self.vboxes.map(lambda x: x['color'])
368
369 def push(self, vbox):
370 self.vboxes.push({
371 'vbox': vbox,
372 'color': vbox.avg,
373 })
374
375 def size(self):
376 return self.vboxes.size()
377
378 def nearest(self, color):
379 d1 = None
380 p_color = None
381 for i in range(self.vboxes.size()):
382 vbox = self.vboxes.peek(i)
383 d2 = math.sqrt(
384 math.pow(color[0] - vbox['color'][0], 2) +
385 math.pow(color[1] - vbox['color'][1], 2) +
386 math.pow(color[2] - vbox['color'][2], 2)
387 )
388 if d1 is None or d2 < d1:
389 d1 = d2
390 p_color = vbox['color']
391 return p_color
392
393 def map(self, color):
394 for i in range(self.vboxes.size()):
395 vbox = self.vboxes.peek(i)
396 if vbox['vbox'].contains(color):
397 return vbox['color']
398 return self.nearest(color)
399
400
401class PQueue(object):

Callers 1

quantizeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected