aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.ts
diff options
context:
space:
mode:
authorSam Wilkins <samwilkins333@gmail.com>2019-09-12 03:10:25 -0400
committerSam Wilkins <samwilkins333@gmail.com>2019-09-12 03:10:25 -0400
commitd68dfd98a75d188a832da6f27d641b451fca7c56 (patch)
tree2103e87d335d9651e1878748a4fc7c9c6f9f7a13 /src/Utils.ts
parent2dd8b13fd3fa30fc390251ed75da3207efed4d5b (diff)
parent186d7aed7b99b1373e99b51cfe0c88c8167c8290 (diff)
merged with master and batch directory upload requests
Diffstat (limited to 'src/Utils.ts')
-rw-r--r--src/Utils.ts106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/Utils.ts b/src/Utils.ts
index 71d88683a..ec2bee9bf 100644
--- a/src/Utils.ts
+++ b/src/Utils.ts
@@ -58,6 +58,98 @@ export class Utils {
document.body.removeChild(textArea);
}
+ public static fromRGBAstr(rgba: string) {
+ let rm = rgba.match(/rgb[a]?\(([0-9]+)/);
+ let r = rm ? Number(rm[1]) : 0;
+ let gm = rgba.match(/rgb[a]?\([0-9]+,([0-9]+)/);
+ let g = gm ? Number(gm[1]) : 0;
+ let bm = rgba.match(/rgb[a]?\([0-9]+,[0-9]+,([0-9]+)/);
+ let b = bm ? Number(bm[1]) : 0;
+ let am = rgba.match(/rgba?\([0-9]+,[0-9]+,[0-9]+,([0-9]+)/);
+ let a = am ? Number(am[1]) : 0;
+ return { r: r, g: g, b: b, a: a };
+ }
+ public static toRGBAstr(col: { r: number, g: number, b: number, a?: number }) {
+ return "rgba(" + col.r + "," + col.g + "," + col.b + (col.a !== undefined ? "," + col.a : "") + ")";
+ }
+
+ public static HSLtoRGB(h: number, s: number, l: number) {
+ // Must be fractions of 1
+ // s /= 100;
+ // l /= 100;
+
+ let c = (1 - Math.abs(2 * l - 1)) * s,
+ x = c * (1 - Math.abs((h / 60) % 2 - 1)),
+ m = l - c / 2,
+ r = 0,
+ g = 0,
+ b = 0;
+ if (0 <= h && h < 60) {
+ r = c; g = x; b = 0;
+ } else if (60 <= h && h < 120) {
+ r = x; g = c; b = 0;
+ } else if (120 <= h && h < 180) {
+ r = 0; g = c; b = x;
+ } else if (180 <= h && h < 240) {
+ r = 0; g = x; b = c;
+ } else if (240 <= h && h < 300) {
+ r = x; g = 0; b = c;
+ } else if (300 <= h && h < 360) {
+ r = c; g = 0; b = x;
+ }
+ r = Math.round((r + m) * 255);
+ g = Math.round((g + m) * 255);
+ b = Math.round((b + m) * 255);
+ return { r: r, g: g, b: b };
+ }
+
+ public static RGBToHSL(r: number, g: number, b: number) {
+ // Make r, g, and b fractions of 1
+ r /= 255;
+ g /= 255;
+ b /= 255;
+
+ // Find greatest and smallest channel values
+ let cmin = Math.min(r, g, b),
+ cmax = Math.max(r, g, b),
+ delta = cmax - cmin,
+ h = 0,
+ s = 0,
+ l = 0;
+ // Calculate hue
+
+ // No difference
+ if (delta == 0)
+ h = 0;
+ // Red is max
+ else if (cmax == r)
+ h = ((g - b) / delta) % 6;
+ // Green is max
+ else if (cmax == g)
+ h = (b - r) / delta + 2;
+ // Blue is max
+ else
+ h = (r - g) / delta + 4;
+
+ h = Math.round(h * 60);
+
+ // Make negative hues positive behind 360°
+ if (h < 0)
+ h += 360; // Calculate lightness
+
+ l = (cmax + cmin) / 2;
+
+ // Calculate saturation
+ s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
+
+ // Multiply l and s by 100
+ // s = +(s * 100).toFixed(1);
+ // l = +(l * 100).toFixed(1);
+
+ return { h: h, s: s, l: l };
+ }
+
+
public static GetClipboardText(): string {
var textArea = document.createElement("textarea");
document.body.appendChild(textArea);
@@ -138,6 +230,20 @@ export function WithKeys(obj: any, keys: string[], addKeyFunc?: (dup: any) => vo
return dup;
}
+export function timenow() {
+ var now = new Date();
+ let ampm = 'am';
+ let h = now.getHours();
+ let m: any = now.getMinutes();
+ let s: any = now.getSeconds();
+ if (h >= 12) {
+ if (h > 12) h -= 12;
+ ampm = 'pm';
+ }
+ if (m < 10) m = '0' + m;
+ return now.toLocaleDateString() + ' ' + h + ':' + m + ' ' + ampm;
+}
+
export function numberRange(num: number) { return Array.from(Array(num)).map((v, i) => i); }
export function returnTrue() { return true; }