forked from mcprtk/tty_color_chooser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
83 lines (66 loc) · 2.51 KB
/
converter.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
# Copyright 2018-2020, Pratik Mullick
#
# This file is part of ttycolor.
#
# ttycolor is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ttycolor is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <https://www.gnu.org/licenses/>.
import yaml
class Converter:
def __init__(self, hex_dict):
self.hex_dict = hex_dict
self.int_dict = self.hexdict2intdict()
self.color_lines_tuple = self.col_lines()
def hex2int_tuple(self,col_hex_string):
col_hex_string = (col_hex_string.lower()).strip("#")
r_dec = int('0x' + col_hex_string[:2], 16)
g_dec = int('0x' + col_hex_string[2:4], 16)
b_dec = int('0x' + col_hex_string[-2:], 16)
return r_dec,g_dec,b_dec
def hexdict2intdict(self):
int_dict = {}
data_dict = self.hex_dict
for code,shade in data_dict.items():
int_dict[code] = self.hex2int_tuple(shade)
return int_dict
def col_lines(self):
red_arr = []
grn_arr = []
blu_arr = []
for code,shade in self.int_dict.items():
red_arr.append(shade[0])
grn_arr.append(shade[1])
blu_arr.append(shade[2])
return red_arr,grn_arr,blu_arr
class Operations(Converter):
def __init__(self, input_yaml):
self.input_yaml = input_yaml
self.yaml_dict = self.read_yaml()
self.colors = self.yaml_dict["color"]
self.metadata = self.yaml_dict["metadata"]
super().__init__(self.colors)
self.line_tuple = self.col_lines()
def read_yaml(self):
with open(self.input_yaml,'r') as filestream:
yaml_dict = yaml.safe_load(filestream.read())
return yaml_dict
def dict2svr(self):
line_arr = []
for line in self.line_tuple:
current_line = ""
for pos,item in enumerate(line):
if pos == len(line) - 1:
current_line += str(item) + '\n'
else:
current_line += str(item) + ','
line_arr.append(current_line)
return line_arr