This repository was archived by the owner on Apr 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.py
More file actions
59 lines (50 loc) · 2.35 KB
/
color.py
File metadata and controls
59 lines (50 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class RGB:
"""As a string, an `RGB` value returns an ANSI color code.
`RGB` requires 4 arguments:
| Parameter | Description |
|----------------|-------------------------------------------------------------------------------------------------------------------|
| `red` | The red value of the RGB. |
| `green` | The green value of the RGB. |
| `blue` | The blue value of the RGB. |
| `isForeground` | Returns a foreground ANSI color code as a string if `True`. Else returns a background ANSI color code. (Optional) |
"""
def __init__(self,
red: int,
green: int,
blue: int,
isForeground: bool = True) -> None:
self.red = red
self.green = green
self.blue = blue
self.isForeground = isForeground
def __str__(self) -> str:
return f"\033[38;2;{self.red};{self.green};{self.blue}" if self.isForeground else f"\033[48;2;{self.red};{self.green};{self.blue}m"
class Foreground:
"""`Foreground` has 8 class variables: `BLACK`, `RED`, `GREEN`, `YELLOW`, `BLUE`, `MAGENTA`, `CYAN`, and `RESET`. They are assigned to their respective ANSI color codes. To use these color codes, simply concatenate them with a string like so:
```python
print(f"{Color.Foreground.RED}This text is red.{Color.Foreground.RESET}")
```
"""
BLACK = "\033[30m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
RESET = "\033[39m"
class Background:
"""`Background` has 9 class variables: `BLACK`, `RED`, `GREEN`, `YELLOW`, `BLUE`, `MAGENTA`, `CYAN`, and `RESET`. They are assigned to their respective ANSI color codes. To use these color codes, simply concatenate them with a string like so:
```python
print(f"{Color.Background.GREEN}This text is green.{Color.Background.RESET}")
```
"""
BLACK = "\033[40m"
RED = "\033[41m"
GREEN = "\033[42m"
YELLOW = "\033[103m"
BLUE = "\033[44m"
MAGENTA = "\033[45m"
CYAN = "\033[46m"
WHITE = "\033[47m"
RESET = "\033[49m"