mirror of
https://codeberg.org/JasterV/Color-Plotter.git
synced 2026-04-26 18:10:05 +00:00
second commit
This commit is contained in:
parent
657b0a67cb
commit
741349a669
3 changed files with 23 additions and 11 deletions
|
|
@ -1,22 +1,30 @@
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
from vector_utils import Vector
|
from vector_utils import Vector as vec
|
||||||
|
|
||||||
|
def get_content(url):
|
||||||
def hex_to_int(s):
|
response = requests.get(url)
|
||||||
s = s.lstrip('#')
|
content = response.content.decode()
|
||||||
return [int(s[:2], 16), int(s[2:4], 16), int(s[4:6], 16)]
|
return content
|
||||||
|
|
||||||
|
|
||||||
def get_json(url):
|
def get_json(url):
|
||||||
response = requests.get(url)
|
content = get_content(url)
|
||||||
content = response.content.decode()
|
|
||||||
data = json.loads(content)
|
data = json.loads(content)
|
||||||
return data
|
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):
|
def parse_colors(data_set):
|
||||||
return dict((item['color'], hex_to_int(item['hex'])) for item in data_set['colors'])
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
@ -24,3 +32,4 @@ if __name__ == '__main__':
|
||||||
data_set = get_json(data_set_url)
|
data_set = get_json(data_set_url)
|
||||||
colors = parse_colors(data_set)
|
colors = parse_colors(data_set)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,9 +11,12 @@ class Vector:
|
||||||
return [i + j for i, j in zip(*vecs)]
|
return [i + j for i, j in zip(*vecs)]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def subs(*vecs):
|
def subtract(*vecs):
|
||||||
return [i - j for i, j in zip(*vecs)]
|
return [i - j for i, j in zip(*vecs)]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def mean(*vecs):
|
def mean(*vecs):
|
||||||
return [float(coord) / len(vecs) for coord in reduce(Vector.add, vecs)]
|
return [float(coord) / len(vecs) for coord in reduce(Vector.add, vecs)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,11 @@ class TestStringMethods(unittest.TestCase):
|
||||||
def test_substract(self):
|
def test_substract(self):
|
||||||
v1 = [2, 3]
|
v1 = [2, 3]
|
||||||
v2 = [3, 4]
|
v2 = [3, 4]
|
||||||
self.assertEqual([-1, -1], Vector.subs(v1, v2))
|
self.assertEqual([-1, -1], Vector.subtract(v1, v2))
|
||||||
|
|
||||||
v1 = [5, 12, 32, 12]
|
v1 = [5, 12, 32, 12]
|
||||||
v2 = [34, 3, 4, 2]
|
v2 = [34, 3, 4, 2]
|
||||||
self.assertEqual([-29, 9, 28, 10], Vector.subs(v1, v2))
|
self.assertEqual([-29, 9, 28, 10], Vector.subtract(v1, v2))
|
||||||
|
|
||||||
def test_distance(self):
|
def test_distance(self):
|
||||||
v1 = [7, 4, 3]
|
v1 = [7, 4, 3]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue