2020-06-04 05:33:17 +02:00
|
|
|
var localStorage = window.localStorage;
|
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const backgroundTextBox = document.getElementById('backgroundSet');
|
|
|
|
const backgroundOpacityTextBox = document.getElementById('backgroundOpacitySet');
|
|
|
|
const foregroundTextBox = document.getElementById('foregroundSet');
|
|
|
|
const foregroundOpacityTextBox = document.getElementById('foregroundOpacitySet');
|
2020-06-04 05:33:17 +02:00
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const blurTextBox = document.getElementById('blurSet');
|
2020-06-04 05:33:17 +02:00
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const applyTheme = document.getElementById('themeEngineAsDefault');
|
|
|
|
const resetTheme = document.getElementById('themeEngineReset');
|
2020-06-04 05:33:17 +02:00
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
// Save CSS color constiables default in localStorage
|
2020-06-04 05:33:17 +02:00
|
|
|
// Will only save if you visit the webpage for the very first time
|
|
|
|
const saveDefaultCSS = () => {
|
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const origBaseBG = localStorage.getItem('origBaseBG');
|
|
|
|
const origBaseColor = localStorage.getItem('origBaseColor');
|
|
|
|
const origBlurStrength = localStorage.getItem('origBlurStrength');
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
if ((origBaseBG === null) || (origBaseColor === null) || (origBlurStrength) === null) {
|
|
|
|
|
|
|
|
localStorage.setItem(
|
|
|
|
'origBaseBG',
|
|
|
|
window.getComputedStyle(document.documentElement).getPropertyValue('--base-bg')
|
|
|
|
);
|
|
|
|
localStorage.setItem(
|
|
|
|
'origBaseColor',
|
|
|
|
window.getComputedStyle(document.documentElement).getPropertyValue('--base-color')
|
|
|
|
);
|
|
|
|
localStorage.setItem(
|
|
|
|
'origBlurStrength',
|
|
|
|
window.getComputedStyle(document.documentElement).getPropertyValue('--blur-strength')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check color validity
|
|
|
|
const checkColorValidity = (colorStr) => {
|
|
|
|
|
|
|
|
// Check if RGBA - (#RRGGBBAA)
|
2020-06-07 09:18:09 +02:00
|
|
|
const colorRGBA = /^#[0-9A-F]{8}$/i.test(colorStr);
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
// If not RGBA
|
|
|
|
if (!colorRGBA) {
|
|
|
|
|
|
|
|
// If RGB - (#RRGGBB)
|
|
|
|
if (/^#([0-9A-F]{3}){1,2}$/i.test(colorStr)) {
|
|
|
|
|
|
|
|
// Add completely opaque alpha
|
|
|
|
return colorStr + 'FF';
|
|
|
|
|
|
|
|
// If three-charactered HEX color - (#RGB)
|
|
|
|
} else if (/^#([0-9A-F]{3}){1,2}$/i.test(colorStr)) {
|
|
|
|
|
|
|
|
// Convert it to RRGGBB
|
|
|
|
return colorStr.replace(/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/, "#$1$1$2$2$3$3");
|
|
|
|
|
|
|
|
// If three-charactered HEX Color(#RGB) with AA - (#RGBAA)
|
|
|
|
} else if (colorStr.length == 6) {
|
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const bg = colorStr.slice(0, -2);
|
|
|
|
const op = colorStr.slice(-2);
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
return bg.replace(/^#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/, "#$1$1$2$2$3$3") + op;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
alert('Invalid color');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's RGBA and a valid color so just return it
|
|
|
|
return colorStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update textboxes
|
|
|
|
const updateTextBoxValues = (bgColor, bgOpacity, fgColor, fgOpacity, blurPower) => {
|
|
|
|
// Set placeholders
|
|
|
|
backgroundTextBox.value = '';
|
|
|
|
backgroundTextBox.placeholder = bgColor;
|
|
|
|
|
|
|
|
backgroundOpacityTextBox.value = '';
|
|
|
|
backgroundOpacityTextBox.placeholder = bgOpacity;
|
|
|
|
|
|
|
|
foregroundTextBox.value = '';
|
|
|
|
foregroundTextBox.placeholder = fgColor;
|
|
|
|
foregroundOpacityTextBox.value = '';
|
|
|
|
foregroundOpacityTextBox.placeholder = fgOpacity;
|
|
|
|
|
|
|
|
blurTextBox.value = '';
|
|
|
|
blurTextBox.placeholder = blurPower;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Theme processing
|
|
|
|
const processTheme = () => {
|
|
|
|
|
|
|
|
// Retrieve custom colors
|
2020-06-07 09:18:09 +02:00
|
|
|
let baseBG = localStorage.getItem('baseBG');
|
|
|
|
let baseColor = localStorage.getItem('baseColor');
|
|
|
|
let blurStrength = localStorage.getItem('blurStrength');
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
// If custom color doesn't exist, use the value in CSS
|
|
|
|
if (baseBG === null) {
|
|
|
|
baseBG = localStorage.getItem('origBaseBG');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (baseColor === null) {
|
|
|
|
baseColor = localStorage.getItem('origBaseColor');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blurStrength === null) {
|
|
|
|
blurStrength = localStorage.getItem('origBlurStrength');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove whitespace
|
|
|
|
baseBG = baseBG.replace(/ /g,'');
|
|
|
|
baseColor = baseColor.replace(/ /g,'');
|
|
|
|
blurStrength = blurStrength.replace(/ /g,'');
|
|
|
|
|
|
|
|
// Check validity
|
|
|
|
baseBG = checkColorValidity(baseBG);
|
|
|
|
baseColor = checkColorValidity(baseColor);
|
|
|
|
|
|
|
|
// Slice to separate RGB and A of background color
|
|
|
|
// Slice alpha out
|
2020-06-07 09:18:09 +02:00
|
|
|
const backgroundColor = baseBG.slice(0, -2);
|
2020-06-04 05:33:17 +02:00
|
|
|
// Get alpha
|
2020-06-07 09:18:09 +02:00
|
|
|
const backgroundOpacity = baseBG.slice(-2);
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Slice to separate RGB and A of foreground color
|
|
|
|
// Slice alpha out
|
2020-06-07 09:18:09 +02:00
|
|
|
const foregroundColor = baseColor.slice(0, -2);
|
2020-06-04 05:33:17 +02:00
|
|
|
// Get alpha
|
2020-06-07 09:18:09 +02:00
|
|
|
const foregroundOpacity = baseColor.slice(-2);
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
updateTextBoxValues(
|
|
|
|
backgroundColor,
|
|
|
|
backgroundOpacity,
|
|
|
|
foregroundColor,
|
|
|
|
foregroundOpacity,
|
|
|
|
blurStrength
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update colors
|
2020-06-07 09:18:09 +02:00
|
|
|
const updateCSSconstiables = () => {
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
// Get value from input fields
|
2020-06-07 09:18:09 +02:00
|
|
|
const background = (backgroundTextBox.value || backgroundTextBox.placeholder) +
|
2020-06-04 05:33:17 +02:00
|
|
|
(backgroundOpacityTextBox.value || backgroundOpacityTextBox.placeholder);
|
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const foreground = (foregroundTextBox.value || foregroundTextBox.placeholder) +
|
2020-06-04 05:33:17 +02:00
|
|
|
(foregroundOpacityTextBox.value || foregroundOpacityTextBox.placeholder);
|
|
|
|
|
2020-06-07 09:18:09 +02:00
|
|
|
const blurPower = (blurTextBox.value || blurTextBox.placeholder);
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
// Check color validity
|
2020-06-07 09:18:09 +02:00
|
|
|
const bgColor = checkColorValidity(background);
|
|
|
|
const fgColor = checkColorValidity(foreground);
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
// Change CSS colors
|
|
|
|
document.documentElement.style.setProperty('--base-bg', bgColor);
|
|
|
|
document.documentElement.style.setProperty('--base-color', fgColor);
|
|
|
|
document.documentElement.style.setProperty('--blur-strength', blurPower);
|
|
|
|
|
|
|
|
// Save custom color
|
|
|
|
localStorage.setItem('baseBG', bgColor);
|
|
|
|
localStorage.setItem('baseColor', fgColor);
|
|
|
|
localStorage.setItem('blurStrength', blurPower);
|
|
|
|
|
|
|
|
processTheme();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run on window onload
|
|
|
|
const updateOnStartUp = () => {
|
|
|
|
// Update
|
|
|
|
processTheme();
|
|
|
|
|
|
|
|
// Update settings
|
2020-06-07 09:18:09 +02:00
|
|
|
updateCSSconstiables();
|
2020-06-04 05:33:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply button event handler
|
2020-06-07 08:34:27 +02:00
|
|
|
applyTheme.onclick = () => {
|
2020-06-07 09:18:09 +02:00
|
|
|
updateCSSconstiables();
|
2020-06-04 05:33:17 +02:00
|
|
|
alert('Success!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset button event handler
|
2020-06-07 08:34:27 +02:00
|
|
|
resetTheme.onclick = () => {
|
2020-06-04 05:33:17 +02:00
|
|
|
localStorage.removeItem('baseBG');
|
|
|
|
localStorage.removeItem('baseColor');
|
|
|
|
localStorage.removeItem('blurStrength');
|
|
|
|
|
|
|
|
saveDefaultCSS();
|
|
|
|
processTheme();
|
2020-06-07 09:18:09 +02:00
|
|
|
updateCSSconstiables();
|
2020-06-04 05:33:17 +02:00
|
|
|
|
|
|
|
alert('Success!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
const onStartUp = () => {
|
|
|
|
saveDefaultCSS();
|
|
|
|
updateOnStartUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call Initialize
|
|
|
|
window.onload = onStartUp();
|