diff --git a/src/closest_colors.py b/src/closest_colors.py new file mode 100644 index 0000000..4b990b9 --- /dev/null +++ b/src/closest_colors.py @@ -0,0 +1,84 @@ +import requests +import json +from vector_utils import Vector as vec +from mpl_toolkits import mplot3d +import numpy as np +import matplotlib.pyplot as plt +import math + +def get_content(url): + response = requests.get(url) + content = response.content.decode() + return content + + +def get_json(url): + content = get_content(url) + data = json.loads(content) + return data + + +def hex_to_rgb(s): + s = s.lstrip('#') + return [int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16)] + + +def parse_colors(data_set): + return dict((item['color'], hex_to_rgb(item['hex'])) for item in data_set['colors']) + + +def calculate_size(dist): + pass + + +def closest(colors, to, n): + closest_colors = ((c, vec.distance(to, c)) for c in colors.values()) + return sorted(closest_colors, key=lambda c: c[1])[:n] + + +def plot_data(ax, values, colors, sizes): + xdata, ydata, zdata = zip(*values) + ax.scatter3D(xdata, ydata, zdata, c=colors / + 255.0, s=sizes, depthshade=False) + + +def plot_closest(ax, colors, to): + distances = closest(colors, to, n=len(colors)//2) + max_distance = max(distances, key=lambda x: x[1])[1] + sizes = np.array( + list(map(lambda x: (math.log2(max_distance) - math.log2(x[1] + 1))*100, distances))) + rgb_values = list(map(lambda x: x[0], distances)) + plot_data(ax, rgb_values, np.array(rgb_values), sizes) + + +def init_projection(size=(13, 10)): + plt.figure(figsize=size, dpi=90) + ax = plt.axes(projection='3d') + ax.set_axis_off() + return ax + + +def print_colors(colors): + print("\n-----COLOR LIST-----") + for color in colors.keys(): + print(color) + + +if __name__ == '__main__': + data_set_url = "https://raw.githubusercontent.com/dariusk/corpora/master/data/colors/xkcd.json" + data_set = get_json(data_set_url) + colors = parse_colors(data_set) + + print_colors(colors) + + color = input( + "\nEnter a color from the color list or just enter an hexadecimal value: ") + + if color in colors: + color = colors[color] + else: + color = hex_to_rgb(color) + ax = init_projection() + plot_closest(ax, colors, color) + plt.show() + diff --git a/src/colors_plot.py b/src/colors_plot.py deleted file mode 100644 index 3ad7d11..0000000 --- a/src/colors_plot.py +++ /dev/null @@ -1,43 +0,0 @@ -import requests -import json -from vector_utils import Vector as vec - -from mpl_toolkits import mplot3d -import numpy as np -import matplotlib.pyplot as plt - -def get_content(url): - response = requests.get(url) - content = response.content.decode() - return content - - -def get_json(url): - content = get_content(url) - data = json.loads(content) - return data - - -def hex_to_rgb(s): - s = s.lstrip('#') - return [int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16)] - - -def parse_colors(data_set): - return dict((item['color'], hex_to_rgb(item['hex'])) for item in data_set['colors']) - - -def closest(space, coord, n=10): - return sorted(space.keys(), key=lambda x: vec.distance(space[x], coord))[:n] - - -if __name__ == '__main__': - data_set_url = "https://raw.githubusercontent.com/dariusk/corpora/master/data/colors/xkcd.json" - data_set = get_json(data_set_url) - colors = parse_colors(data_set) - - fig = plt.figure() - ax = plt.axes(projection='3d') - - plt.show(fig) - diff --git a/src/vector_utils.py b/src/vector_utils.py index 0779f54..413f6f6 100644 --- a/src/vector_utils.py +++ b/src/vector_utils.py @@ -1,5 +1,6 @@ import math from functools import reduce +import operator class Vector: @staticmethod @@ -8,15 +9,15 @@ class Vector: @staticmethod def add(*vecs): - return [i + j for i, j in zip(*vecs)] + return [sum(t) for t in zip(*vecs)] @staticmethod def subtract(*vecs): - return [i - j for i, j in zip(*vecs)] + return [reduce(operator.__sub__, t) for t in zip(*vecs)] @staticmethod - def mean(*vecs): - return [float(coord) / len(vecs) for coord in reduce(Vector.add, vecs)] + def mean(*vectors): + return [float(coord) / len(vectors) for coord in reduce(Vector.add, vectors)] diff --git a/test/test_vector.py b/test/test_vector.py index 80cf348..7d0cb3f 100644 --- a/test/test_vector.py +++ b/test/test_vector.py @@ -2,7 +2,7 @@ import unittest from src.vector_utils import Vector -class TestStringMethods(unittest.TestCase): +class TestVector(unittest.TestCase): def test_add(self): v1 = [2, 3]