let selectedPalette = [];
let animationsEnabled = true;
// --- LOCAL STORAGE LOADING ---
// Load palette from Local Storage on startup
const savedPaletteJSON = localStorage.getItem('userPalette');
if (savedPaletteJSON) {
try {
selectedPalette = JSON.parse(savedPaletteJSON);
} catch (e) {
console.error("Error parsing saved palette from Local Storage", e);
selectedPalette = []; // Reset if data is corrupt
}
}
// Main function to fetch color data and build the page
fetch('colors.json')
.then(response => response.json())
.then(data => {
const colorFamiliesContainer = document.getElementById('color-families');
// Create the color swatch for each color in the JSON data
data.forEach(family => {
const familyDiv = document.createElement('div');
familyDiv.classList.add('color-family');
familyDiv.innerHTML = `
${family.family}
`;
const swatchContainer = document.createElement('div');
swatchContainer.classList.add('swatch-container');
family.colors.forEach(color => {
const swatchWrapper = document.createElement('div');
swatchWrapper.classList.add('swatch-wrapper');
const swatch = document.createElement('div');
swatch.classList.add('color-swatch');
swatch.dataset.color = color.hex;
const backgroundDiv = document.createElement('div');
backgroundDiv.classList.add('color-background');
if (color.image) {
backgroundDiv.style.backgroundImage = `url(${color.image})`;
} else {
backgroundDiv.style.backgroundColor = color.hex;
}
if (color.metallic && color.chromeType) {
backgroundDiv.classList.add('metallic', `chrome-${color.chromeType}`);
}
if (isLightColor(color.hex)) {
backgroundDiv.style.border = '1px solid rgba(0, 0, 0, 0.2)';
}
swatch.appendChild(backgroundDiv);
const shineImg = document.createElement('img');
shineImg.classList.add('color-shine');
shineImg.src = "shine.svg";
shineImg.alt = "";
swatch.appendChild(shineImg);
const colorName = document.createElement('span');
colorName.classList.add('color-name');
colorName.textContent = color.name;
// Event listener to add/remove a color from the palette
swatch.addEventListener('click', () => {
const isSelected = selectedPalette.some(c => c.hex === color.hex);
if (isSelected) {
selectedPalette = selectedPalette.filter(c => c.hex !== color.hex);
} else {
selectedPalette.push(color);
}
// Add a 'pop' animation on click
backgroundDiv.classList.add('pop');
backgroundDiv.addEventListener('animationend', () => {
backgroundDiv.classList.remove('pop');
}, { once: true });
renderSelectedPalette();
updateSwatchHighlights();
});
swatchWrapper.appendChild(swatch);
swatchWrapper.appendChild(colorName);
swatchContainer.appendChild(swatchWrapper);
});
familyDiv.appendChild(swatchContainer);
colorFamiliesContainer.appendChild(familyDiv);
});
// Initial renders after setting up the page
renderSelectedPalette();
updateSwatchHighlights();
// Check if a palette was shared in the URL (will override Local Storage)
loadPaletteFromURL(data);
})
.catch(error => console.error('Error loading colors:', error));
/**
* Saves the current 'selectedPalette' array to the browser's Local Storage.
*/
function savePaletteToLocalStorage() {
localStorage.setItem('userPalette', JSON.stringify(selectedPalette));
}
/**
* Renders the selected colors as floating balloons in the top palette.
*/
function renderSelectedPalette() {
const paletteColorsContainer = document.getElementById('palette-colors');
paletteColorsContainer.innerHTML = '';
selectedPalette.forEach(color => {
const swatchWrapper = document.createElement('div');
swatchWrapper.classList.add('swatch-wrapper');
const floatGroup = document.createElement('div');
floatGroup.classList.add('balloon-float-group');
if (animationsEnabled) {
floatGroup.style.animationDuration = `${(Math.random() * 3 + 3).toFixed(2)}s`;
floatGroup.style.animationDelay = `${(Math.random() * 2).toFixed(2)}s`;
} else {
floatGroup.style.animation = 'none';
}
const swatch = document.createElement('div');
swatch.classList.add('color-swatch');
swatch.dataset.color = color.hex;
const backgroundDiv = document.createElement('div');
backgroundDiv.classList.add('color-background', 'chosen');
if (color.image) {
backgroundDiv.style.backgroundImage = `url(${color.image})`;
} else {
backgroundDiv.style.backgroundColor = color.hex;
}
if (isLightColor(color.hex)) {
backgroundDiv.style.border = '1px solid rgba(0, 0, 0, 0.2)';
}
if (color.metallic && color.chromeType) {
backgroundDiv.classList.add('metallic', `chrome-${color.chromeType}`);
}
swatch.appendChild(backgroundDiv);
const shineImg = document.createElement('img');
shineImg.classList.add('color-shine');
shineImg.src = "shine.svg";
shineImg.alt = "";
swatch.appendChild(shineImg);
const svgNS = "http://www.w3.org/2000/svg";
const stringSVG = document.createElementNS(svgNS, "svg");
stringSVG.setAttribute("class", "balloon-string-svg");
stringSVG.setAttribute("width", "20");
stringSVG.setAttribute("height", "60");
stringSVG.setAttribute("viewBox", "0 0 20 60");
const path = document.createElementNS(svgNS, "path");
path.setAttribute("d", "M10 0 C8 10, 12 20, 10 30 C8 40, 12 50, 10 60");
path.setAttribute("stroke", "#444");
path.setAttribute("stroke-width", "2");
path.setAttribute("fill", "none");
stringSVG.appendChild(path);
floatGroup.appendChild(swatch);
floatGroup.appendChild(stringSVG);
swatchWrapper.appendChild(floatGroup);
const colorName = document.createElement('span');
colorName.classList.add('color-name', 'highlighted-name');
colorName.textContent = color.name;
swatchWrapper.appendChild(colorName);
// Click to remove a color from the selected palette
swatch.addEventListener('click', () => {
selectedPalette = selectedPalette.filter(c => c.hex !== color.hex);
renderSelectedPalette();
updateSwatchHighlights();
});
paletteColorsContainer.appendChild(swatchWrapper);
});
// Save the state to Local Storage whenever the palette is re-rendered
savePaletteToLocalStorage();
}
/**
* Updates the visual highlight on the main color swatches to show which are selected.
*/
function updateSwatchHighlights() {
const allSwatches = document.querySelectorAll('#color-families .color-swatch');
allSwatches.forEach(swatch => {
const color = swatch.dataset.color;
const background = swatch.querySelector('.color-background');
const nameEl = swatch.parentElement.querySelector('.color-name');
const isSelected = selectedPalette.some(c => c.hex === color);
if (isSelected) {
background.classList.add('chosen');
nameEl.classList.add('highlighted-name');
} else {
background.classList.remove('chosen');
nameEl.classList.remove('highlighted-name');
}
});
}
/**
* Determines if a hex color is light or dark to decide on border visibility.
* @param {string} hex - The hex color code (e.g., "#FFFFFF").
* @returns {boolean} - True if the color is light, false otherwise.
*/
function isLightColor(hex) {
const rgb = hex.replace('#', '').match(/.{1,2}/g).map(x => parseInt(x, 16));
const brightness = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;
return brightness > 220;
}
/**
* Shuffles an array in place.
* @param {Array} array - The array to shuffle.
* @returns {Array} - The shuffled array.
*/
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
/**
* Checks the URL for a 'colors' parameter and loads the corresponding palette.
* @param {Array} allColorData - The entire array of color families from colors.json.
*/
function loadPaletteFromURL(allColorData) {
const params = new URLSearchParams(window.location.search);
const colorsFromURL = params.get('colors');
if (colorsFromURL) {
const flatColorList = allColorData.flatMap(family => family.colors);
const hexCodesFromURL = colorsFromURL.split(',');
// Find the full color object for each hex code in the URL
const paletteFromURL = hexCodesFromURL.map(hex => {
// The URL stores hex codes without the '#', so we add it back for the comparison
return flatColorList.find(color => color.hex === `#${hex}`);
}).filter(Boolean); // Filter out any invalid colors
// If we found valid colors from the URL, it overrides Local Storage
if (paletteFromURL.length > 0) {
selectedPalette = paletteFromURL;
renderSelectedPalette();
updateSwatchHighlights();
}
}
}
// --- Event Listeners for Palette Controls ---
document.getElementById('clear-palette').addEventListener('click', () => {
selectedPalette = [];
renderSelectedPalette();
updateSwatchHighlights();
});
document.getElementById('toggle-animation').addEventListener('click', () => {
animationsEnabled = !animationsEnabled;
renderSelectedPalette();
});
document.getElementById('shuffle-palette').addEventListener('click', () => {
selectedPalette = shuffleArray(selectedPalette);
renderSelectedPalette();
updateSwatchHighlights();
});
// --- Modal Functionality ---
const shareButton = document.getElementById('share-palette');
const modalBackdrop = document.querySelector('.modal-backdrop');
const closeModalButton = document.getElementById('close-modal');
const modalColorList = document.getElementById('modal-color-list');
shareButton.addEventListener('click', () => {
if (selectedPalette.length === 0) {
alert("Your palette is empty! Add some colors to create a shareable link.");
return;
}
const baseURL = window.location.href.split('?')[0];
const colorParams = selectedPalette.map(color => color.hex.substring(1)).join(',');
const shareableLink = `${baseURL}?colors=${colorParams}`;
modalColorList.innerHTML = `