-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility_functions.py
120 lines (106 loc) · 3.74 KB
/
utility_functions.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Functions for usage in Colour Tint Converter main file
# ------------- Functions for converting linear SRGB (SRGB[0,1]) -------------
# Function to convert linear-srgb value to SRGB8
def LSRGBtoSRGB8(lsrgb):
rgb_list = []
for nlvalue in lsrgb:
if nlvalue < 0.0031308:
# Converts SRGB Linear to SRGB Non-Linear
lvalue = nlvalue * 12.92
# Converts Non-Linear SRGB to SRGB8 and ints
value8 = int(256 * lvalue)
rgb_list.append(value8)
else:
lvalue = 1.055 * nlvalue ** (1 / 2.4) - 0.055
value8 = int(256 * lvalue)
rgb_list.append(value8)
# Returns int tuple for entry into webcolor function
return tuple(rgb_list)
# Converts SRGB8 values from colour picker to SRBG
def RGB8toLSRGB(rgbt):
lsrgb_list = []
for rbg_val in rgbt:
nlsrgb = rbg_val / 255
if nlsrgb < 0.04045:
lsrgb = nlsrgb / 12.92
lsrgb_list.append(lsrgb)
else:
lsrgb = ((nlsrgb + 0.055) / 1.055) ** 2.4
lsrgb_list.append(lsrgb)
return tuple(lsrgb_list)
# ------------- Functions for converting non-linear SRGB (SRGB'[0,1]) -------------
# Converts non-linear SRGB to SRGB8
def NLSRGBtoSRGB8(nlsrgb):
return tuple([int(256 * nl) for nl in nlsrgb])
# Converts SRGB8 to non-linear SRGB
def RGB8toNLSRGB(srgb8):
return tuple([rgb / 255 for rgb in srgb8])
# ------------- Tests to confirm entries are as expected or warn/adjust as required -------------
# If greater than 1 return 1 (useful in print)
def export_clarity(value, conv_type):
# Converts value over 1 to 1 to make printing make more sense and returns 0 for non numerical ones
if not conv_type == "sRGB8 [0,255]":
if 1.0 <= value:
return 1.0
elif 0.0 <= value < 1.0:
return value
else:
return 0.0
# Converts value over 255 to 255 to make printing make more sense and returns 0 for non numerical ones
else:
if 255 <= value:
return 255
elif 0 <= value < 255:
return int(value)
else:
return 0
# Tests if correct input is entered and returns false if incorrect, this allows for box to display red
def incorrect_entry_test(value, conv_type):
if not conv_type == "sRGB8 [0,255]":
if not value == "":
if not value == ".":
try:
if not 0 <= float(value) <= 1:
return False
else:
return True
except ValueError:
return False
else:
return True
else:
return True
else:
if not value == "":
try:
if not 0 <= int(value) <= 255:
return False
else:
return True
except ValueError:
return False
else:
return True
# Conversion for calculation to float or int and returns 0 if error
def get_entries_convert(get_entries, conv_type):
converted_get_entries = []
if conv_type == 'sRGB8 [0,255]':
for entry in get_entries:
try:
int_entry = int(entry)
except ValueError:
int_entry = 0
converted_get_entries.append(int_entry)
else:
for entry in get_entries:
try:
flt_entry = float(entry)
except ValueError:
flt_entry = 0.0
converted_get_entries.append(flt_entry)
return tuple(converted_get_entries)
# Warning to confirm if functions file was run independently
def main():
print("Have you run the correct file?")
if __name__ == "__main__":
main()