Philipp Hörist pushed to branch master at gajim / python-nbxmpp


Commits:
6eb48cbf by mjk at 2022-12-25T10:17:53+00:00
change: XEP-0392: Update to 0.8.0

* Remove background adaptation
* Allow users to specify saturation and lightness
* Provide more granular access to separate algorithms
* Add type information to the interface
* Add docs with links to precise XEP versions

- - - - -


2 changed files:

- nbxmpp/util.py
- python-nbxmpp.doap


Changes:

=====================================
nbxmpp/util.py
=====================================
@@ -53,7 +53,7 @@ from nbxmpp.structs import CommonError
 from nbxmpp.structs import HTTPUploadError
 from nbxmpp.structs import StanzaMalformedError
 from nbxmpp.modules.dataforms import extend_form
-from nbxmpp.third_party.hsluv import hsluv_to_rgb
+from nbxmpp.third_party import hsluv
 
 log = logging.getLogger('nbxmpp.util')
 
@@ -136,26 +136,38 @@ def clip_rgb(red, green, blue):
     )
 
 
-@lru_cache(maxsize=1024)
-def text_to_color(text, background_color):
-    # background color = (rb, gb, bb)
+def text_to_hue(text: str) -> float:
     hash_ = hashlib.sha1()
     hash_.update(text.encode())
-    hue = int.from_bytes(hash_.digest()[:2], 'little') / 65536
+    return int.from_bytes(hash_.digest()[:2], 'little') / 65536 * 360
 
-    red, green, blue = clip_rgb(*hsluv_to_rgb((hue * 360, 100, 50)))
 
-    rb, gb, bb = background_color
+def hsluv_to_rgb(hue: float,
+                 saturation: float,
+                 lightness: float,
+                 ) -> tuple[float, float, float]:
+    return clip_rgb(*hsluv.hsluv_to_rgb((hue, saturation, lightness)))
 
-    rb_inv = 1 - rb
-    gb_inv = 1 - gb
-    bb_inv = 1 - bb
 
-    rc = 0.2 * rb_inv + 0.8 * red
-    gc = 0.2 * gb_inv + 0.8 * green
-    bc = 0.2 * bb_inv + 0.8 * blue
+@lru_cache(maxsize=1024)
+def text_to_color(text: str,
+                  saturation: float,
+                  lightness: float,
+                  ) -> tuple[float, float, float]:
+    '''
+    Combines algorithms from XEP-0392 (version 0.8.0
+    <https://xmpp.org/extensions/attic/xep-0392-0.8.0.html>):
+        5.1 Angle generation,
+        5.4 RGB generation.
 
-    return rc, gc, bc
+    Input:
+        text: the subject to compute hue for,
+        saturation ∈ [0, 100],
+        lightness ∈ [0, 100].
+
+    Returns color in sRGB color space: r, g, b ∈ [0, 1].
+    '''
+    return hsluv_to_rgb(text_to_hue(text), saturation, lightness)
 
 
 def compute_caps_hash(info, compare=True):


=====================================
python-nbxmpp.doap
=====================================
@@ -391,7 +391,7 @@
       <xmpp:SupportedXep>
         <xmpp:xep rdf:resource="https://xmpp.org/extensions/xep-0392.html"/>
         <xmpp:status>complete</xmpp:status>
-        <xmpp:version>0.6.0</xmpp:version>
+        <xmpp:version>0.8.0</xmpp:version>
       </xmpp:SupportedXep>
     </implements>
     <implements>



View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/commit/6eb48cbfbe8eb66311e00b77c738b6b7f773583a

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/commit/6eb48cbfbe8eb66311e00b77c738b6b7f773583a
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
Commits@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to