浏览代码

[+] Decode RGB from hex

Azalea (on HyDEV-Daisy) 3 年之前
父节点
当前提交
bfa8540c79
共有 1 个文件被更改,包括 19 次插入0 次删除
  1. 19 0
      hyfetch/color_util.py

+ 19 - 0
hyfetch/color_util.py

@@ -5,3 +5,22 @@ class RGB(NamedTuple):
     r: int
     g: int
     b: int
+
+    @classmethod
+    def from_hex(cls, hex: str) -> "RGB":
+        """
+        Create color from hex code
+
+        >>> RGB.from_hex('#FFAAB7')
+        RGB(r=255, g=170, b=183)
+
+        :param hex: Hex color code
+        :return: RGB object
+        """
+        while hex.startswith('#'):
+            hex = hex[1:]
+
+        r = int(hex[0:2], 16)
+        g = int(hex[2:4], 16)
+        b = int(hex[4:6], 16)
+        return cls(r, g, b)