This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
426 lines (325 loc) · 11.4 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# -*- coding: utf-8 -*-
"""
Application
===========
"""
from __future__ import division, unicode_literals
import json
import os
from cachelib import SimpleCache
from flask import Flask, Response, render_template, request
from flask_caching import Cache
from flask_compress import Compress
from colour.utilities import domain_range_scale
from colour_analysis import (
COLOURSPACE_MODEL, IMAGE_COLOURSPACE, IMAGE_CCTF_DECODING,
PRIMARY_COLOURSPACE, RGB_colourspaces, RGB_colourspace_volume_visual,
RGB_image_scatter_visual, SECONDARY_COLOURSPACE, colourspace_models,
cctf_decodings, image_data, pointer_gamut_visual, spectral_locus_visual,
visible_spectrum_visual)
__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2018-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = '[email protected]'
__status__ = 'Production'
__application_name__ = 'Colour - Analysis'
__major_version__ = '0'
__minor_version__ = '1'
__change_version__ = '8'
__version__ = '.'.join(
(__major_version__,
__minor_version__,
__change_version__)) # yapf: disable
__all__ = [
'APP', 'CACHE', 'CACHE_DEFAULT_TIMEOUT', 'IMAGES_DIRECTORY',
'images_response', 'cctf_decodings_response',
'colourspace_models_response', 'RGB_colourspaces_response',
'image_data_response', 'RGB_colourspace_volume_visual_response',
'RGB_image_scatter_visual_response', 'spectral_locus_visual_response',
'pointer_gamut_visual_response', 'index', 'after_request'
]
APP = Flask(__name__)
"""
*Flask* app.
APP : Flask
"""
CACHE = Cache(config={'CACHE_TYPE': 'simple'})
"""
Global application responses cache.
CACHE : Cache
"""
CACHE_DEFAULT_TIMEOUT = 60 * 24 * 7
"""
Cache responses timeout.
CACHE_DEFAULT_TIMEOUT : int
"""
CACHE.init_app(APP)
APP.config.update(
COMPRESS_LEVEL=3,
COMPRESS_CACHE_KEY=lambda x: x.full_path,
COMPRESS_CACHE_BACKEND=lambda: SimpleCache(default_timeout= # noqa
CACHE_DEFAULT_TIMEOUT),
)
Compress(APP)
IMAGES_DIRECTORY = os.path.join(os.getcwd(), 'static', 'images')
"""
Images directory.
IMAGES_DIRECTORY : unicode
"""
def _null_to_None(data):
"""
Converts *Javascript* originated *null* and *undefined* strings
to `None`. Non-matching data will be passed untouched.
Parameters
----------
data : unicode
Data to convert.
Returns
-------
None or unicode
Converted data.
"""
if data in ('null', 'undefined'):
return None
else:
return data
def _bool_to_bool(data):
"""
Converts *Javascript* originated *true* and *false* strings
to `True` or `False`. Non-matching data will be passed untouched.
Parameters
----------
data : unicode
Data to convert.
Returns
-------
True or False or unicode
Converted data.
"""
if data == 'true' or data is True:
return True
elif data == 'false' or data is False:
return False
else:
return data
@APP.route('/images')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def images_response():
"""
Returns the images response.
Returns
-------
Response
Images response.
"""
json_data = json.dumps(sorted(os.listdir(IMAGES_DIRECTORY)))
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/decoding-cctfs')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def cctf_decodings_response():
"""
Returns the decoding colour component transfer functions response.
Returns
-------
Response
Decoding colour component transfer functions response.
"""
json_data = cctf_decodings()
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/colourspace-models')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def colourspace_models_response():
"""
Returns the colourspace models response.
Returns
-------
Response
Colourspace models response.
"""
json_data = colourspace_models()
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/RGB-colourspaces')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def RGB_colourspaces_response():
"""
Returns the RGB colourspaces response.
Returns
-------
Response
RGB colourspaces response.
"""
json_data = RGB_colourspaces()
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/image-data/<image>')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def image_data_response(image):
"""
Returns an image data response.
Returns
-------
Response
Image data response.
"""
path = os.path.join(os.getcwd(), 'static', 'images', image)
args = request.args
json_data = image_data(
path=path,
primary_colourspace=args.get('primaryColourspace',
PRIMARY_COLOURSPACE),
secondary_colourspace=args.get('secondaryColourspace',
SECONDARY_COLOURSPACE),
image_colourspace=args.get('imageColourspace', IMAGE_COLOURSPACE),
image_decoding_cctf=args.get('imageDecodingCctf', IMAGE_CCTF_DECODING),
out_of_primary_colourspace_gamut=_bool_to_bool(
args.get('outOfPrimaryColourspaceGamut', False)),
out_of_secondary_colourspace_gamut=_bool_to_bool(
args.get('outOfSecondaryColourspaceGamut', False)),
out_of_pointer_gamut=_bool_to_bool(
args.get('outOfPointerGamut', False)),
saturate=_bool_to_bool(args.get('saturate', False)))
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/RGB-colourspace-volume-visual')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def RGB_colourspace_volume_visual_response():
"""
Returns a RGB colourspace volume visual response.
Returns
-------
Response
RGB colourspace volume visual response.
"""
args = request.args
json_data = RGB_colourspace_volume_visual(
colourspace=args.get('colourspace', PRIMARY_COLOURSPACE),
colourspace_model=args.get('colourspaceModel', COLOURSPACE_MODEL),
segments=int(args.get('segments', 16)),
wireframe=_bool_to_bool(args.get('wireframe', False)),
)
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/RGB-image-scatter-visual/<image>')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def RGB_image_scatter_visual_response(image):
"""
Returns a RGB image scatter visual response.
Returns
-------
Response
RGB image scatter visual response.
"""
path = os.path.join(os.getcwd(), 'static', 'images', image)
args = request.args
json_data = RGB_image_scatter_visual(
path=path,
primary_colourspace=args.get('primaryColourspace',
PRIMARY_COLOURSPACE),
secondary_colourspace=args.get('secondaryColourspace',
SECONDARY_COLOURSPACE),
image_colourspace=args.get('imageColourspace', IMAGE_COLOURSPACE),
image_decoding_cctf=args.get('imageDecodingCctf', IMAGE_CCTF_DECODING),
colourspace_model=args.get('colourspaceModel', COLOURSPACE_MODEL),
out_of_primary_colourspace_gamut=_bool_to_bool(
args.get('outOfPrimaryColourspaceGamut', False)),
out_of_secondary_colourspace_gamut=_bool_to_bool(
args.get('outOfSecondaryColourspaceGamut', False)),
out_of_pointer_gamut=_bool_to_bool(
args.get('outOfPointerGamut', False)),
sub_sampling=int(args.get('subSampling', 25)),
saturate=_bool_to_bool(args.get('saturate', False)),
)
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/spectral-locus-visual')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def spectral_locus_visual_response():
"""
Returns a spectral locus visual response.
Returns
-------
Response
Spectral locus visual response.
"""
args = request.args
json_data = spectral_locus_visual(
colourspace=args.get('colourspace', PRIMARY_COLOURSPACE),
colourspace_model=args.get('colourspaceModel', COLOURSPACE_MODEL),
)
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/pointer-gamut-visual')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def pointer_gamut_visual_response():
"""
Returns a *Pointer's Gamut* visual response.
Returns
-------
Response
*Pointer's Gamut* visual response.
"""
args = request.args
json_data = pointer_gamut_visual(
colourspace_model=args.get('colourspaceModel', COLOURSPACE_MODEL), )
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/visible-spectrum-visual')
@CACHE.cached(timeout=CACHE_DEFAULT_TIMEOUT, query_string=True)
def visible_spectrum_visual_response():
"""
Returns the visible spectrum visual response.
Returns
-------
Response
visible spectrum visual response.
"""
args = request.args
json_data = visible_spectrum_visual(
colourspace_model=args.get('colourspaceModel', COLOURSPACE_MODEL), )
response = Response(json_data, status=200, mimetype='application/json')
response.headers['X-Content-Length'] = len(json_data)
return response
@APP.route('/')
def index():
"""
Returns the index response.
Returns
-------
Response
Index response.
"""
return render_template(
'index.html',
colour_analysis_js=os.environ.get('COLOUR_ANALYSIS_JS',
'/static/js/colour-analysis.js'),
image=sorted(os.listdir(IMAGES_DIRECTORY))[0],
primary_colourspace=PRIMARY_COLOURSPACE,
secondary_colourspace=SECONDARY_COLOURSPACE,
image_colourspace=IMAGE_COLOURSPACE,
image_decoding_cctf=IMAGE_CCTF_DECODING,
colourspace_model=COLOURSPACE_MODEL)
@APP.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers',
'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods',
'GET,PUT,POST,DELETE,OPTIONS')
response.headers.add('Access-Control-Expose-Headers', 'X-Content-Length')
return response
if __name__ == '__main__':
with domain_range_scale(1):
APP.run()