"use client" import { useState } from "react" import { Copy } from "lucide-react" interface ColorSwatchProps { color: string name: string variable: string showCopy?: boolean } export function ColorSwatch({ color, name, variable, showCopy = true }: ColorSwatchProps) { const [copied, setCopied] = useState(false) // Function to determine if a color is light or dark const isLightColor = (hexColor: string) => { // Remove the hash if it exists const hex = hexColor.replace("#", "") // Convert hex to RGB const r = Number.parseInt(hex.substr(0, 2), 16) const g = Number.parseInt(hex.substr(2, 2), 16) const b = Number.parseInt(hex.substr(4, 2), 16) // Calculate brightness (perceived) const brightness = (r * 299 + g * 587 + b * 114) / 1000 // Return true if the color is light, false if it's dark return brightness > 128 } const copyToClipboard = () => { if (!showCopy) return navigator.clipboard.writeText(variable) setCopied(true) setTimeout(() => setCopied(false), 2000) } // Determine text color based on background color const textColor = isLightColor(color) ? "text-gray-900" : "text-white" return (
{name} {showCopy && ( )}
{variable}
{copied && (
Copied!
)}
) }