diff --git a/src/views/ColorConverter.vue b/src/views/ColorConverter.vue index 97ed1b5..f221da4 100644 --- a/src/views/ColorConverter.vue +++ b/src/views/ColorConverter.vue @@ -257,7 +257,13 @@ const currentColor = computed(() => { return `rgb(${rgb.value.r}, ${rgb.value.g}, ${rgb.value.b})` }) -// RGB转十六进制 +/** + * RGB转十六进制 + * @param {number} r - 红色值 (0-255) + * @param {number} g - 绿色值 (0-255) + * @param {number} b - 蓝色值 (0-255) + * @returns {string} 十六进制颜色值(不含#号) + */ function rgbToHex(r, g, b) { const toHex = (n) => { const hex = Math.round(Math.max(0, Math.min(255, n))).toString(16) @@ -266,7 +272,13 @@ function rgbToHex(r, g, b) { return (toHex(r) + toHex(g) + toHex(b)).toUpperCase() } -// RGB转HSL +/** + * RGB转HSL + * @param {number} r - 红色值 (0-255) + * @param {number} g - 绿色值 (0-255) + * @param {number} b - 蓝色值 (0-255) + * @returns {{h: number, s: number, l: number}} HSL对象 + */ function rgbToHsl(r, g, b) { r /= 255 g /= 255 @@ -302,7 +314,11 @@ function rgbToHsl(r, g, b) { } } -// 十六进制转RGB +/** + * 十六进制转RGB + * @param {string} hex - 十六进制颜色值(不含#号) + * @returns {{r: number, g: number, b: number}|null} RGB对象或null + */ function hexToRgb(hex) { const result = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) return result @@ -314,7 +330,13 @@ function hexToRgb(hex) { : null } -// HSL转RGB +/** + * HSL转RGB + * @param {number} h - 色相 (0-360) + * @param {number} s - 饱和度 (0-100) + * @param {number} l - 亮度 (0-100) + * @returns {{r: number, g: number, b: number}} RGB对象 + */ function hslToRgb(h, s, l) { h /= 360 s /= 100 @@ -568,7 +590,6 @@ async function copyRgb() { await navigator.clipboard.writeText(rgbText) showToast('RGB已复制到剪贴板', 'info', 2000) } catch (err) { - console.error('复制失败:', err) showToast('复制失败:' + err.message) } } @@ -651,7 +672,6 @@ async function pasteRgb() { const text = await navigator.clipboard.readText() processPastedText(text, 'rgb') } catch (err) { - console.error('粘贴失败:', err) // Clipboard API 失败,使用备用方法 currentPasteType.value = 'rgb' pasteInputValue.value = '' @@ -682,7 +702,6 @@ async function copyHex() { await navigator.clipboard.writeText(hexText) showToast('十六进制已复制到剪贴板', 'info', 2000) } catch (err) { - console.error('复制失败:', err) showToast('复制失败:' + err.message) } } @@ -695,7 +714,6 @@ async function pasteHex() { const text = await navigator.clipboard.readText() processPastedText(text, 'hex') } catch (err) { - console.error('粘贴失败:', err) // Clipboard API 失败,使用备用方法 currentPasteType.value = 'hex' pasteInputValue.value = '' @@ -726,7 +744,6 @@ async function copyHsl() { await navigator.clipboard.writeText(hslText) showToast('HSL已复制到剪贴板', 'info', 2000) } catch (err) { - console.error('复制失败:', err) showToast('复制失败:' + err.message) } } @@ -739,7 +756,6 @@ async function pasteHsl() { const text = await navigator.clipboard.readText() processPastedText(text, 'hsl') } catch (err) { - console.error('粘贴失败:', err) // Clipboard API 失败,使用备用方法 currentPasteType.value = 'hsl' pasteInputValue.value = '' @@ -779,7 +795,10 @@ function handlePasteEvent(event) { } } -// 保存到历史记录 +/** + * 保存当前颜色到历史记录 + * 自动去重,最多保存50条记录 + */ function saveToHistory() { const colorData = { rgb: `rgb(${rgb.value.r}, ${rgb.value.g}, ${rgb.value.b})`, @@ -803,7 +822,7 @@ function saveToHistory() { history = JSON.parse(stored) } } catch (e) { - console.error('读取历史记录失败', e) + // 读取历史记录失败,忽略错误 } // 检查是否与最后一条历史记录相同 @@ -828,11 +847,13 @@ function saveToHistory() { localStorage.setItem(STORAGE_KEY, JSON.stringify(history)) loadHistoryList() } catch (e) { - console.error('保存历史记录失败', e) + // 保存历史记录失败,忽略错误 } } -// 加载历史记录列表 +/** + * 从localStorage加载历史记录列表 + */ function loadHistoryList() { try { const stored = localStorage.getItem(STORAGE_KEY) @@ -840,12 +861,15 @@ function loadHistoryList() { historyList.value = JSON.parse(stored) } } catch (e) { - console.error('加载历史记录失败', e) + // 加载历史记录失败,重置为空数组 historyList.value = [] } } -// 加载历史记录 +/** + * 加载历史记录到编辑器 + * @param {Object} colorData - 颜色数据对象 + */ function loadHistory(colorData) { updating.value = true rgb.value = { ...colorData.rgbValues } @@ -860,7 +884,11 @@ function toggleSidebar() { sidebarOpen.value = !sidebarOpen.value } -// 格式化时间 +/** + * 格式化时间戳为相对时间或日期字符串 + * @param {number} timestamp - 时间戳(毫秒) + * @returns {string} 格式化后的时间字符串 + */ function formatTime(timestamp) { const date = new Date(timestamp) const now = new Date() diff --git a/src/views/Comparator.vue b/src/views/Comparator.vue index 2b1ffd7..a18c3ba 100644 --- a/src/views/Comparator.vue +++ b/src/views/Comparator.vue @@ -274,7 +274,7 @@