-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3a58a9
commit f66b9cf
Showing
5 changed files
with
80 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import re | ||
from typing import Sequence, Union | ||
|
||
|
||
class RGB: | ||
@staticmethod | ||
def check(color: Union[str, Sequence[str]]) -> bool: | ||
if color is None: | ||
return False | ||
|
||
if type(color) == tuple: | ||
if len(color) != 3: | ||
return False | ||
|
||
color = RGB.to_str(color) | ||
|
||
assert type(color) == str | ||
regex = r"(\d+),\s*(\d+),\s*(\d+)" | ||
match = re.match(regex, color) | ||
|
||
if match is None: | ||
return False | ||
|
||
return all(0 <= int(group) <= 255 for group in match.groups()) | ||
|
||
@staticmethod | ||
def to_str(color: Union[str, tuple]) -> str: | ||
if type(color) == str: | ||
return color | ||
|
||
return ",".join([str(comp) for comp in color]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from termspark.helpers.rgb import RGB | ||
|
||
|
||
class TestRGB: | ||
def test_check(self): | ||
assert RGB.check("36,114,200") | ||
assert RGB.check((36, 114, 200)) | ||
assert RGB.check(None) == False | ||
assert RGB.check("") == False | ||
assert RGB.check((36, 114)) == False | ||
assert RGB.check((36, 114, 200, 114)) == False | ||
assert RGB.check((36, 114, 256)) == False | ||
assert RGB.check("36,114") == False | ||
|
||
def test_to_str(self): | ||
assert RGB.to_str("36,114,200") == "36,114,200" | ||
assert RGB.to_str((36, 114, 200)) == "36,114,200" |