| ... |
... |
@@ -53,7 +53,7 @@ from nbxmpp.structs import CommonError |
|
53
|
53
|
from nbxmpp.structs import HTTPUploadError
|
|
54
|
54
|
from nbxmpp.structs import StanzaMalformedError
|
|
55
|
55
|
from nbxmpp.modules.dataforms import extend_form
|
|
56
|
|
-from nbxmpp.third_party.hsluv import hsluv_to_rgb
|
|
|
56
|
+from nbxmpp.third_party import hsluv
|
|
57
|
57
|
|
|
58
|
58
|
log = logging.getLogger('nbxmpp.util')
|
|
59
|
59
|
|
| ... |
... |
@@ -136,26 +136,38 @@ def clip_rgb(red, green, blue): |
|
136
|
136
|
)
|
|
137
|
137
|
|
|
138
|
138
|
|
|
139
|
|
-@lru_cache(maxsize=1024)
|
|
140
|
|
-def text_to_color(text, background_color):
|
|
141
|
|
- # background color = (rb, gb, bb)
|
|
|
139
|
+def text_to_hue(text: str) -> float:
|
|
142
|
140
|
hash_ = hashlib.sha1()
|
|
143
|
141
|
hash_.update(text.encode())
|
|
144
|
|
- hue = int.from_bytes(hash_.digest()[:2], 'little') / 65536
|
|
|
142
|
+ return int.from_bytes(hash_.digest()[:2], 'little') / 65536 * 360
|
|
145
|
143
|
|
|
146
|
|
- red, green, blue = clip_rgb(*hsluv_to_rgb((hue * 360, 100, 50)))
|
|
147
|
144
|
|
|
148
|
|
- rb, gb, bb = background_color
|
|
|
145
|
+def hsluv_to_rgb(hue: float,
|
|
|
146
|
+ saturation: float,
|
|
|
147
|
+ lightness: float,
|
|
|
148
|
+ ) -> tuple[float, float, float]:
|
|
|
149
|
+ return clip_rgb(*hsluv.hsluv_to_rgb((hue, saturation, lightness)))
|
|
149
|
150
|
|
|
150
|
|
- rb_inv = 1 - rb
|
|
151
|
|
- gb_inv = 1 - gb
|
|
152
|
|
- bb_inv = 1 - bb
|
|
153
|
151
|
|
|
154
|
|
- rc = 0.2 * rb_inv + 0.8 * red
|
|
155
|
|
- gc = 0.2 * gb_inv + 0.8 * green
|
|
156
|
|
- bc = 0.2 * bb_inv + 0.8 * blue
|
|
|
152
|
+@lru_cache(maxsize=1024)
|
|
|
153
|
+def text_to_color(text: str,
|
|
|
154
|
+ saturation: float,
|
|
|
155
|
+ lightness: float,
|
|
|
156
|
+ ) -> tuple[float, float, float]:
|
|
|
157
|
+ '''
|
|
|
158
|
+ Combines algorithms from XEP-0392 (version 0.8.0
|
|
|
159
|
+ <https://xmpp.org/extensions/attic/xep-0392-0.8.0.html>):
|
|
|
160
|
+ 5.1 Angle generation,
|
|
|
161
|
+ 5.4 RGB generation.
|
|
157
|
162
|
|
|
158
|
|
- return rc, gc, bc
|
|
|
163
|
+ Input:
|
|
|
164
|
+ text: the subject to compute hue for,
|
|
|
165
|
+ saturation ∈ [0, 100],
|
|
|
166
|
+ lightness ∈ [0, 100].
|
|
|
167
|
+
|
|
|
168
|
+ Returns color in sRGB color space: r, g, b ∈ [0, 1].
|
|
|
169
|
+ '''
|
|
|
170
|
+ return hsluv_to_rgb(text_to_hue(text), saturation, lightness)
|
|
159
|
171
|
|
|
160
|
172
|
|
|
161
|
173
|
def compute_caps_hash(info, compare=True):
|