Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 11f7c2777d20b48c941a906f7c1d33c7b7ab1929 @dhotson committed Jun 2, 2012
Showing with 61 additions and 0 deletions.
  1. +10 −0 README.md
  2. +48 −0 colordb.py
  3. +3 −0 requirements.txt
@@ -0,0 +1,10 @@
+Color DB
+====
+
+Setup
+----
+
+
+ sudo aptitude install build-essential python-dev libspatialindex-dev
+ pip install -r requirements.txt
+
@@ -0,0 +1,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)
@@ -0,0 +1,3 @@
+Flask
+Rtree
+colormath

0 comments on commit 11f7c27

Please sign in to comment.