Skip to content

Commit

Permalink
HEX check
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Jul 10, 2024
1 parent 1578870 commit 4372d0a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions termspark/helpers/hex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import re
from typing import Sequence, Union


class HEX:
@staticmethod
def check(color: Union[str, Sequence[str], None]) -> bool:
if color is None:
return False

assert type(color) == str
regex = r"^#(?:[0-9a-fA-F]{3}){1,2}$"
match = re.match(regex, color)

return match is not None
20 changes: 20 additions & 0 deletions tests/hex_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from termspark.helpers.hex import HEX


class TestHEX:
def test_check(self):
assert HEX.check("#000000")
assert HEX.check("#ffffff")
assert HEX.check("#FFFFFF")
assert HEX.check("#2472c8")
assert HEX.check("#2472C8")
assert HEX.check("#000")
assert HEX.check("#fff")
assert HEX.check("#FFF")
assert HEX.check("#ffg") == False
assert HEX.check("#FFG") == False
assert HEX.check("#FFFFFFF") == False
assert HEX.check("#FF") == False
assert HEX.check("#") == False
assert HEX.check(None) == False
assert HEX.check("") == False

0 comments on commit 4372d0a

Please sign in to comment.