4
4
import json
5
5
import re
6
6
from collections import defaultdict
7
- from random import Random
8
7
9
8
import conllu
10
9
from chardet import UniversalDetector
11
10
from django .db import transaction
12
11
from django .conf import settings
12
+ from colour import Color
13
13
import pyexcel
14
14
from rest_framework .renderers import JSONRenderer
15
15
from seqeval .metrics .sequence_labeling import get_entities
@@ -66,7 +66,7 @@ def exclude_created_labels(cls, labels, created):
66
66
return [label for label in labels if label not in created ]
67
67
68
68
@classmethod
69
- def to_serializer_format (cls , labels , created , random_seed = None ):
69
+ def to_serializer_format (cls , labels , created ):
70
70
existing_shortkeys = {(label .suffix_key , label .prefix_key )
71
71
for label in created .values ()}
72
72
@@ -81,9 +81,10 @@ def to_serializer_format(cls, labels, created, random_seed=None):
81
81
serializer_label ['prefix_key' ] = shortkey [1 ]
82
82
existing_shortkeys .add (shortkey )
83
83
84
- color = Color .random (seed = random_seed )
85
- serializer_label ['background_color' ] = color .hex
86
- serializer_label ['text_color' ] = color .contrast_color .hex
84
+ background_color = Color (pick_for = label )
85
+ text_color = Color ('white' ) if background_color .get_luminance () < 0.5 else Color ('black' )
86
+ serializer_label ['background_color' ] = background_color .hex
87
+ serializer_label ['text_color' ] = text_color .hex
87
88
88
89
serializer_labels .append (serializer_label )
89
90
@@ -450,47 +451,6 @@ def paint(self, documents):
450
451
return res
451
452
452
453
453
- class Color :
454
- def __init__ (self , red , green , blue ):
455
- self .red = red
456
- self .green = green
457
- self .blue = blue
458
-
459
- @property
460
- def contrast_color (self ):
461
- """Generate black or white color.
462
-
463
- Ensure that text and background color combinations provide
464
- sufficient contrast when viewed by someone having color deficits or
465
- when viewed on a black and white screen.
466
-
467
- Algorithm from w3c:
468
- * https://www.w3.org/TR/AERT/#color-contrast
469
- """
470
- return Color .white () if self .brightness < 128 else Color .black ()
471
-
472
- @property
473
- def brightness (self ):
474
- return ((self .red * 299 ) + (self .green * 587 ) + (self .blue * 114 )) / 1000
475
-
476
- @property
477
- def hex (self ):
478
- return '#{:02x}{:02x}{:02x}' .format (self .red , self .green , self .blue )
479
-
480
- @classmethod
481
- def white (cls ):
482
- return cls (red = 255 , green = 255 , blue = 255 )
483
-
484
- @classmethod
485
- def black (cls ):
486
- return cls (red = 0 , green = 0 , blue = 0 )
487
-
488
- @classmethod
489
- def random (cls , seed = None ):
490
- rgb = Random (seed ).choices (range (256 ), k = 3 )
491
- return cls (* rgb )
492
-
493
-
494
454
def iterable_to_io (iterable , buffer_size = io .DEFAULT_BUFFER_SIZE ):
495
455
"""See https://stackoverflow.com/a/20260030/3817588."""
496
456
class IterStream (io .RawIOBase ):
0 commit comments