color_util.py 521 B

1234567891011121314151617181920212223242526
  1. from typing import NamedTuple
  2. class RGB(NamedTuple):
  3. r: int
  4. g: int
  5. b: int
  6. @classmethod
  7. def from_hex(cls, hex: str) -> "RGB":
  8. """
  9. Create color from hex code
  10. >>> RGB.from_hex('#FFAAB7')
  11. RGB(r=255, g=170, b=183)
  12. :param hex: Hex color code
  13. :return: RGB object
  14. """
  15. while hex.startswith('#'):
  16. hex = hex[1:]
  17. r = int(hex[0:2], 16)
  18. g = int(hex[2:4], 16)
  19. b = int(hex[4:6], 16)
  20. return cls(r, g, b)