from PIL import Image
import os

path = os.path.join(os.path.dirname(__file__), '..', 'img', 'varaha-industrial-park-logo.png')
img = Image.open(path).convert('RGBA')
# Resize to speed up
img_small = img.resize((200, 200))
# Convert to palette with adaptive colors
pal = img_small.convert('P', palette=Image.ADAPTIVE, colors=6)
palette = pal.getpalette()
color_counts = sorted(pal.getcolors(), reverse=True)

colors = []
for count, idx in color_counts[:6]:
    r = palette[idx*3]
    g = palette[idx*3+1]
    b = palette[idx*3+2]
    colors.append((count, (r, g, b)))

# Helper to hex
def rgb_to_hex(rgb):
    return '#%02x%02x%02x' % rgb

print('Detected colors (count, rgb, hex):')
for c, rgb in colors:
    print(c, rgb, rgb_to_hex(rgb))

# Save a small swatch image
sw = Image.new('RGB', (len(colors)*60, 60))
for i, (_, rgb) in enumerate(colors):
    block = Image.new('RGB', (60, 60), rgb)
    sw.paste(block, (i*60, 0))
sw_path = os.path.join(os.path.dirname(__file__), '..', 'img', 'logo_color_swatches.png')
sw.save(sw_path)
print('\nSwatch saved to', sw_path)
