// UUID Generator
function generateUUID() {
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
document.getElementById('uuid-output').value = uuid;
}
// Timestamp Converter
function convertTimestamp() {
const input = document.getElementById('timestamp-input').value.trim();
const output = document.getElementById('timestamp-output');
try {
if (input === '') {
const now = new Date();
const timestamp = Math.floor(now.getTime() / 1000);
output.innerHTML = `
Current timestamp: ${timestamp}
Current time: ${now.toLocaleString('en-US')}
ISO format: ${now.toISOString()}
`;
} else {
let date;
if (/^\d{10}$/.test(input)) {
date = new Date(parseInt(input) * 1000);
} else if (/^\d{13}$/.test(input)) {
date = new Date(parseInt(input));
} else {
date = new Date(input);
}
if (isNaN(date.getTime())) {
throw new Error('Invalid time format');
}
const timestamp10 = Math.floor(date.getTime() / 1000);
const timestamp13 = date.getTime();
output.innerHTML = `
10-digit timestamp: ${timestamp10}
13-digit timestamp: ${timestamp13}
Formatted time: ${date.toLocaleString('en-US')}
ISO format: ${date.toISOString()}
`;
}
} catch (error) {
output.innerHTML = `Error: ${error.message}
`;
}
}
// Base64 Encoder/Decoder
function base64Encode() {
const input = document.getElementById('base64-input').value;
const output = document.getElementById('base64-output');
try {
const encoded = btoa(unescape(encodeURIComponent(input)));
output.value = encoded;
} catch (error) {
output.value = 'Encoding failed: ' + error.message;
}
}
function base64Decode() {
const input = document.getElementById('base64-input').value;
const output = document.getElementById('base64-output');
try {
const decoded = decodeURIComponent(escape(atob(input)));
output.value = decoded;
} catch (error) {
output.value = 'Decoding failed: ' + error.message;
}
}
// Random Password Generator
function generatePassword() {
const length = parseInt(document.getElementById('password-length').value);
const includeUppercase = document.getElementById('include-uppercase').checked;
const includeLowercase = document.getElementById('include-lowercase').checked;
const includeNumbers = document.getElementById('include-numbers').checked;
const includeSymbols = document.getElementById('include-symbols').checked;
let charset = '';
if (includeUppercase) charset += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (includeLowercase) charset += 'abcdefghijklmnopqrstuvwxyz';
if (includeNumbers) charset += '0123456789';
if (includeSymbols) charset += '!@#$%^&*()_+-=[]{}|;:,.<>?';
if (charset === '') {
document.getElementById('password-output').value = 'Please select at least one character type';
return;
}
let password = '';
for (let i = 0; i < length; i++) {
password += charset.charAt(Math.floor(Math.random() * charset.length));
}
document.getElementById('password-output').value = password;
}
// JSON Formatter
function formatJSON() {
const input = document.getElementById('json-input').value;
const output = document.getElementById('json-output');
try {
const parsed = JSON.parse(input);
const formatted = JSON.stringify(parsed, null, 2);
output.value = formatted;
} catch (error) {
output.value = 'JSON format error: ' + error.message;
}
}
function minifyJSON() {
const input = document.getElementById('json-input').value;
const output = document.getElementById('json-output');
try {
const parsed = JSON.parse(input);
const minified = JSON.stringify(parsed);
output.value = minified;
} catch (error) {
output.value = 'JSON format error: ' + error.message;
}
}
// Color Code Converter
function convertColor() {
const colorPicker = document.getElementById('color-picker');
const colorInput = document.getElementById('color-input');
const output = document.getElementById('color-output');
let color = colorInput.value.trim() || colorPicker.value;
try {
// Handle different color input formats
if (color.startsWith('#')) {
// HEX format
const hex = color;
const rgb = hexToRgb(hex);
const hsl = rgbToHsl(rgb.r, rgb.g, rgb.b);
output.innerHTML = `
HEX: ${hex.toUpperCase()}
RGB: rgb(${rgb.r}, ${rgb.g}, ${rgb.b})
RGBA: rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, 1)
HSL: hsl(${hsl.h}, ${hsl.s}%, ${hsl.l}%)
`;
colorPicker.value = hex;
} else if (color.startsWith('rgb')) {
// RGB format
const rgbMatch = color.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
if (rgbMatch) {
const r = parseInt(rgbMatch[1]);
const g = parseInt(rgbMatch[2]);
const b = parseInt(rgbMatch[3]);
const hex = rgbToHex(r, g, b);
const hsl = rgbToHsl(r, g, b);
output.innerHTML = `
HEX: ${hex.toUpperCase()}
RGB: rgb(${r}, ${g}, ${b})
RGBA: rgba(${r}, ${g}, ${b}, 1)
HSL: hsl(${hsl.h}, ${hsl.s}%, ${hsl.l}%)
`;
colorPicker.value = hex;
} else {
throw new Error('Invalid RGB format');
}
} else {
throw new Error('Color format not supported');
}
} catch (error) {
output.innerHTML = `Error: ${error.message}
`;
}
}
// Color conversion helper functions
function hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
function rgbToHsl(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max === min) {
h = s = 0;
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return {
h: Math.round(h * 360),
s: Math.round(s * 100),
l: Math.round(l * 100)
};
}
// Copy to clipboard
function copyToClipboard(elementId) {
const element = document.getElementById(elementId);
if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
element.select();
element.setSelectionRange(0, 99999);
} else {
const textArea = document.createElement('textarea');
textArea.value = element.textContent || element.innerHTML.replace(/<[^>]*>/g, '');
document.body.appendChild(textArea);
textArea.select();
textArea.setSelectionRange(0, 99999);
}
try {
document.execCommand('copy');
showCopyFeedback();
} catch (err) {
console.error('Copy failed: ', err);
}
if (element.tagName !== 'INPUT' && element.tagName !== 'TEXTAREA') {
document.body.removeChild(document.querySelector('textarea:last-child'));
}
}
// Copy feedback
function showCopyFeedback() {
const feedback = document.createElement('div');
feedback.textContent = 'Copied to clipboard!';
feedback.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: #28a745;
color: white;
padding: 10px 20px;
border-radius: 8px;
z-index: 1000;
animation: slideIn 0.3s ease-out;
`;
document.body.appendChild(feedback);
setTimeout(() => {
feedback.remove();
}, 2000);
}
// Add animation styles
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
`;
document.head.appendChild(style);
// Auto-update when color picker changes
document.getElementById('color-picker').addEventListener('change', function() {
document.getElementById('color-input').value = this.value;
convertColor();
});
// Initialize color conversion on page load
document.addEventListener('DOMContentLoaded', function() {
convertColor();
});