mirror of
https://codeberg.org/JasterV/Color-Plotter.git
synced 2026-04-26 18:10:05 +00:00
Implemented!!
This commit is contained in:
parent
a996cc619b
commit
2c521433d3
4 changed files with 90 additions and 48 deletions
84
src/closest_colors.py
Normal file
84
src/closest_colors.py
Normal file
|
|
@ -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()
|
||||||
|
|
||||||
|
|
@ -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)
|
|
||||||
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import math
|
import math
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
import operator
|
||||||
|
|
||||||
class Vector:
|
class Vector:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
@ -8,15 +9,15 @@ class Vector:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add(*vecs):
|
def add(*vecs):
|
||||||
return [i + j for i, j in zip(*vecs)]
|
return [sum(t) for t in zip(*vecs)]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def subtract(*vecs):
|
def subtract(*vecs):
|
||||||
return [i - j for i, j in zip(*vecs)]
|
return [reduce(operator.__sub__, t) for t in zip(*vecs)]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def mean(*vecs):
|
def mean(*vectors):
|
||||||
return [float(coord) / len(vecs) for coord in reduce(Vector.add, vecs)]
|
return [float(coord) / len(vectors) for coord in reduce(Vector.add, vectors)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import unittest
|
||||||
from src.vector_utils import Vector
|
from src.vector_utils import Vector
|
||||||
|
|
||||||
|
|
||||||
class TestStringMethods(unittest.TestCase):
|
class TestVector(unittest.TestCase):
|
||||||
|
|
||||||
def test_add(self):
|
def test_add(self):
|
||||||
v1 = [2, 3]
|
v1 = [2, 3]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue