2fd444bad4e6241d23b1139657cf4757c98e516b
3 from math
import sin
, pi
6 from sqlalchemy
import Column
, ForeignKey
7 from sqlalchemy
.orm
import relation
8 from sqlalchemy
.types
import Integer
, Unicode
10 from spline
.model
.meta
import TableBase
12 class User(TableBase
):
13 __tablename__
= 'users'
14 id = Column(Integer
, primary_key
=True)
15 name
= Column(Unicode(length
=20), nullable
=False)
16 unique_identifier
= Column(Unicode(length
=32), nullable
=False)
18 def __init__(self
, *args
, **kwargs
):
19 # Generate a unique hash if one isn't provided (which it shouldn't be)
20 if 'unique_identifier' not in kwargs
:
21 ident
= u
''.join(random
.choice(u
'0123456789abcdef')
23 kwargs
['unique_identifier'] = ident
25 super(User
, self
).__init__(*args
, **kwargs
)
28 def unique_colors(self
):
29 """Returns a list of (width, '#rrggbb') tuples that semi-uniquely
32 width_blob
, colors_blob
= self
.unique_identifier
[0:8], \
33 self
.unique_identifier
[8:32]
37 width_hex
= width_blob
[i
*2:i
*2+2]
38 widths
.append(int(width_hex
, 16))
39 total_width
= sum(widths
)
44 raw_hue
= int(colors_blob
[i
*6:i
*6+2], 16) / 256.0
46 # Make adjacent hues relatively close together, to avoid green
47 # + purple sorts of clashes.
48 # Minimum distance is 0.1; maximum is 0.35. Leaves half the
49 # spectrum available for any given color.
50 # Change 0.0–0.1 to -0.35–-0.1, 0.1–0.35
51 hue_offset
= raw_hue
* 0.5 - 0.25
57 h
= last_hue
+ raw_hue
62 l
= int(colors_blob
[i
*6+2:i
*6+4], 16) / 256.0
63 s
= int(colors_blob
[i
*6+4:i
*6+6], 16) / 256.0
65 # Secondary colors are extremely biased against when picking
66 # randomly from the hue spectrum.
67 # To alleviate this, try to bias hue towards secondary colors.
68 # This adjustment is based purely on experimentation; sin() works
69 # well because hue is periodic, * 6 means each period is 1/3 the
70 # hue spectrum, and the final / 24 is eyeballed
71 h
+= sin(h
* pi
* 6) / 24
73 # Cap lightness to 0.4 to 0.95, so it's not too close to white or
77 # Cap saturation to 0.5 to 1.0, so the color isn't too gray
80 r
, g
, b
= colorsys
.hls_to_rgb(h
, l
, s
)
81 color
= "#{0:02x}{1:02x}{2:02x}".format(
87 ret
.append((1.0 * widths
[i
] / total_width
, color
))
92 class OpenID(TableBase
):
93 __tablename__
= 'openid'
94 openid
= Column(Unicode(length
=255), primary_key
=True)
95 user_id
= Column(Integer
, ForeignKey('users.id'))
96 user
= relation(User
, lazy
=False, backref
='openids')