Skip to content

Commit f1abf23

Browse files
authored
Main File
1 parent 664820f commit f1abf23

1 file changed

Lines changed: 77 additions & 68 deletions

File tree

Rcolor/rcolor.py

Lines changed: 77 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
# ____________________________________________
55

66
# ======== # import built-in library # ======== #
7-
from random import choice
7+
from random import choice
88
from os import name
99
from json import load
1010

11-
# ======== # random foreground # ======== #
12-
def color(*user_input) -> str:
13-
'''Change foreground of output to 255 different colors, but it may not work in every terminal.'''
11+
# ======== # random foreground color# ======== #
12+
def foreground(*user_input) -> str:
13+
'''Change foreground color of output to 255 different colors, but it may not work in every terminal.'''
1414
try:
15-
# ====== # Opening json file that contain colors code in windows and linux # ====== #
15+
# ====== # Opening json file that contain colors code in windows and unix # ====== #
1616
if(name == 'nt'):
17-
json_paths: str = __file__.replace('rcolor.py', 'colors\\colors.json')
18-
else:
19-
json_paths: str = __file__.replace('rcolor.py', 'colors/colors.json')
20-
with open(json_paths , 'r') as f:
21-
colors: list[str] = load(f).get("colors")
17+
colors_paths: str = __file__.replace('rcolor.py', 'colors\\foreground.json')
18+
else:
19+
colors_paths: str = __file__.replace('rcolor.py', 'colors/foreground.json')
20+
with open(colors_paths , 'r') as f:
21+
foreground_colors: list[str] = load(f).get("colors")
2222
user_input = list(user_input)
23-
23+
2424
# ======== # make all item in user_input list to string # ======== #
2525
for i in user_input:
2626
if type(i) == str:
@@ -29,12 +29,12 @@ def color(*user_input) -> str:
2929
user_input[user_input.index(i)] = str(i)
3030

3131
user_input = ''.join(user_input)
32-
random_color: str = choice(colors)
33-
32+
random_color: str = choice(foreground_colors)
33+
3434
return random_color + user_input + '\033[0m'
3535
except FileNotFoundError:
3636
pass
37-
return('\33[31m' + '"colors.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
37+
return('\33[31m' + '"foreground.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
3838
except TypeError:
3939
pass
4040
return('\33[31m' + f'Input type {type(i)} is not supported' + '\033[0m')
@@ -43,13 +43,13 @@ def color(*user_input) -> str:
4343
def background(*user_input) -> str:
4444
'''Change background color of output to 255 different colors, but it may not work in every terminal.'''
4545
try:
46-
# ====== # Opening json file that contain colors code in windows and linux # ====== #
46+
# ====== # Opening json file that contain colors code in windows and unix # ====== #
4747
if name == 'nt':
48-
json_paths: str = __file__.replace('rcolor.py', 'colors\background_color.json')
49-
else:
50-
json_paths: str = __file__.replace('rcolor.py', 'colors/background_color.json')
51-
with open(json_paths, 'r') as f:
52-
background_color: list[str] = load(f).get("background_color")
48+
colors_paths: str = __file__.replace('rcolor.py', 'colors\background_colors.json')
49+
else:
50+
colors_paths: str = __file__.replace('rcolor.py', 'colors/background_colors.json')
51+
with open(colors_paths, 'r') as f:
52+
background_colors: list[str] = load(f).get("background_color")
5353
user_input = list(user_input)
5454

5555
# ======== # make all item in user_input list to string # ======== #
@@ -60,26 +60,26 @@ def background(*user_input) -> str:
6060
user_input[user_input.index(i)] = str(i)
6161

6262
user_input = ''.join(user_input)
63-
random_color: str = choice(background_color)
63+
random_color: str = choice(background_colors)
6464
return random_color + user_input + '\033[0m'
6565
except FileNotFoundError:
6666
pass
67-
return('\33[31m' + '"background_color.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
67+
return('\33[31m' + '"background_colors.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
6868
except TypeError:
6969
pass
7070
return('\33[31m' + f'Input type {type(i)} is not supported' + '\033[0m')
7171

72-
# ======== # Random Windows color # ======== #
73-
def windows_color(*user_input) -> str:
72+
# ======== # Random foreground color using standard colors # ======== #
73+
def standard_fg(*user_input) -> str:
7474
'''Change foreground of output to 15 different colors. It use standard colors so it work in every terminal.'''
7575
try:
76-
# ====== # Opening json file that contain colors code in windows and linux # ====== #
76+
# ====== # Opening json file that contain colors code in windows and unix # ====== #
7777
if name == 'nt':
78-
json_paths: str = __file__.replace('rcolor.py', 'colors\\Windows_color.json')
79-
else:
80-
json_paths: str = __file__.replace('rcolor.py', 'colors/Windows_color.json')
81-
with open(json_paths, 'r') as f:
82-
windows_color: list[str] = load(f).get("Windows_color")
78+
colors_paths: str = __file__.replace('rcolor.py', 'colors\\standard_colors.json')
79+
else:
80+
colors_paths: str = __file__.replace('rcolor.py', 'colors/standard_colors.json')
81+
with open(colors_paths, 'r') as f:
82+
standard_color: list[str] = load(f).get("standard_foreground_colors")
8383
user_input = list(user_input)
8484

8585
# ======== # make all item in user_input list to string # ======== #
@@ -91,26 +91,26 @@ def windows_color(*user_input) -> str:
9191
from colorama import just_fix_windows_console
9292
just_fix_windows_console()
9393
user_input = ''.join(user_input)
94-
random_color: str = choice(windows_color)
94+
random_color: str = choice(standard_color)
9595
return random_color + user_input + '\033[0m'
9696
except FileNotFoundError:
9797
pass
98-
return('\33[31m' + '"Windows_color.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
98+
return('\33[31m' + '"standard_colors.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
9999
except TypeError:
100100
pass
101101
return('\33[31m' + f'Input type {type(i)} is not supported' + '\033[0m')
102102

103-
# ======== # Random Windows background color # ======== #
104-
def windows_background(*user_input) -> str :
103+
# ======== # Random background color using standard color # ======== #
104+
def standard_bg(*user_input) -> str :
105105
'''Change background color of output to 15 different colors. It use standard colors so it work in every terminal..'''
106106
try:
107107
# ====== # Opening json file that contain colors code in windows and linux # ====== #
108108
if name == 'nt':
109-
json_paths: str = __file__.replace('rcolor.py', 'colors\\Windows_background.json')
110-
else:
111-
json_paths: str = __file__.replace('rcolor.py', 'colors/Windows_background.json')
112-
with open(json_paths, 'r') as f:
113-
Windows_background: list[str] = load(f).get("Windows_background")
109+
colors_paths: str = __file__.replace('rcolor.py', 'colors\\standard_background.json')
110+
else:
111+
colors_paths: str = __file__.replace('rcolor.py', 'colors/standard_background.json')
112+
with open(colors_paths, 'r') as f:
113+
standard_background: list[str] = load(f).get("standard_background")
114114
from colorama import just_fix_windows_console
115115
just_fix_windows_console()
116116
user_input = list(user_input)
@@ -123,28 +123,28 @@ def windows_background(*user_input) -> str :
123123
user_input[user_input.index(i)] = str(i)
124124

125125
user_input = ''.join(user_input)
126-
random_color: str = choice(Windows_background)
126+
random_color: str = choice(standard_background)
127127
return random_color + user_input + '\033[0m'
128128
except FileNotFoundError:
129129
pass
130-
return('\33[31m' + '"Windows_background.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
130+
return('\33[31m' + '"standard_background.json" did not found.\nIt\'s may because the file was moved or deleted' + '\033[0m')
131131
except TypeError:
132132
pass
133133
return('\33[31m' + f'Input type {type(i)} is not supported' + '\033[0m')
134134

135135
# ======== # Random style # ======== #
136136
def style(*user_input) -> str:
137137
'''Change font of output to 3 different font.'''
138-
try:
138+
try:
139139
# ====== # Opening json file that contain colors code in windows and linux # ====== #
140140
if name == 'nt':
141-
json_paths: str = __file__.replace('rcolor.py', 'colors\\style1.json')
142-
else:
143-
json_paths: str = __file__.replace('rcolor.py', 'colors/style1.json')
144-
with open(json_paths, 'r') as f:
141+
colors_paths: str = __file__.replace('rcolor.py', 'colors\\style1.json')
142+
else:
143+
colors_paths: str = __file__.replace('rcolor.py', 'colors/style1.json')
144+
with open(colors_paths, 'r') as f:
145145
style1: list[str] = load(f).get("style1")
146146
user_input = list(user_input)
147-
147+
148148
# ======== # make all item in user_input list to string # ======== #
149149
for i in user_input:
150150
if type(i) == str:
@@ -161,19 +161,28 @@ def style(*user_input) -> str:
161161
except TypeError:
162162
pass
163163
return('\33[31m' + f'Input type {type(i)} is not supported' + '\033[0m')
164-
165-
# ======== # change foreground for each character # ======== #
166-
def chameleons_char(user_input: str | int) -> str: #type: ignore
167-
''''''
168-
user_input: list[str] = list(str(user_input))
164+
165+
# ======== # change foreground color for each character # ======== #
166+
def rainbow_fg(user_input: str | int) -> str: #type: ignore
167+
'''It changes foreground color of every character in the text randomly to 255 different color, but it may not work in every terminal.'''
168+
user_input: str = str(user_input)
169169
Final_text: str = str()
170-
for i in enumerate(user_input):
171-
Final_text: str = Final_text + auto_color() + i + '\033[0m'
170+
for i in (user_input):
171+
Final_text: str = Final_text + foreground(i)
172172
return Final_text
173-
174-
173+
174+
# ======== # to change foreground color for each character using standard colors # ======== #
175+
def standard_rainbow_fg(user_input: str | int) -> str: #type: ignore
176+
'''It changes foreground color of every character in the text randomly to 15 different color.'''
177+
user_input: str = str(user_input)
178+
Final_text: str = str()
179+
for i in (user_input):
180+
Final_text: str = Final_text + standard_fg(i)
181+
return Final_text
182+
183+
175184
# ======== # Automatically detects the operating system and return the correct color # ======== #
176-
def auto_color(*user_input) -> str:
185+
def auto_fg(*user_input) -> str:
177186
'''Automatically detects the operating system and decide to use standard foreground(15 colors) or not standard foregrounds(255 colors)'''
178187
user_input = list(user_input)
179188
for i in user_input:
@@ -184,12 +193,12 @@ def auto_color(*user_input) -> str:
184193

185194
user_input = ''.join(user_input)
186195
if name == 'nt':
187-
return windows_color(user_input)
196+
return standard_fg(user_input)
188197
else:
189-
return color(user_input)
198+
return foreground(user_input)
190199

191200
# ======== # Automatically detects the operating system and return the correct background color # ======== #
192-
def auto_background(*user_input) -> str:
201+
def auto_bg(*user_input) -> str:
193202
'''Automatically detects the operating system and decide to use standard background colors(15 colors) or not standard background colors(255 colors)'''
194203
user_input = list(user_input)
195204
for i in user_input:
@@ -200,12 +209,12 @@ def auto_background(*user_input) -> str:
200209

201210
user_input = ''.join(user_input)
202211
if name == 'nt':
203-
return windows_background(user_input)
212+
return standard_bg(user_input)
204213
else:
205214
return background(user_input)
206215

207216
# ======== # return random color and background color # ======== #
208-
def color_background(*user_input) -> str:
217+
def foreground_background(*user_input) -> str:
209218
'''Change foreground and background color of output randomly at the same time.'''
210219
user_input = list(user_input)
211220
for i in user_input:
@@ -215,10 +224,10 @@ def color_background(*user_input) -> str:
215224
user_input[user_input.index(i)] = str(i)
216225

217226
user_input = ''.join(user_input)
218-
return auto_color(auto_background(user_input))
227+
return auto_fg(auto_bg(user_input))
219228

220229
# ======== # return random color and style # ======== #
221-
def color_style(*user_input) -> str:
230+
def foreground_style(*user_input) -> str:
222231
'''Change foreground and font of output randomly at the same time.'''
223232
user_input = list(user_input)
224233
for i in user_input:
@@ -228,7 +237,7 @@ def color_style(*user_input) -> str:
228237
user_input[user_input.index(i)] = str(i)
229238

230239
user_input = ''.join(user_input)
231-
return auto_color(style(user_input))
240+
return auto_fg(style(user_input))
232241

233242
# ======== # return random background color and style # ======== #
234243
def background_style(*user_input) -> str:
@@ -241,10 +250,10 @@ def background_style(*user_input) -> str:
241250
user_input[user_input.index(i)] = str(i)
242251

243252
user_input = ''.join(user_input)
244-
return auto_background(style(user_input))
253+
return auto_bg(style(user_input))
245254

246255
# ======== # return random color and background color and style # ======== #
247-
def color_background_style(*user_input) -> str:
256+
def foreground_background_style(*user_input) -> str:
248257
'''Change foreground, background color and font of output randomly at the same time.'''
249258
user_input = list(user_input)
250259
for i in user_input:
@@ -254,4 +263,4 @@ def color_background_style(*user_input) -> str:
254263
user_input[user_input.index(i)] = str(i)
255264

256265
user_input = ''.join(user_input)
257-
return auto_color(auto_background(style(user_input)))
266+
return auto_fg(auto_bg(style(user_input)))

0 commit comments

Comments
 (0)