summaryrefslogtreecommitdiff
path: root/assets/js/app.js
blob: 507552512cdcbf45a23a50c74122cb64ea9eb031 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
function getRGB() {
	var r = Math.floor(Math.random() * 255);
	var g = Math.floor(Math.random() * 255);
	var b = Math.floor(Math.random() * 255);
	return [r, g, b];
}

function getHex(rgb) {
	var hex = "";
	rgb.forEach(function (val) {
		var hexPartial = Number(val).toString(16);
		if (hexPartial.length < 2) {
			hexPartial = "0" + hexPartial;
		}
		hex = hex + hexPartial;
	});
	return hex;
}

function getCMYK(rgb) {
	var finalK = 0;

	var r = rgb[0];
	var g = rgb[1];
	var b = rgb[2];

	if (r === 0 && g === 0 && b === 0) {
		finalK = 1;
		return [0, 0, 0, 1];
	}

	var finalC = 1 - r / 255;
	var finalM = 1 - g / 255;
	var finalY = 1 - b / 255;

	var minCMY = Math.min(finalC, Math.min(finalM, finalY));
	finalC = Math.trunc(((finalC - minCMY) / (1 - minCMY)) * 100);
	finalM = Math.trunc(((finalM - minCMY) / (1 - minCMY)) * 100);
	finalY = Math.trunc(((finalY - minCMY) / (1 - minCMY)) * 100);
	finalK = Math.trunc(minCMY * 100);

	return finalC + "," + finalM + "," + finalY + "," + finalK;
}

function getContrast(rgb) {
	return (299 * rgb[0] + 587 * rgb[1] + 114 * rgb[2]) / 1000;
}

function generate(elements) {
	elements.each(function (index, column) {
		let rgb = getRGB();
		$(column).find(".color-rgb").text(`(${rgb})`);
		$(column)
			.find(".color-hex")
			.text("#" + getHex(rgb));
		$(column)
			.find(".color-cmyk")
			.text(`(${getCMYK(rgb)})`);
		$(column).css("background-color", `rgb(${rgb})`);
		if (getContrast(rgb) < 123) {
			$(column).addClass("text-white");
		} else {
			$(column).removeClass("text-white");
		}
	});
}

function regenerate() {
	generate($(".color-column[data-locked='false']"));
}

function copy(type, text) {
	var $tempTextField = $("<input>");
	$("body").append($tempTextField);
	switch (type) {
		case "rgb":
			$tempTextField.val("rgb" + text).select();
			break;
		case "hex":
			$tempTextField.val(text).select();
			break;
		case "cmyk":
			$tempTextField.val("cmyk" + text).select();
			break;
	}
	document.execCommand("copy");
	$tempTextField.remove();
}

function showToast(type, text) {
	var alert =
		"<div class='toast-alert alert alert-" +
		type +
		"' role='alert'>" +
		text +
		"</div>";
	$(".container-fluid").append(alert);
	$(".toast-alert").animate(
		{
			opacity: 0,
		},
		2000,
		function () {
			$(this).remove();
		}
	);
}

function init() {
	regenerate();

	$(".color-value").click(function (e) {
		var text = $(e.target).text();
		copy($(e.target).data("format"), text);
		showToast("success", "Color code copied to clipboard.");
	});

	$(".color-column-lock").click(function (e) {
		var icon = $(e.target);
		var column = $(e.target).parent().parent();
		var status = column.attr("data-locked");
		if (status === "true") {
			column.attr("data-locked", "false");
		} else {
			column.attr("data-locked", "true");
		}
		icon.toggleClass(["fa-lock-open", "fa-lock"]);
	});

	$(".color-column-regenerate").click(function (e) {
		generate($(e.target).parent().parent());
	});

	$("#submitNewColor").click(function (e) {
		setNewColor(getNewColor(e));
	});

	loadAllPalettes();
}

function setNewColor(values) {
	var column = $(".color-column:eq(" + (parseInt(values[1]) - 1) + ")");
	var rgb = JSON.parse("[" + values[0] + "]");
	$(column).find(".color-rgb").text(`(${rgb})`);
	$(column)
		.find(".color-hex")
		.text("#" + getHex(rgb));
	$(column)
		.find(".color-cmyk")
		.text(`(${getCMYK(rgb)})`);
	$(column).css("background-color", `rgb(${rgb})`);
	if (getContrast(rgb) < 123) {
		$(column).addClass("text-white");
	} else {
		$(column).removeClass("text-white");
	}
}

function getNewColor(e) {
	var string = $(e.target).closest(".modal-content").find("input").val();
	var newString = string.replace("(", "").replace(")", "").replace("rgb", "");
	var column = $(e.target)
		.closest(".modal-content")
		.find("select option:selected")
		.text();
	return [newString, column];
}

function switchTheme() {
	$("body").toggleClass("custom-bg");
	$(".fa-moon").toggleClass("d-none");
	$(".fa-sun").toggleClass("d-none");
	$(".color-column-labels").toggleClass("text-white");
}

function storePalette(keysObject, paletteName) {
	// Query object of all rgb colors and store text values in new object
	var rgbDOMObject = $(".color-rgb");
	var rgbStorageObject = {
		0: $(rgbDOMObject[0]).text().replace("(", "").replace(")", ""),
		1: $(rgbDOMObject[1]).text().replace("(", "").replace(")", ""),
		2: $(rgbDOMObject[2]).text().replace("(", "").replace(")", ""),
		3: $(rgbDOMObject[3]).text().replace("(", "").replace(")", ""),
		4: $(rgbDOMObject[4]).text().replace("(", "").replace(")", ""),
	};
	// Generate random key, ensure it doesn't exist already, then save the key in storage
	var randomKey = generateRandomKey();
	if (keysObject === null) {
		var newKeysObject = {};
		newKeysObject[paletteName] = randomKey;
	} else {
		var newKeysObject = keysObject;
		newKeysObject[paletteName] = randomKey;
	}
	localStorage.setItem("paletteKeys", JSON.stringify(newKeysObject));
	// Save color palette object
	localStorage.setItem(randomKey, JSON.stringify(rgbStorageObject));

	return randomKey;
}

function savePalette() {
	// Get palette names object and user input of new palette name
	var keysObject = JSON.parse(localStorage.getItem("paletteKeys"));
	var paletteName = $("#inputPaletteName").val();
	var nameCheck = false;
	// Check palette name exists
	if (keysObject !== null && paletteName in keysObject) {
		nameCheck = true;
	}
	if (typeof Storage !== "undefined") {
		// If the input name is null or empty, show an error alert
		if (
			paletteName === null ||
			paletteName == "undefined" ||
			paletteName == ""
		) {
			alert("Error: You must enter a valid name for this palette.");
		} else if (keysObject != null && nameCheck === true) {
			// If the name exists, ask user to confirm
			var userConfirmation = confirm(
				"Palette name exists. Do you want to overwrite this palette?"
			);
			// If the user confirms, save the palette
			if (userConfirmation) {
				var randomKey = storePalette(keysObject, paletteName);
				$("#savedPalettesBody").append(
					"<button class='btn btn-outline-secondary my-2 w-100' onclick='loadPalette(" +
						randomKey +
						")'>" +
						paletteName +
						"</button>"
				);
				showToast("success", "Color palette saved.");
			}
		} else {
			// If the name doesn't exist, save palette
			var randomKey = storePalette(keysObject, paletteName);
			$("#savedPalettesBody").append(
				"<div id='" +
					randomKey +
					"' class='my-2 d-flex'><div class='btn-group w-100' role='group' aria-label='Saved palette'><button class='btn btn-secondary w-100' onclick='loadPalette(" +
					randomKey +
					")'>" +
					paletteName +
					"</button><button class='btn btn-danger' onclick='deletePalette(" +
					randomKey +
					")'><i class='far fa-trash-alt'></i></button></div></div>"
			);
			showToast("success", "Color palette saved.");
		}
	} else {
		alert(
			"Sorry, your browser does not support Web Storage. Please ugrade your browser and try again."
		);
	}
}

function loadAllPalettes() {
	if (typeof Storage !== "undefined") {
		var fetchedData = localStorage.getItem("paletteKeys");
		if (fetchedData === null) {
			console.log("Warning: No palettes exist.");
		} else {
			$.each(JSON.parse(fetchedData), function (key, value) {
				var palette = localStorage.getItem(key);
				$("#savedPalettesBody").append(
					"<div id='" +
						value +
						"' class='my-2 d-flex'><div class='btn-group w-100' role='group' aria-label='Saved palette'><button class='btn btn-secondary w-100' onclick='loadPalette(" +
						value +
						")'>" +
						key +
						"</button><button class='btn btn-danger' onclick='deletePalette(" +
						value +
						")'><i class='far fa-trash-alt'></i></button></div></div>"
				);
				$("#editColorModal").modal("hide");
			});
			showToast("success", "Color palettes loaded.");
		}
	} else {
		alert(
			"Sorry, your browser does not support Web Storage. Please ugrade your browser and try again."
		);
	}
}

function loadPalette(requestedKey) {
	var fetchedData = localStorage.getItem("paletteKeys");
	$.each(JSON.parse(fetchedData), function (key, value) {
		var newValue = parseInt(value, 10);
		if (requestedKey === newValue) {
			var palette = localStorage.getItem(value);
			$.each(JSON.parse(palette), function (subKey, subValue) {
				var col = parseInt(subKey, 10) + 1;
				setNewColor([subValue, col]);
			});
		}
		$("#editColorModal").modal("hide");
	});
}

function deleteAllPalettes() {
	var confirmation = confirm("Are you sure you want to delete all palettes?");
	if (confirmation) {
		$("#savedPalettesBody button").remove();
		localStorage.clear();
	}
}

function deletePalette(id) {
	var confirmation = confirm("Are you sure you want to delete this palette?");
	if (confirmation) {
		// Get name of palette by the id
		var keysObject = JSON.parse(localStorage.getItem("paletteKeys"));
		var name = getKeyByValue(keysObject, id);
		// Remove the button
		$("#" + id).remove();
		// Remove local storage item
		localStorage.removeItem(id);
		// Remove item from paletteKeys
		delete keysObject[name];
		// Set new paletteKeys object
		var newKeysObject = keysObject;
		localStorage.setItem("paletteKeys", JSON.stringify(newKeysObject));
	}
}

function generateRandomKey() {
	// Generate random key
	var randomKey = Math.floor(Math.random() * 9007199254740992 + 1);
	// Check if random key exists (up to 100 times)
	for (var i = 0; i < 100; i++) {
		// If the key exists, generate a new key
		if (checkKey(randomKey) === true) {
			randomKey = Math.floor(Math.random() * 9007199254740992 + 1);
		}
		// If the key does not exist, break the loop and return the key
		else {
			break;
		}
	}
	return randomKey;
}

function checkKey(key) {
	// Fetch paletteKeys object
	var keysObject = localStorage.getItem("paletteKeys");
	// If object is null, it doesn't exist yet
	if (keysObject !== null) {
		// If object exists, parse it and check to see if the key exists
		var tempObject = JSON.parse(keysObject);
		if (tempObject.hasOwnProperty(key.toString())) {
			return true;
		} else {
			return false;
		}
	}
}

function saveImage() {
	// Remove previous image
	$("#saveImageModal img").remove();
	// Hide toolbar so it doesn't appear in screenshot
	$(".color-column-toolbar").hide();
	// Set background color of canvas based on body
	var bgColor = $("body").hasClass("custom-bg") ? "#000000" : "#FFFFFF";
	// Get canvas and launch modal
	html2canvas(document.getElementsByClassName("container-fluid")[0], {
		backgroundColor: bgColor,
	}).then(function (canvas) {
		$(".color-column-toolbar").show();
		var img = canvas.toDataURL("image/png");
		$("#saveImageModal").modal("show");
		$("#saveImageModal .modal-body").append(
			'<img id="palette-image" class="img-fluid" src="' + img + '"/>'
		);
	});
}

function getKeyByValue(object, value) {
	for (var i = 0; i < Object.keys(object).length; i++) {
		var key = Object.keys(object)[i];
		var fetchedValue = object[key];
		if (value === fetchedValue) {
			return key;
		}
	}
}