color_convert
Convert color values between HEX, RGB, and HSL formats for web development and design workflows.
Instructions
Convert colors between HEX, RGB, and HSL formats.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| color | Yes | Color value (e.g., '#FF5733', 'rgb(255,87,51)', 'hsl(11,100%,60%)') |
Implementation Reference
- src/tools/converters.ts:165-268 (handler)The 'color_convert' tool implementation, including registration, input validation schema, and the handler function that processes the conversion logic.
server.tool( "color_convert", "Convert colors between HEX, RGB, and HSL formats.", { color: z .string() .describe( "Color value (e.g., '#FF5733', 'rgb(255,87,51)', 'hsl(11,100%,60%)')" ), }, async ({ color }) => { try { let r: number, g: number, b: number; const hexMatch = color.match( /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i ); const rgbMatch = color.match( /rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/i ); const hslMatch = color.match( /hsl\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)/i ); if (hexMatch) { r = parseInt(hexMatch[1], 16); g = parseInt(hexMatch[2], 16); b = parseInt(hexMatch[3], 16); } else if (rgbMatch) { r = parseInt(rgbMatch[1]); g = parseInt(rgbMatch[2]); b = parseInt(rgbMatch[3]); } else if (hslMatch) { const h = parseInt(hslMatch[1]) / 360; const s = parseInt(hslMatch[2]) / 100; const l = parseInt(hslMatch[3]) / 100; if (s === 0) { r = g = b = Math.round(l * 255); } else { const hue2rgb = (p: number, q: number, t: number) => { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; }; const q = l < 0.5 ? l * (1 + s) : l + s - l * s; const p = 2 * l - q; r = Math.round(hue2rgb(p, q, h + 1 / 3) * 255); g = Math.round(hue2rgb(p, q, h) * 255); b = Math.round(hue2rgb(p, q, h - 1 / 3) * 255); } } else { return { content: [ { type: "text" as const, text: "Error: Unrecognized color format. Use HEX (#FF5733), RGB (rgb(255,87,51)), or HSL (hsl(11,100%,60%)).", }, ], isError: true, }; } // RGB to HSL const rn = r / 255, gn = g / 255, bn = b / 255; const max = Math.max(rn, gn, bn), min = Math.min(rn, gn, bn); const l = (max + min) / 2; let h = 0, s = 0; if (max !== min) { const d = max - min; s = l > 0.5 ? d / (2 - max - min) : d / (max + min); switch (max) { case rn: h = ((gn - bn) / d + (gn < bn ? 6 : 0)) / 6; break; case gn: h = ((bn - rn) / d + 2) / 6; break; case bn: h = ((rn - gn) / d + 4) / 6; break; } } const result = { hex: `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`.toUpperCase(), rgb: `rgb(${r}, ${g}, ${b})`, hsl: `hsl(${Math.round(h * 360)}, ${Math.round(s * 100)}%, ${Math.round(l * 100)}%)`, r, g, b, h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100), }; return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2) }, ], }; } catch (e) { return { content: [ { type: "text" as const, text: `Error: ${e instanceof Error ? e.message : String(e)}`, },