Spaces:
Runtime error
Runtime error
import json | |
import numpy as np | |
def load_palettes(filepath): | |
with open(filepath, 'r') as file: | |
return json.load(file) | |
def hex_to_rgb(hex_color): | |
hex_color = hex_color.lstrip('#') | |
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) | |
def rgb_distance(color1, color2): | |
return np.sqrt(sum((a - b) ** 2 for a, b in zip(color1, color2))) | |
def predict_palette(collection_name, hex_color, filepath='output.json'): | |
palettes_data = load_palettes(filepath) | |
target_rgb = hex_to_rgb(hex_color) | |
min_distance = float('inf') | |
best_palette = None | |
for collection in palettes_data['collections']: | |
if collection['collection'] == collection_name: | |
for palette in collection['colors']: | |
for color in palette: | |
distance = rgb_distance(target_rgb, hex_to_rgb(color)) | |
if distance < min_distance: | |
min_distance = distance | |
best_palette = palette | |
return best_palette | |