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