Skip to content

Commit

Permalink
Added option to include a the pixel count of a color
Browse files Browse the repository at this point in the history
  • Loading branch information
Blanen committed Jul 14, 2017
1 parent 18a099b commit 85acbf8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions colorthief.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_color(self, quality=10):
palette = self.get_palette(5, quality)
return palette[0]

def get_palette(self, color_count=10, quality=10):
def get_palette(self, color_count=10, quality=10, count=False):
"""Build a color palette. We are using the median cut algorithm to
cluster similar colors.
Expand All @@ -74,7 +74,7 @@ def get_palette(self, color_count=10, quality=10):

# Send array to quantize function which clusters values
# using median cut algorithm
cmap = MMCQ.quantize(valid_pixels, color_count)
cmap = MMCQ.quantize(valid_pixels, color_count, count)
return cmap.palette


Expand Down Expand Up @@ -206,7 +206,7 @@ def median_cut_apply(histo, vbox):
return (None, None)

@staticmethod
def quantize(pixels, max_color):
def quantize(pixels, max_color, count):
"""Quantize.
:param pixels: a list of pixel in the form (r, g, b)
Expand Down Expand Up @@ -266,7 +266,7 @@ def iter_(lh, target):
iter_(pq2, max_color - pq2.size())

# calculate the actual colors
cmap = CMap()
cmap = CMap(count=count)
while pq2.size():
cmap.push(pq2.pop())
return cmap
Expand Down Expand Up @@ -349,11 +349,15 @@ def count(self):

class CMap(object):
"""Color map"""
def __init__(self):
def __init__(self, count):
self.vboxes = PQueue(lambda x: x['vbox'].count * x['vbox'].volume)
self.count = count

@property
def palette(self):
if self.count:
return self.vboxes.map(
lambda x: tuple(list(x['color']) + [x['vbox'].count]))
return self.vboxes.map(lambda x: x['color'])

def push(self, vbox):
Expand Down

0 comments on commit 85acbf8

Please sign in to comment.