-
Notifications
You must be signed in to change notification settings - Fork 5
/
colordb.py
48 lines (35 loc) · 1.22 KB
/
colordb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask
from flask import request
from rtree import index
from colormath.color_objects import RGBColor
import json
p = index.Property()
p.dimension = 3
p.dat_extension = 'dat'
p.idx_extension = 'idx'
idx = index.Index('colordb', properties=p)
def hex_to_rgb(hex):
color = RGBColor()
color.set_from_rgb_hex(hex)
return color
def insert(color, entry):
lab = color.convert_to('lab')
idx.insert(entry, (lab.lab_l, lab.lab_a, lab.lab_b, lab.lab_l, lab.lab_a, lab.lab_b))
def nearest(color):
lab = color.convert_to('lab')
return idx.nearest((lab.lab_l, lab.lab_a, lab.lab_b, lab.lab_l, lab.lab_a, lab.lab_b), 50)
#test = {111: 'FF9900', 222: '0066FF', 333: '00FF00'}
#for e in test:
# insert(hex_to_rgb(test[e]), e)
app = Flask(__name__)
@app.route('/<hex_color>', methods=['GET'])
def get_entries(hex_color):
return json.dumps(list(nearest(hex_to_rgb(hex_color))))
@app.route('/entries', methods=['POST', 'GET'])
def insert_entry():
entryid = int(request.args.get('entryid'))
for color in request.args.get('colors','').split(','):
insert(hex_to_rgb(color), entryid)
return json.dumps('ok')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)