index.ts•7.66 kB
import { server } from "../index.js";
import { z } from "zod";
import fetch from "node-fetch";
// Helper function to detect color format and prepare API parameters
function parseColorInput(color: string): { param: string; value: string } {
const cleanColor = color.trim();
// Check for hex format
if (cleanColor.startsWith("#")) {
return { param: "hex", value: cleanColor.substring(1) };
} else if (/^[0-9A-Fa-f]{6}$/.test(cleanColor)) {
return { param: "hex", value: cleanColor };
}
// Check for RGB format
if (
cleanColor.toLowerCase().includes("rgb") ||
/^\d+,\d+,\d+$/.test(cleanColor)
) {
const rgbMatch = cleanColor.match(/(\d+),\s*(\d+),\s*(\d+)/);
if (rgbMatch) {
return {
param: "rgb",
value: `${rgbMatch[1]},${rgbMatch[2]},${rgbMatch[3]}`,
};
}
}
// Check for HSL format
if (cleanColor.toLowerCase().includes("hsl") || cleanColor.includes("%")) {
const hslMatch = cleanColor.match(/(\d+),\s*(\d+)%,\s*(\d+)%/);
if (hslMatch) {
return {
param: "hsl",
value: `${hslMatch[1]},${hslMatch[2]}%,${hslMatch[3]}%`,
};
}
}
// Default to hex if format is unclear
return { param: "hex", value: cleanColor.replace("#", "") };
}
// Helper function to call The Color API
async function generateColorScheme(
color: string,
mode: string,
count: number = 5
) {
const { param, value } = parseColorInput(color);
const url = `https://www.thecolorapi.com/scheme?${param}=${value}&mode=${mode}&count=${count}&format=json`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Color API request failed: ${response.status} ${response.statusText}`
);
}
const data: any = await response.json();
if (!data.colors || !Array.isArray(data.colors)) {
throw new Error("Invalid response from Color API");
}
// Format the response for better readability
const colors = data.colors.map((color: any, index: number) => ({
position: index + 1,
hex: color.hex?.value || "N/A",
rgb: color.rgb
? `rgb(${color.rgb.r}, ${color.rgb.g}, ${color.rgb.b})`
: "N/A",
hsl: color.hsl
? `hsl(${color.hsl.h}, ${color.hsl.s}%, ${color.hsl.l}%)`
: "N/A",
name: color.name?.value || "Unknown",
}));
return {
scheme_mode: mode,
seed_color: data.seed?.hex?.value || value,
color_count: colors.length,
colors: colors,
};
} catch (error) {
console.error(`Error generating ${mode} color scheme:`, error);
throw error;
}
}
// Common input schema for all color scheme tools
const colorSchemeInputShape = {
color: z
.string()
.describe(
"The seed color in hex (098765), RGB (0,71,171), or HSL (215,100%,34%) format"
),
count: z
.number()
.int()
.min(3)
.max(10)
.default(5)
.optional()
.describe("Number of colors to generate (3-10, default: 5)"),
};
// Tool 1: Generate Monochrome Scheme
function registerMonochromeScheme() {
server.tool(
"generate_monochrome_scheme",
"Generates a monochrome color scheme with variations of a single hue",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(color, "monochrome", count);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 2: Generate Monochrome Dark Scheme
function registerMonochromeDarkScheme() {
server.tool(
"generate_monochrome_dark_scheme",
"Generates a dark monochrome color scheme",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(color, "monochrome-dark", count);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 3: Generate Monochrome Light Scheme
function registerMonochromeLightScheme() {
server.tool(
"generate_monochrome_light_scheme",
"Generates a light monochrome color scheme",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(
color,
"monochrome-light",
count
);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 4: Generate Analogic Scheme
function registerAnalogicScheme() {
server.tool(
"generate_analogic_scheme",
"Generates an analogic color scheme with adjacent colors on the color wheel",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(color, "analogic", count);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 5: Generate Complement Scheme
function registerComplementScheme() {
server.tool(
"generate_complement_scheme",
"Generates a complementary color scheme with opposite colors on the color wheel",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(color, "complement", count);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 6: Generate Analogic Complement Scheme
function registerAnalogicComplementScheme() {
server.tool(
"generate_analogic_complement_scheme",
"Generates an analogic complement color scheme",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(
color,
"analogic-complement",
count
);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 7: Generate Triad Scheme
function registerTriadScheme() {
server.tool(
"generate_triad_scheme",
"Generates a triad color scheme with three evenly spaced colors on the color wheel",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(color, "triad", count);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Tool 8: Generate Quad Scheme
function registerQuadScheme() {
server.tool(
"generate_quad_scheme",
"Generates a quad color scheme with four evenly spaced colors on the color wheel",
colorSchemeInputShape,
async (args) => {
const { color, count } = args;
const result = await generateColorScheme(color, "quad", count);
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
}
);
}
// Function called by src/index.ts to register all tools for this server
export function registerTools() {
registerMonochromeScheme();
registerMonochromeDarkScheme();
registerMonochromeLightScheme();
registerAnalogicScheme();
registerComplementScheme();
registerAnalogicComplementScheme();
registerTriadScheme();
registerQuadScheme();
}