Skip to content

Commit

Permalink
fix: improper regex syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
trumully committed Apr 21, 2024
1 parent 15a4742 commit 2f4395d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 21 deletions.
1 change: 1 addition & 0 deletions pfp_generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def clean_color(color: str, seed: str) -> colors.RGB:
return colors.generate_random_color(seed)
if colors.is_valid_color_name(color):
return colors.str_to_rgb(color)
color = "#" + color if not color.startswith("#") else color
if colors.is_valid_hex(color):
return colors.hex_to_rgb(color)

Expand Down
24 changes: 4 additions & 20 deletions pfp_generator/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def hex_to_rgb(hex_color: str) -> RGB:
RGB: The RGB color.
"""
hex_color = hex_color.lstrip("#")
return RGB(*(int(hex_color[i : i + 2]) for i in range(0, len(hex_color), 2)))
return RGB(*(int(hex_color[i : i + 2], 16) for i in range(0, len(hex_color), 2)))


def is_valid_color_name(color: str) -> bool:
Expand All @@ -137,29 +137,13 @@ def is_valid_color_name(color: str) -> bool:
def is_valid_hex(color: str) -> bool:
"""Check if a string is a valid hex color.
from answer @ https://stackoverflow.com/a/53330328
^ // start of line
# // literal pound sign, followed by
(?: // either:
(?: // a non-capturing group of:
[\da-f]{3} // exactly 3 of: a single digit or a letter 'a'-'f'
){1,2} // repeated exactly 1 or 2 times
| // or:
(?: // a non-capturing group of:
[\da-f]{4} // exactly 4 of: a single digit or a letter 'a'-'f'
){1,2} // repeated exactly 1 or 2 times
)
$ // end of line
i // ignore case (let 'A'-'F' match 'a'-'f')
Uses regex to check if the string is a valid hex color.
Solution from https://stackoverflow.com/a/19282773
Args:
color (str): The color string to check.
Returns:
bool: True if the string is a valid hex color, False otherwise.
"""
return bool(
re.match(
r"/^#(?:(?:[\da-f]{3}){1,2}|(?:[\da-f]{4}){1,2})$/i", color, re.IGNORECASE
)
)
return bool(re.match(r"^#?(?:(?:[0-9a-fA-F]{2}){3}|(?:[0-9a-fA-F]){3})$", color))
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pfp-generator"
version = "0.2.1"
version = "0.2.2"
description = "Generate profile pictures."
authors = ["trumully <truman.mulholland@gmail.com>"]
readme = "README.md"
Expand Down

0 comments on commit 2f4395d

Please sign in to comment.