File size: 2,291 Bytes
b094968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import modules.scripts as scripts
import scripts.cc_const as const
import json

STYLE_FILE = scripts.basedir() + '/' + 'styles.json'

EMPTY_STYLE = {
	'styles' : {},
	'deleted' : {}
}

class StyleManager():
	def __init__(self):
		self.STYLE_SHEET = None

	def load_styles(self):
		if self.STYLE_SHEET is not None:
			return

		try:
			with open(STYLE_FILE, 'r') as json_file:
				self.STYLE_SHEET = json.loads(json_file.read())
				print('[Vec. CC] Style Sheet Loaded...')
		except IOError:
			with open(STYLE_FILE, 'w+') as json_file:
				self.STYLE_SHEET = EMPTY_STYLE
				json_file.write(json.dumps(self.STYLE_SHEET))
				print('[Vec. CC] Creating Empty Style Sheet...')

	def list_style(self):
		return list(self.STYLE_SHEET['styles'].keys())

	def get_style(self, style_name):
		try:
			style = self.STYLE_SHEET['styles'][style_name]
			return style['alt'], style['brightness'], style['contrast'], style['saturation'], style['rgb'][0], style['rgb'][1], style['rgb'][2]
		except KeyError:
			print(f'\n[Warning] No Style of Name "{style_name}" Found!\n')
			return False, const.Brightness.default, const.Contrast.default, const.Saturation.default, const.R.default, const.G.default, const.B.default

	def save_style(self, style_name, latent, bri, con, sat, r, g, b):
		if style_name in self.STYLE_SHEET['styles'].keys():
			print(f'\n[Warning] Duplicated Style Name "{style_name}" Detected! Values are not saved!\n')
			return self.list_style()

		style = {
			'alt' : latent,
			'brightness' : bri,
			'contrast' : con,
			'saturation' : sat,
			'rgb' : [r, g, b]
		}

		self.STYLE_SHEET['styles'].update({style_name:style})

		with open(STYLE_FILE, 'w+') as json_file:
			json_file.write(json.dumps(self.STYLE_SHEET))

		print(f'\nStyle of Name "{style_name}" Saved!\n')
		return self.list_style()

	def delete_style(self, style_name):
		try:
			style = self.STYLE_SHEET['styles'][style_name]
			del self.STYLE_SHEET['styles'][style_name]
		except KeyError:
			print(f'\n[Warning] No Style of Name "{style_name}" Found!\n')
			return self.list_style()

		self.STYLE_SHEET['deleted'].update({style_name:style})

		with open(STYLE_FILE, 'w+') as json_file:
			json_file.write(json.dumps(self.STYLE_SHEET))

		print(f'\nStyle of Name "{style_name}" Deleted!\n')
		return self.list_style()