daily-color 2.3.19 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -1
- package/bin/daily-color-mcp.js +458 -0
- package/bin/dailycolor.js +90 -2
- package/dist/daily-color.js +1 -0
- package/dist/daily-color.umd.js +1 -0
- package/index.d.ts +42 -0
- package/index.js +12 -0
- package/package.json +13 -4
- package/palette.js +161 -0
package/README.md
CHANGED
|
@@ -21,6 +21,32 @@ console.log("Today's color is:", getDailyColor());
|
|
|
21
21
|
console.log("Another day's color is:", getDailyColor(new Date("2024-11-09")));
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
Colors that go with the day's color, worked out in OKLCH so they keep its lightness and strength (on gray days they get a little extra strength, so there's always some color):
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import { getDailyPalette, getPalette } from "daily-color";
|
|
28
|
+
|
|
29
|
+
const palette = getDailyPalette(); // or getDailyPalette(new Date("2024-11-09"))
|
|
30
|
+
palette.color; // the day's color, e.g. "#7ad2f9"
|
|
31
|
+
palette.contrast; // "#000000" or "#ffffff", whichever reads better on it
|
|
32
|
+
palette.complementary; // "#ffb086"
|
|
33
|
+
palette.splitComplementary; // ["#ffabac", "#ecbc6f"]
|
|
34
|
+
palette.analogous; // ["#5edbdf", "#a2c7ff"]
|
|
35
|
+
palette.triadic; // ["#fba8d1", "#cbcb74"]
|
|
36
|
+
palette.tetradic; // ["#e4aef3", "#ffb086", "#a1d691"]
|
|
37
|
+
|
|
38
|
+
getPalette("#3fa2c1"); // the same, for any color
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Black or white text, whichever is easier to read on a color:
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { contrastColor } from "daily-color";
|
|
45
|
+
|
|
46
|
+
contrastColor("#7ad2f9"); // "#000000"
|
|
47
|
+
contrastColor("#1935a4"); // "#ffffff"
|
|
48
|
+
```
|
|
49
|
+
|
|
24
50
|
Directly loading the bundled JavaScript, for static projects not using NPM:
|
|
25
51
|
|
|
26
52
|
```html
|
|
@@ -32,10 +58,93 @@ Directly loading the bundled JavaScript, for static projects not using NPM:
|
|
|
32
58
|
</script>
|
|
33
59
|
```
|
|
34
60
|
|
|
61
|
+
Or as a plain script, which sets `window.DailyColor`. Plain scripts in `<head>` run before the page is drawn, so the color is there from the very first frame. Pin a version from jsDelivr so it never changes under you:
|
|
62
|
+
|
|
63
|
+
```html
|
|
64
|
+
<script src="https://cdn.jsdelivr.net/npm/daily-color@3/dist/daily-color.umd.js"></script>
|
|
65
|
+
<script>
|
|
66
|
+
document.documentElement.style.setProperty("--day", DailyColor.getDailyColor());
|
|
67
|
+
</script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Building your own colors
|
|
71
|
+
|
|
72
|
+
The color tools behind the palette are there too, for making a whole theme from the day's color:
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
import { contrastRatio, getDailyColor, oklch, readableOn, toOklch } from "daily-color";
|
|
76
|
+
|
|
77
|
+
const { l, c, h } = toOklch(getDailyColor()); // lightness 0–1, chroma 0–0.37, hue 0–360°
|
|
78
|
+
const pale = oklch(0.97, 0.02, h); // a background faintly tinted with the day's hue
|
|
79
|
+
const link = readableOn(getDailyColor(), pale); // darker or lighter until it reads at 4.5:1
|
|
80
|
+
contrastRatio(link, pale); // 4.5 or more
|
|
81
|
+
|
|
82
|
+
readableOn(getDailyColor(), "#ffffff", 7); // ask for more contrast
|
|
83
|
+
readableOn({ l: 0.9, c: 0.25, h }, "#ffffff"); // keep chroma that only fits once it's darker
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`oklch()` brings the chroma down when a color doesn't fit in sRGB, so its lightness and hue stay true. `readableOn()` keeps the hue and chroma and only moves the lightness: darker on a light background, lighter on a dark one.
|
|
87
|
+
|
|
35
88
|
Using the CLI:
|
|
36
89
|
|
|
37
90
|
```bash
|
|
38
|
-
dailycolor
|
|
91
|
+
dailycolor # today's color
|
|
92
|
+
dailycolor complementary # the color opposite it
|
|
93
|
+
dailycolor triadic analogous # several, labeled
|
|
94
|
+
dailycolor all --date 2027-01-01 # everything, for another day
|
|
95
|
+
dailycolor --help # all the options
|
|
96
|
+
dailycolor --version
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### For your AI
|
|
100
|
+
|
|
101
|
+
daily-color comes with an [MCP](https://modelcontextprotocol.io) server, so an AI assistant can look up the color of any day and the colors that go with it. It has:
|
|
102
|
+
|
|
103
|
+
- A tool, `get_daily_colors`, which the AI uses when it wants colors. It takes an optional `date` (like `2027-01-01`, today if left out) and `days` (up to 31 days in a row).
|
|
104
|
+
- Resources, which you can attach to a conversation yourself: `daily-color://today`, or any day, like `daily-color://2027-01-01`. Apps that subscribe to `daily-color://today` hear when it changes at midnight.
|
|
105
|
+
- A prompt, `theme_from_daily_color`, which asks for a website color theme made from a day's colors. It takes an optional `date`, and `mode` (`light` or `dark`, or leave it out for both). In Claude Code it's the `/mcp__daily-color__theme_from_daily_color` command.
|
|
106
|
+
|
|
107
|
+
Apps that support it can suggest dates and modes as you type them.
|
|
108
|
+
|
|
109
|
+
In Claude Code:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
claude mcp add daily-color -- npx -y -p daily-color daily-color-mcp
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
In Claude Desktop (Settings → Developer → Edit Config) or Cursor (`~/.cursor/mcp.json`):
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{
|
|
119
|
+
"mcpServers": {
|
|
120
|
+
"daily-color": {
|
|
121
|
+
"command": "npx",
|
|
122
|
+
"args": ["-y", "-p", "daily-color", "daily-color-mcp"]
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
In VS Code (`.vscode/mcp.json`):
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"servers": {
|
|
133
|
+
"daily-color": {
|
|
134
|
+
"type": "stdio",
|
|
135
|
+
"command": "npx",
|
|
136
|
+
"args": ["-y", "-p", "daily-color", "daily-color-mcp"]
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Then ask something like "What's today's color, and what goes with it?" or "Give me the colors for next week".
|
|
143
|
+
|
|
144
|
+
To try the server out by hand, open it in the MCP Inspector:
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
npx @modelcontextprotocol/inspector node bin/daily-color-mcp.js
|
|
39
148
|
```
|
|
40
149
|
|
|
41
150
|
Just want to see today's color?
|
|
@@ -54,6 +163,19 @@ Install dependencies:
|
|
|
54
163
|
npm install
|
|
55
164
|
```
|
|
56
165
|
|
|
166
|
+
See the demo page at [localhost:6766](http://localhost:6766):
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
npm run dev
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Make the pictures in the day's color: the one that shows when someone shares the page, and the home screen icons for phones. It needs Chrome, and the site makes new ones every day when it deploys:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
npm run images # today's
|
|
176
|
+
npm run images -- --date 2027-01-01 # another day's
|
|
177
|
+
```
|
|
178
|
+
|
|
57
179
|
Run tests:
|
|
58
180
|
|
|
59
181
|
```bash
|
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// An MCP (Model Context Protocol) server that lets an AI look up daily colors.
|
|
4
|
+
//
|
|
5
|
+
// MCP is JSON-RPC 2.0. With the stdio transport, the AI app starts this file
|
|
6
|
+
// as a child process and the two talk over stdin and stdout, one JSON message
|
|
7
|
+
// per line. Anything else printed to stdout would break the conversation, so
|
|
8
|
+
// logs go to stderr.
|
|
9
|
+
//
|
|
10
|
+
// A session goes like this:
|
|
11
|
+
// 1. The client sends "initialize". We agree on a protocol version and say
|
|
12
|
+
// what we can do (our "capabilities": here, just tools).
|
|
13
|
+
// 2. The client sends "notifications/initialized". Notifications have no
|
|
14
|
+
// id and never get a reply.
|
|
15
|
+
// 3. The client asks "tools/list" to learn what tools there are, and shows
|
|
16
|
+
// their names, descriptions and input schemas to the model.
|
|
17
|
+
// 4. When the model wants a tool, the client sends "tools/call".
|
|
18
|
+
//
|
|
19
|
+
// Tools are one of three things a server can offer:
|
|
20
|
+
// - Tools are for the model: it decides when to call them.
|
|
21
|
+
// - Resources are for the app: data it can read by URI and put in front of
|
|
22
|
+
// the model, like attaching a file. Here, daily-color://2027-01-01.
|
|
23
|
+
// - Prompts are for the user: ready-made requests they pick from a menu,
|
|
24
|
+
// often as a slash command. Here, a theme made from the day's colors.
|
|
25
|
+
//
|
|
26
|
+
// Most messages are requests from the client with a reply from us. Two
|
|
27
|
+
// things here go further:
|
|
28
|
+
// - Subscriptions: the client can subscribe to a resource, and we send it
|
|
29
|
+
// a notification when that resource changes. daily-color://today changes
|
|
30
|
+
// at midnight.
|
|
31
|
+
// - Completions: while the user types a prompt argument or fills in a
|
|
32
|
+
// resource template, the client can ask us for suggestions.
|
|
33
|
+
|
|
34
|
+
import { readFileSync } from "node:fs";
|
|
35
|
+
import { createInterface } from "node:readline";
|
|
36
|
+
|
|
37
|
+
import { getDailyPalette } from "../index.js";
|
|
38
|
+
|
|
39
|
+
const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
40
|
+
|
|
41
|
+
// Newest first. We answer in the client's version when we know it, and
|
|
42
|
+
// otherwise offer our newest and let the client decide whether to carry on.
|
|
43
|
+
const PROTOCOL_VERSIONS = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"];
|
|
44
|
+
|
|
45
|
+
const MAX_DAYS = 31;
|
|
46
|
+
const MAX_COMPLETIONS = 100; // the most the protocol allows in one reply
|
|
47
|
+
|
|
48
|
+
// The tests set this to see what happens at midnight without waiting for it
|
|
49
|
+
const CLOCK_OFFSET = process.env.DAILY_COLOR_MCP_NOW ? new Date(process.env.DAILY_COLOR_MCP_NOW) - Date.now() : 0;
|
|
50
|
+
const now = () => new Date(Date.now() + CLOCK_OFFSET);
|
|
51
|
+
|
|
52
|
+
// Standard JSON-RPC error codes
|
|
53
|
+
const PARSE_ERROR = -32700;
|
|
54
|
+
const INVALID_REQUEST = -32600;
|
|
55
|
+
const METHOD_NOT_FOUND = -32601;
|
|
56
|
+
const INVALID_PARAMS = -32602;
|
|
57
|
+
const RESOURCE_NOT_FOUND = -32002; // MCP's own code, for resources/read
|
|
58
|
+
|
|
59
|
+
const HEX = { type: "string", pattern: "^#[0-9a-f]{6}$" };
|
|
60
|
+
const PAIR = { type: "array", items: HEX, minItems: 2, maxItems: 2 };
|
|
61
|
+
|
|
62
|
+
const DAY_SCHEMA = {
|
|
63
|
+
type: "object",
|
|
64
|
+
properties: {
|
|
65
|
+
date: { type: "string", description: "The day, as YYYY-MM-DD" },
|
|
66
|
+
color: { ...HEX, description: "The day's color" },
|
|
67
|
+
contrast: { ...HEX, description: "Black or white, whichever is easier to read on the day's color" },
|
|
68
|
+
complementary: { ...HEX, description: "Opposite the day's color on the color wheel" },
|
|
69
|
+
splitComplementary: { ...PAIR, description: "Either side of the complementary color" },
|
|
70
|
+
analogous: { ...PAIR, description: "Next to the day's color on the color wheel" },
|
|
71
|
+
triadic: { ...PAIR, description: "A third of the way round the color wheel each way" },
|
|
72
|
+
tetradic: {
|
|
73
|
+
type: "array",
|
|
74
|
+
items: HEX,
|
|
75
|
+
minItems: 3,
|
|
76
|
+
maxItems: 3,
|
|
77
|
+
description: "With the day's color, a square round the color wheel",
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
required: ["date", "color", "contrast", "complementary", "splitComplementary", "analogous", "triadic", "tetradic"],
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// What the model sees. The descriptions matter: they are how the model
|
|
84
|
+
// decides when to use the tool and what to pass it.
|
|
85
|
+
const TOOLS = [
|
|
86
|
+
{
|
|
87
|
+
name: "get_daily_colors",
|
|
88
|
+
title: "Daily colors",
|
|
89
|
+
description:
|
|
90
|
+
"Get the daily color for a date, and the colors that go with it (complementary, split-complementary, " +
|
|
91
|
+
"analogous, triadic and tetradic), as hex codes. Every date has its own color, the same for everyone. " +
|
|
92
|
+
"The matching colors keep the day's color's lightness and strength. Use `days` to get several days in a row.",
|
|
93
|
+
inputSchema: {
|
|
94
|
+
type: "object",
|
|
95
|
+
properties: {
|
|
96
|
+
date: {
|
|
97
|
+
type: "string",
|
|
98
|
+
pattern: "^\\d{4}-\\d{2}-\\d{2}$",
|
|
99
|
+
description: "The day, as YYYY-MM-DD. Leave it out for today.",
|
|
100
|
+
},
|
|
101
|
+
days: {
|
|
102
|
+
type: "integer",
|
|
103
|
+
minimum: 1,
|
|
104
|
+
maximum: MAX_DAYS,
|
|
105
|
+
default: 1,
|
|
106
|
+
description: "How many days to get, starting at `date`",
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
additionalProperties: false,
|
|
110
|
+
},
|
|
111
|
+
outputSchema: {
|
|
112
|
+
type: "object",
|
|
113
|
+
properties: { days: { type: "array", items: DAY_SCHEMA } },
|
|
114
|
+
required: ["days"],
|
|
115
|
+
},
|
|
116
|
+
annotations: { readOnlyHint: true, idempotentHint: true, openWorldHint: false },
|
|
117
|
+
},
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
// Something wrong with what we were asked for, like a date that doesn't
|
|
121
|
+
// exist. Each kind of request reports it in its own way.
|
|
122
|
+
class ArgumentError extends Error {}
|
|
123
|
+
|
|
124
|
+
// One day's colors, the same for the tool, the resources and the prompt
|
|
125
|
+
function colorsFor(day) {
|
|
126
|
+
return { date: formatDate(day), ...getDailyPalette(day) };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function getDailyColors({ date, days = 1, ...rest } = {}) {
|
|
130
|
+
const unknown = Object.keys(rest);
|
|
131
|
+
if (unknown.length) throw new ArgumentError(`Unknown argument: ${unknown.join(", ")}`);
|
|
132
|
+
if (!Number.isInteger(days) || days < 1 || days > MAX_DAYS) {
|
|
133
|
+
throw new ArgumentError(`"days" must be a whole number from 1 to ${MAX_DAYS}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const start = date === undefined ? today() : parseDate(date);
|
|
137
|
+
const result = [];
|
|
138
|
+
for (let i = 0; i < days; i++) {
|
|
139
|
+
result.push(colorsFor(new Date(start.getFullYear(), start.getMonth(), start.getDate() + i)));
|
|
140
|
+
}
|
|
141
|
+
return { days: result };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// "Today" is the day where this server runs, which is where its user is
|
|
145
|
+
function today() {
|
|
146
|
+
const time = now();
|
|
147
|
+
return new Date(time.getFullYear(), time.getMonth(), time.getDate());
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// A day where you are, not UTC midnight. A day that doesn't exist, like
|
|
151
|
+
// 2027-02-30, would roll over into the next month, and Date reads years 0 to
|
|
152
|
+
// 99 as 1900 to 1999, so the day has to come back as it went in.
|
|
153
|
+
function parseDate(value) {
|
|
154
|
+
const m = typeof value === "string" && /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
|
155
|
+
const day = m && new Date(+m[1], +m[2] - 1, +m[3]);
|
|
156
|
+
if (!day || day.getFullYear() !== +m[1] || day.getMonth() !== +m[2] - 1) {
|
|
157
|
+
throw new ArgumentError(`"${value}" isn't a date like 2027-01-01`);
|
|
158
|
+
}
|
|
159
|
+
return day;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function formatDate(day) {
|
|
163
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
164
|
+
return `${day.getFullYear()}-${pad(day.getMonth() + 1)}-${pad(day.getDate())}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function callTool({ name, arguments: args } = {}) {
|
|
168
|
+
if (name !== "get_daily_colors") throw rpcError(INVALID_PARAMS, `Unknown tool: ${name}`);
|
|
169
|
+
try {
|
|
170
|
+
const structured = getDailyColors(args ?? {});
|
|
171
|
+
return {
|
|
172
|
+
// Clients that don't read structuredContent still get the same thing as text
|
|
173
|
+
content: [{ type: "text", text: JSON.stringify(structured, null, 2) }],
|
|
174
|
+
structuredContent: structured,
|
|
175
|
+
};
|
|
176
|
+
} catch (error) {
|
|
177
|
+
// A tool's own failures are results with isError, not protocol errors,
|
|
178
|
+
// so the model sees the message and can try again
|
|
179
|
+
if (!(error instanceof ArgumentError)) throw error;
|
|
180
|
+
return { content: [{ type: "text", text: error.message }], isError: true };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Resources. "today" is listed as it is; any other day comes from the
|
|
185
|
+
// template, which tells the app how to make a URI for itself.
|
|
186
|
+
const RESOURCES = [
|
|
187
|
+
{
|
|
188
|
+
uri: "daily-color://today",
|
|
189
|
+
name: "today",
|
|
190
|
+
title: "Today's colors",
|
|
191
|
+
description: "Today's color and the colors that go with it",
|
|
192
|
+
mimeType: "application/json",
|
|
193
|
+
},
|
|
194
|
+
];
|
|
195
|
+
|
|
196
|
+
const RESOURCE_TEMPLATES = [
|
|
197
|
+
{
|
|
198
|
+
uriTemplate: "daily-color://{date}",
|
|
199
|
+
name: "day",
|
|
200
|
+
title: "A day's colors",
|
|
201
|
+
description: "The color for a date (YYYY-MM-DD) and the colors that go with it",
|
|
202
|
+
mimeType: "application/json",
|
|
203
|
+
},
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
// The day a resource URI is for, or a RESOURCE_NOT_FOUND error
|
|
207
|
+
function resourceDay(uri) {
|
|
208
|
+
const m = /^daily-color:\/\/(.+)$/.exec(uri ?? "");
|
|
209
|
+
try {
|
|
210
|
+
if (!m) throw new ArgumentError(`There's no resource at ${uri}`);
|
|
211
|
+
return m[1] === "today" ? today() : parseDate(m[1]);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (error instanceof ArgumentError) throw rpcError(RESOURCE_NOT_FOUND, error.message);
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function readResource({ uri } = {}) {
|
|
219
|
+
const colors = colorsFor(resourceDay(uri));
|
|
220
|
+
return { contents: [{ uri, mimeType: "application/json", text: JSON.stringify(colors, null, 2) }] };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Subscriptions. Only daily-color://today ever changes, but subscribing to
|
|
224
|
+
// a particular day is allowed too: it just never sends anything.
|
|
225
|
+
const subscriptions = new Set();
|
|
226
|
+
|
|
227
|
+
function subscribe({ uri } = {}) {
|
|
228
|
+
resourceDay(uri); // so a URI that doesn't exist is an error
|
|
229
|
+
subscriptions.add(uri);
|
|
230
|
+
return {};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function unsubscribe({ uri } = {}) {
|
|
234
|
+
subscriptions.delete(uri);
|
|
235
|
+
return {};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Wakes up at midnight, and tells the client today's colors have changed if
|
|
239
|
+
// it subscribed to them. Timers can wake a little early, and a computer that
|
|
240
|
+
// sleeps can make them late, so it checks the date when it wakes, and never
|
|
241
|
+
// sleeps more than an hour at a time.
|
|
242
|
+
let lastDay = formatDate(today());
|
|
243
|
+
|
|
244
|
+
function watchTheDate() {
|
|
245
|
+
const time = now();
|
|
246
|
+
const midnight = new Date(time.getFullYear(), time.getMonth(), time.getDate() + 1);
|
|
247
|
+
const timer = setTimeout(
|
|
248
|
+
() => {
|
|
249
|
+
const day = formatDate(today());
|
|
250
|
+
if (day !== lastDay) {
|
|
251
|
+
lastDay = day;
|
|
252
|
+
if (subscriptions.has("daily-color://today")) {
|
|
253
|
+
send({ jsonrpc: "2.0", method: "notifications/resources/updated", params: { uri: "daily-color://today" } });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
watchTheDate();
|
|
257
|
+
},
|
|
258
|
+
Math.min(midnight - time + 10, 60 * 60 * 1000)
|
|
259
|
+
);
|
|
260
|
+
timer.unref(); // so the server still stops when the client goes away
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Prompts. Their arguments are always strings, filled in by the user.
|
|
264
|
+
const PROMPTS = [
|
|
265
|
+
{
|
|
266
|
+
name: "theme_from_daily_color",
|
|
267
|
+
title: "Theme from the daily color",
|
|
268
|
+
description:
|
|
269
|
+
"Make a website color theme, as CSS custom properties, from a day's color and the colors that go with it",
|
|
270
|
+
arguments: [
|
|
271
|
+
{ name: "date", description: "The day, as YYYY-MM-DD. Leave it out for today.", required: false },
|
|
272
|
+
{ name: "mode", description: '"light", "dark", or leave it out for both', required: false },
|
|
273
|
+
],
|
|
274
|
+
},
|
|
275
|
+
];
|
|
276
|
+
|
|
277
|
+
const MODES = {
|
|
278
|
+
light: "a light theme",
|
|
279
|
+
dark: "a dark theme",
|
|
280
|
+
both: "a light theme, and a dark one inside @media (prefers-color-scheme: dark)",
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
function getPrompt({ name, arguments: args } = {}) {
|
|
284
|
+
if (name !== "theme_from_daily_color") throw rpcError(INVALID_PARAMS, `Unknown prompt: ${name}`);
|
|
285
|
+
// Apps often send "" for an argument the user left blank
|
|
286
|
+
const date = args?.date || undefined;
|
|
287
|
+
const mode = args?.mode || "both";
|
|
288
|
+
if (!MODES[mode]) throw rpcError(INVALID_PARAMS, '"mode" must be "light" or "dark", or left out for both');
|
|
289
|
+
|
|
290
|
+
let day;
|
|
291
|
+
try {
|
|
292
|
+
day = date === undefined ? today() : parseDate(date);
|
|
293
|
+
} catch (error) {
|
|
294
|
+
throw error instanceof ArgumentError ? rpcError(INVALID_PARAMS, error.message) : error;
|
|
295
|
+
}
|
|
296
|
+
const colors = colorsFor(day);
|
|
297
|
+
const uri = `daily-color://${colors.date}`;
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
description: `A theme from the color for ${colors.date}`,
|
|
301
|
+
messages: [
|
|
302
|
+
{
|
|
303
|
+
role: "user",
|
|
304
|
+
content: {
|
|
305
|
+
type: "text",
|
|
306
|
+
text:
|
|
307
|
+
`Make a website color theme from the daily color for ${colors.date}, which is ${colors.color}. ` +
|
|
308
|
+
"It and the colors that go with it are attached.\n\n" +
|
|
309
|
+
`Write it as CSS custom properties on :root, for ${MODES[mode]}: background, surface, text, ` +
|
|
310
|
+
"muted text, primary, accent and border. Keep the day's character, but tint the background " +
|
|
311
|
+
"and surface only faintly with its hue rather than using colors at full strength. Text, muted " +
|
|
312
|
+
"text and primary must each reach at least 4.5:1 contrast with the background (WCAG AA), so " +
|
|
313
|
+
"make colors darker or lighter where they need it. Afterwards, say in a sentence or two which " +
|
|
314
|
+
"of the day's colors went where.",
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
// An embedded resource: the data travels with the prompt, so the
|
|
319
|
+
// model doesn't need to call a tool to see it
|
|
320
|
+
role: "user",
|
|
321
|
+
content: {
|
|
322
|
+
type: "resource",
|
|
323
|
+
resource: { uri, mimeType: "application/json", text: JSON.stringify(colors, null, 2) },
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
],
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// The server's icon, for apps that show one: a dot of today's color, with a
|
|
331
|
+
// faint ring so a pale one still shows. It's sent as a data: URI, so there's
|
|
332
|
+
// nothing to host.
|
|
333
|
+
function icon() {
|
|
334
|
+
const { color, contrast } = getDailyPalette(today());
|
|
335
|
+
const svg =
|
|
336
|
+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><circle cx="16" cy="16" r="14.5" ' +
|
|
337
|
+
`fill="${color}" stroke="${contrast}" stroke-opacity="0.35" stroke-width="2"/></svg>`;
|
|
338
|
+
return {
|
|
339
|
+
src: `data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`,
|
|
340
|
+
mimeType: "image/svg+xml",
|
|
341
|
+
sizes: ["any"],
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const METHODS = {
|
|
346
|
+
initialize: ({ protocolVersion } = {}) => ({
|
|
347
|
+
protocolVersion: PROTOCOL_VERSIONS.includes(protocolVersion) ? protocolVersion : PROTOCOL_VERSIONS[0],
|
|
348
|
+
capabilities: { tools: {}, resources: { subscribe: true }, prompts: {}, completions: {} },
|
|
349
|
+
serverInfo: { name: "daily-color", title: "Daily color", version, icons: [icon()] },
|
|
350
|
+
instructions:
|
|
351
|
+
"Look up the color of any day, and the colors that go with it, with get_daily_colors. " +
|
|
352
|
+
"The same colors are resources at daily-color://YYYY-MM-DD and daily-color://today.",
|
|
353
|
+
}),
|
|
354
|
+
ping: () => ({}),
|
|
355
|
+
"tools/list": () => ({ tools: TOOLS }),
|
|
356
|
+
"tools/call": callTool,
|
|
357
|
+
"resources/list": () => ({ resources: RESOURCES }),
|
|
358
|
+
"resources/templates/list": () => ({ resourceTemplates: RESOURCE_TEMPLATES }),
|
|
359
|
+
"resources/read": readResource,
|
|
360
|
+
"resources/subscribe": subscribe,
|
|
361
|
+
"resources/unsubscribe": unsubscribe,
|
|
362
|
+
"prompts/list": () => ({ prompts: PROMPTS }),
|
|
363
|
+
"prompts/get": getPrompt,
|
|
364
|
+
"completion/complete": getCompletions,
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// Completions. The client tells us what the user has typed so far, and we
|
|
368
|
+
// suggest whole values that start with it.
|
|
369
|
+
const complete = (values, typed) => {
|
|
370
|
+
const matches = [...new Set(values)].filter((value) => value.startsWith(typed));
|
|
371
|
+
return {
|
|
372
|
+
completion: {
|
|
373
|
+
values: matches.slice(0, MAX_COMPLETIONS),
|
|
374
|
+
total: matches.length,
|
|
375
|
+
hasMore: matches.length > MAX_COMPLETIONS,
|
|
376
|
+
},
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// The coming year, and once a year is typed, every day of that year too
|
|
381
|
+
function dateSuggestions(typed) {
|
|
382
|
+
const start = today();
|
|
383
|
+
const days = Array.from(
|
|
384
|
+
{ length: 365 },
|
|
385
|
+
(_, i) => new Date(start.getFullYear(), start.getMonth(), start.getDate() + i)
|
|
386
|
+
);
|
|
387
|
+
const year = /^\d{4}/.exec(typed);
|
|
388
|
+
if (year) {
|
|
389
|
+
for (let day = new Date(+year[0], 0, 1); day.getFullYear() === +year[0]; day.setDate(day.getDate() + 1)) {
|
|
390
|
+
days.push(new Date(day));
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return days.map(formatDate);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function getCompletions({ ref = {}, argument = {} } = {}) {
|
|
397
|
+
const { name, value = "" } = argument;
|
|
398
|
+
|
|
399
|
+
if (ref.type === "ref/prompt" && ref.name === "theme_from_daily_color") {
|
|
400
|
+
if (name === "mode") return complete(["light", "dark"], value);
|
|
401
|
+
if (name === "date") return complete(dateSuggestions(value), value);
|
|
402
|
+
} else if (ref.type === "ref/resource" && ref.uri === "daily-color://{date}") {
|
|
403
|
+
if (name === "date") return complete(["today", ...dateSuggestions(value)], value);
|
|
404
|
+
} else {
|
|
405
|
+
throw rpcError(INVALID_PARAMS, `Nothing to complete for ${ref.name ?? ref.uri}`);
|
|
406
|
+
}
|
|
407
|
+
return complete([], value); // an argument we have no suggestions for
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function rpcError(code, message) {
|
|
411
|
+
return Object.assign(new Error(message), { code });
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Works out the reply to one message, or null for a notification
|
|
415
|
+
function handle(message) {
|
|
416
|
+
if (message === null || typeof message !== "object" || Array.isArray(message) || message.jsonrpc !== "2.0") {
|
|
417
|
+
return { jsonrpc: "2.0", id: null, error: { code: INVALID_REQUEST, message: "Not a JSON-RPC 2.0 message" } };
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const { id, method, params } = message;
|
|
421
|
+
const isNotification = id === undefined;
|
|
422
|
+
|
|
423
|
+
// Replies to requests we never send, and notifications we don't need
|
|
424
|
+
// (like notifications/initialized), are fine to ignore
|
|
425
|
+
if (isNotification || method === undefined) return null;
|
|
426
|
+
|
|
427
|
+
try {
|
|
428
|
+
const run = METHODS[method];
|
|
429
|
+
if (!run) throw rpcError(METHOD_NOT_FOUND, `Unknown method: ${method}`);
|
|
430
|
+
if (params !== undefined && (params === null || typeof params !== "object" || Array.isArray(params))) {
|
|
431
|
+
throw rpcError(INVALID_PARAMS, "params must be an object");
|
|
432
|
+
}
|
|
433
|
+
return { jsonrpc: "2.0", id, result: run(params) };
|
|
434
|
+
} catch (error) {
|
|
435
|
+
const code = error.code ?? -32603; // Internal error
|
|
436
|
+
if (code === -32603) console.error(error);
|
|
437
|
+
return { jsonrpc: "2.0", id, error: { code, message: error.message } };
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function send(reply) {
|
|
442
|
+
if (reply) process.stdout.write(JSON.stringify(reply) + "\n");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const lines = createInterface({ input: process.stdin });
|
|
446
|
+
lines.on("line", (line) => {
|
|
447
|
+
if (!line.trim()) return;
|
|
448
|
+
let message;
|
|
449
|
+
try {
|
|
450
|
+
message = JSON.parse(line);
|
|
451
|
+
} catch {
|
|
452
|
+
send({ jsonrpc: "2.0", id: null, error: { code: PARSE_ERROR, message: "That wasn't valid JSON" } });
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
send(handle(message));
|
|
456
|
+
});
|
|
457
|
+
watchTheDate();
|
|
458
|
+
console.error(`daily-color MCP server ${version} is running on stdio`);
|
package/bin/dailycolor.js
CHANGED
|
@@ -1,5 +1,93 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { getDailyPalette } from "../index.js";
|
|
6
|
+
|
|
7
|
+
const HELP = `Usage: dailycolor [colors...] [--date YYYY-MM-DD]
|
|
8
|
+
|
|
9
|
+
Prints today's color as a hex code. Name other colors to print the ones that
|
|
10
|
+
go with it instead.
|
|
11
|
+
|
|
12
|
+
Colors:
|
|
13
|
+
color the day's color (the default)
|
|
14
|
+
contrast black or white, whichever reads better on it
|
|
15
|
+
complementary opposite on the color wheel
|
|
16
|
+
split-complementary either side of the complement
|
|
17
|
+
analogous next door on the color wheel
|
|
18
|
+
triadic a third of the way round each way
|
|
19
|
+
tetradic a square round the color wheel
|
|
20
|
+
all all of the above, labeled
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
-d, --date <date> another day, like 2027-01-01
|
|
24
|
+
-h, --help show this help
|
|
25
|
+
-v, --version show the version number
|
|
26
|
+
|
|
27
|
+
Examples:
|
|
28
|
+
dailycolor
|
|
29
|
+
dailycolor complementary
|
|
30
|
+
dailycolor all --date 2027-01-01`;
|
|
31
|
+
|
|
32
|
+
// "split-complementary" on the command line, splitComplementary in the palette
|
|
33
|
+
const NAMES = {
|
|
34
|
+
color: "color",
|
|
35
|
+
contrast: "contrast",
|
|
36
|
+
complementary: "complementary",
|
|
37
|
+
"split-complementary": "splitComplementary",
|
|
38
|
+
analogous: "analogous",
|
|
39
|
+
triadic: "triadic",
|
|
40
|
+
tetradic: "tetradic",
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function fail(message) {
|
|
44
|
+
console.error(`dailycolor: ${message}\nRun "dailycolor --help" to see what it can do.`);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const args = process.argv.slice(2);
|
|
49
|
+
const wanted = [];
|
|
50
|
+
let date = new Date();
|
|
51
|
+
|
|
52
|
+
for (let i = 0; i < args.length; i++) {
|
|
53
|
+
const arg = args[i];
|
|
54
|
+
if (arg === "-h" || arg === "--help") {
|
|
55
|
+
console.log(HELP);
|
|
56
|
+
process.exit(0);
|
|
57
|
+
} else if (arg === "-v" || arg === "--version") {
|
|
58
|
+
const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
59
|
+
console.log(version);
|
|
60
|
+
process.exit(0);
|
|
61
|
+
} else if (arg === "-d" || arg === "--date" || arg.startsWith("--date=")) {
|
|
62
|
+
const value = arg.startsWith("--date=") ? arg.slice(7) : args[++i];
|
|
63
|
+
date = parseDate(value);
|
|
64
|
+
} else if (arg === "all") {
|
|
65
|
+
wanted.push(...Object.keys(NAMES));
|
|
66
|
+
} else if (NAMES[arg]) {
|
|
67
|
+
wanted.push(arg);
|
|
68
|
+
} else {
|
|
69
|
+
fail(`there's no color called "${arg}"`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A day where you are, not UTC midnight
|
|
74
|
+
function parseDate(value) {
|
|
75
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value || "");
|
|
76
|
+
const day = m && new Date(+m[1], +m[2] - 1, +m[3]);
|
|
77
|
+
if (!day || day.getFullYear() !== +m[1] || day.getMonth() !== +m[2] - 1)
|
|
78
|
+
fail(`"${value ?? ""}" isn't a date like 2027-01-01`);
|
|
79
|
+
return day;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const palette = getDailyPalette(date);
|
|
83
|
+
const show = (name) => [palette[NAMES[name]]].flat().join(" ");
|
|
84
|
+
|
|
85
|
+
if (wanted.length === 0) {
|
|
86
|
+
console.log(palette.color);
|
|
87
|
+
} else if (wanted.length === 1) {
|
|
88
|
+
console.log(show(wanted[0]));
|
|
89
|
+
} else {
|
|
90
|
+
// Several colors get a label each, lined up
|
|
91
|
+
const width = Math.max(...wanted.map((name) => name.length));
|
|
92
|
+
for (const name of new Set(wanted)) console.log(`${name.padEnd(width)} ${show(name)}`);
|
|
93
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function t(t){const{L:r,C:o,H:a}=f(u(t)),e=Math.max(o,.11),c=t=>s(r,e,(a+t)%360);return{color:t.toLowerCase(),contrast:n(t),complementary:c(180),splitComplementary:[c(150),c(210)],analogous:[c(330),c(30)],triadic:[c(120),c(240)],tetradic:[c(90),c(180),c(270)]}}function n(t){const n=c(t);return(n+.05)/.05>=1.05/(n+.05)?"#000000":"#ffffff"}function r(t,n){const r=c(t),o=c(n);return(Math.max(r,o)+.05)/(Math.min(r,o)+.05)}function o(t,o,c=4.5){const{l:u,c:f,h:i}="string"==typeof t?a(t):t,h="#000000"===n(o)?-1:1;for(let n=0;n<=200;n++){const a=u+h*n*.005;if(a<0||a>1)break;const s=0===n&&"string"==typeof t?t.toLowerCase():e(a,f,i);if(r(s,o)>=c)return s}return h<0?"#000000":"#ffffff"}function a(t){const{L:n,C:r,H:o}=f(u(t));return{l:n,c:r,h:o}}function e(t,n,r){return s(Math.min(1,Math.max(0,t)),Math.max(0,n),(r%360+360)%360)}function c(t){const[n,r,o]=u(t);return.2126*n+.7152*r+.0722*o}function u(t){const n=parseInt(t.replace("#",""),16);return[n>>16&255,n>>8&255,255&n].map(t=>(t/=255)<=.04045?t/12.92:((t+.055)/1.055)**2.4)}function f([t,n,r]){const o=Math.cbrt(.4122214708*t+.5363325363*n+.0514459929*r),a=Math.cbrt(.2119034982*t+.6806995451*n+.1073969566*r),e=Math.cbrt(.0883024619*t+.2817188376*n+.6299787005*r),c=1.9779984951*o-2.428592205*a+.4505937099*e,u=.0259040371*o+.7827717662*a-.808675766*e;return{L:.2104542553*o+.793617785*a-.0040720468*e,C:Math.hypot(c,u),H:(180*Math.atan2(u,c)/Math.PI+360)%360}}function i(t,n,r){const o=n*Math.cos(r*Math.PI/180),a=n*Math.sin(r*Math.PI/180),e=(t+.3963377774*o+.2158037573*a)**3,c=(t-.1055613458*o-.0638541728*a)**3,u=(t-.0894841775*o-1.291485548*a)**3;return[4.0767416621*e-3.3077115913*c+.2309699292*u,-1.2684380046*e+2.6097574011*c-.3413193965*u,-.0041960863*e-.7034186147*c+1.707614701*u]}function h(t){return t.every(t=>t>=-1e-4&&t<=1.0001)}function s(t,n,r){let o=i(t,n,r);if(!h(o)){let a=0,e=n;for(let n=0;n<20;n++){const n=(a+e)/2;h(i(t,n,r))?a=n:e=n}o=i(t,a,r)}return function(t){return"#"+t.map(t=>{const n=(t=Math.min(1,Math.max(0,t)))<=.0031308?12.92*t:1.055*t**(1/2.4)-.055;return Math.round(255*n).toString(16).padStart(2,"0")}).join("")}(o)}function M(t=new Date){const n=function(t){const n=t.getDate(),r=t.getMonth()+1,o=t.getFullYear(),a=`${n}${n*o*r}${o}${r}${n}${o}${r}${n}`;let e=0;for(let t=0;t<a.length;t++)e=(e<<5)-e+a.charCodeAt(t),e|=0;return Math.abs(e)}(t),r=n>>8&255,o=255&n;return`#${(n>>16&255).toString(16).padStart(2,"0")}${r.toString(16).padStart(2,"0")}${o.toString(16).padStart(2,"0")}`}function l(n=new Date){return t(M(n))}export{n as contrastColor,r as contrastRatio,M as getDailyColor,l as getDailyPalette,t as getPalette,e as oklch,o as readableOn,a as toOklch};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.DailyColor=n():t.DailyColor=n()}(globalThis,()=>(()=>{"use strict";const t={d:(n,e)=>{for(var o in e)t.o(e,o)&&!t.o(n,o)&&Object.defineProperty(n,o,{enumerable:!0,get:e[o]})},o:(t,n)=>Object.prototype.hasOwnProperty.call(t,n),r:t=>{Object.defineProperties(t,{[Symbol.toStringTag]:{value:"Module"},__esModule:{value:!0}})}};let n={};t.r(n),t.d(n,{contrastColor:()=>r,contrastRatio:()=>a,getDailyColor:()=>d,getDailyPalette:()=>m,getPalette:()=>o,oklch:()=>f,readableOn:()=>c,toOklch:()=>i});const e=.11;function o(t){const{L:n,C:o,H:a}=s(l(t)),c=Math.max(o,e),i=t=>M(n,c,(a+t)%360);return{color:t.toLowerCase(),contrast:r(t),complementary:i(180),splitComplementary:[i(150),i(210)],analogous:[i(330),i(30)],triadic:[i(120),i(240)],tetradic:[i(90),i(180),i(270)]}}function r(t){const n=u(t);return(n+.05)/.05>=1.05/(n+.05)?"#000000":"#ffffff"}function a(t,n){const e=u(t),o=u(n);return(Math.max(e,o)+.05)/(Math.min(e,o)+.05)}function c(t,n,e=4.5){const{l:o,c,h:u}="string"==typeof t?i(t):t,l="#000000"===r(n)?-1:1;for(let r=0;r<=200;r++){const i=o+l*r*.005;if(i<0||i>1)break;const s=0===r&&"string"==typeof t?t.toLowerCase():f(i,c,u);if(a(s,n)>=e)return s}return l<0?"#000000":"#ffffff"}function i(t){const{L:n,C:e,H:o}=s(l(t));return{l:n,c:e,h:o}}function f(t,n,e){return M(Math.min(1,Math.max(0,t)),Math.max(0,n),(e%360+360)%360)}function u(t){const[n,e,o]=l(t);return.2126*n+.7152*e+.0722*o}function l(t){const n=parseInt(t.replace("#",""),16);return[n>>16&255,n>>8&255,255&n].map(t=>(t/=255)<=.04045?t/12.92:((t+.055)/1.055)**2.4)}function s([t,n,e]){const o=Math.cbrt(.4122214708*t+.5363325363*n+.0514459929*e),r=Math.cbrt(.2119034982*t+.6806995451*n+.1073969566*e),a=Math.cbrt(.0883024619*t+.2817188376*n+.6299787005*e),c=1.9779984951*o-2.428592205*r+.4505937099*a,i=.0259040371*o+.7827717662*r-.808675766*a;return{L:.2104542553*o+.793617785*r-.0040720468*a,C:Math.hypot(c,i),H:(180*Math.atan2(i,c)/Math.PI+360)%360}}function h(t,n,e){const o=n*Math.cos(e*Math.PI/180),r=n*Math.sin(e*Math.PI/180),a=(t+.3963377774*o+.2158037573*r)**3,c=(t-.1055613458*o-.0638541728*r)**3,i=(t-.0894841775*o-1.291485548*r)**3;return[4.0767416621*a-3.3077115913*c+.2309699292*i,-1.2684380046*a+2.6097574011*c-.3413193965*i,-.0041960863*a-.7034186147*c+1.707614701*i]}function p(t){return t.every(t=>t>=-1e-4&&t<=1.0001)}function M(t,n,e){let o=h(t,n,e);if(!p(o)){let r=0,a=n;for(let n=0;n<20;n++){const n=(r+a)/2;p(h(t,n,e))?r=n:a=n}o=h(t,r,e)}return function(t){return"#"+t.map(t=>{const n=(t=Math.min(1,Math.max(0,t)))<=.0031308?12.92*t:1.055*t**(1/2.4)-.055;return Math.round(255*n).toString(16).padStart(2,"0")}).join("")}(o)}function d(t=new Date){const n=function(t){const n=t.getDate(),e=t.getMonth()+1,o=t.getFullYear(),r=`${n}${n*o*e}${o}${e}${n}${o}${e}${n}`;let a=0;for(let t=0;t<r.length;t++)a=(a<<5)-a+r.charCodeAt(t),a|=0;return Math.abs(a)}(t),e=n>>8&255,o=255&n;return`#${(n>>16&255).toString(16).padStart(2,"0")}${e.toString(16).padStart(2,"0")}${o.toString(16).padStart(2,"0")}`}function m(t=new Date){return o(d(t))}return n})());
|
package/index.d.ts
CHANGED
|
@@ -1 +1,43 @@
|
|
|
1
|
+
export interface Palette {
|
|
2
|
+
/** The color itself */
|
|
3
|
+
color: string;
|
|
4
|
+
/** Black or white, whichever is easier to read on the color */
|
|
5
|
+
contrast: string;
|
|
6
|
+
/** Opposite on the color wheel (180°) */
|
|
7
|
+
complementary: string;
|
|
8
|
+
/** Either side of the complement (150° and 210°) */
|
|
9
|
+
splitComplementary: [string, string];
|
|
10
|
+
/** Next door on the color wheel (-30° and +30°) */
|
|
11
|
+
analogous: [string, string];
|
|
12
|
+
/** A third of the way round each way (120° and 240°) */
|
|
13
|
+
triadic: [string, string];
|
|
14
|
+
/** A square: 90°, 180° and 270° */
|
|
15
|
+
tetradic: [string, string, string];
|
|
16
|
+
}
|
|
17
|
+
|
|
1
18
|
export function getDailyColor(date?: Date): string;
|
|
19
|
+
export function getDailyPalette(date?: Date): Palette;
|
|
20
|
+
export function getPalette(hex: string): Palette;
|
|
21
|
+
export function contrastColor(hex: string): string;
|
|
22
|
+
|
|
23
|
+
/** The WCAG contrast ratio between two colors, from 1 to 21 */
|
|
24
|
+
export function contrastRatio(a: string, b: string): number;
|
|
25
|
+
|
|
26
|
+
export interface Oklch {
|
|
27
|
+
/** Lightness, 0 to 1 */
|
|
28
|
+
l: number;
|
|
29
|
+
/** Chroma (strength), 0 to about 0.37 */
|
|
30
|
+
c: number;
|
|
31
|
+
/** Hue, 0 to 360° */
|
|
32
|
+
h: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A hex color in OKLCH */
|
|
36
|
+
export function toOklch(hex: string): Oklch;
|
|
37
|
+
/** The hex code for an OKLCH color, with chroma brought down to fit in sRGB */
|
|
38
|
+
export function oklch(l: number, c: number, h: number): string;
|
|
39
|
+
/**
|
|
40
|
+
* The color made darker or lighter until it reads on the background (4.5:1 by
|
|
41
|
+
* default), keeping its hue and chroma. Black or white if nothing is enough.
|
|
42
|
+
*/
|
|
43
|
+
export function readableOn(color: string | Oklch, background: string, ratio?: number): string;
|
package/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { getPalette } from "./palette.js";
|
|
2
|
+
|
|
3
|
+
export { contrastColor, contrastRatio, getPalette, oklch, readableOn, toOklch } from "./palette.js";
|
|
4
|
+
|
|
1
5
|
export function getDailyColor(date = new Date()) {
|
|
2
6
|
const hash = getSimpleHash(date);
|
|
3
7
|
|
|
@@ -8,6 +12,14 @@ export function getDailyColor(date = new Date()) {
|
|
|
8
12
|
return `#${r.toString(16).padStart(2, "0")}${g.toString(16).padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
|
|
9
13
|
}
|
|
10
14
|
|
|
15
|
+
/**
|
|
16
|
+
* The day's color and the colors that go with it.
|
|
17
|
+
* @param {Date} [date] Defaults to today
|
|
18
|
+
*/
|
|
19
|
+
export function getDailyPalette(date = new Date()) {
|
|
20
|
+
return getPalette(getDailyColor(date));
|
|
21
|
+
}
|
|
22
|
+
|
|
11
23
|
function getSimpleHash(date) {
|
|
12
24
|
const day = date.getDate();
|
|
13
25
|
const month = date.getMonth() + 1; // getMonth() is zero-based
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "daily-color",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Provides a unique hex code each day",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"color",
|
|
7
7
|
"daily",
|
|
8
8
|
"date",
|
|
9
9
|
"hash",
|
|
10
|
-
"hex color"
|
|
10
|
+
"hex color",
|
|
11
|
+
"mcp",
|
|
12
|
+
"model context protocol"
|
|
11
13
|
],
|
|
12
14
|
"homepage": "https://github.com/druewilding/daily-color#readme",
|
|
13
15
|
"bugs": {
|
|
@@ -16,11 +18,14 @@
|
|
|
16
18
|
"license": "ISC",
|
|
17
19
|
"author": "Drue Wilding",
|
|
18
20
|
"files": [
|
|
21
|
+
"dist",
|
|
19
22
|
"index.d.ts",
|
|
20
|
-
"index.js"
|
|
23
|
+
"index.js",
|
|
24
|
+
"palette.js"
|
|
21
25
|
],
|
|
22
26
|
"main": "index.js",
|
|
23
27
|
"bin": {
|
|
28
|
+
"daily-color-mcp": "bin/daily-color-mcp.js",
|
|
24
29
|
"dailycolor": "bin/dailycolor.js"
|
|
25
30
|
},
|
|
26
31
|
"repository": {
|
|
@@ -29,16 +34,20 @@
|
|
|
29
34
|
},
|
|
30
35
|
"type": "module",
|
|
31
36
|
"scripts": {
|
|
37
|
+
"dev": "serve --listen 6766",
|
|
32
38
|
"format": "prettier --write .",
|
|
33
39
|
"format:check": "prettier --check .",
|
|
40
|
+
"images": "node scripts/images.js",
|
|
34
41
|
"lint": "eslint --fix",
|
|
35
42
|
"lint:check": "eslint",
|
|
36
43
|
"package:lint": "npx npm-package-json-lint .",
|
|
37
|
-
"
|
|
44
|
+
"prepublishOnly": "npm run webpack",
|
|
45
|
+
"test": "node --test \"test/*.js\"",
|
|
38
46
|
"webpack": "npx webpack"
|
|
39
47
|
},
|
|
40
48
|
"devDependencies": {
|
|
41
49
|
"eslint-config-plus-prettier": "5.0.0",
|
|
50
|
+
"serve": "14.2.8",
|
|
42
51
|
"webpack": "5.113.0",
|
|
43
52
|
"webpack-cli": "7.3.1"
|
|
44
53
|
},
|
package/palette.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Colors that go with a color: its complement, triads and so on.
|
|
2
|
+
//
|
|
3
|
+
// The hues are turned in OKLCH, which matches how we see color better than
|
|
4
|
+
// HSL does, so the partners keep the color's lightness and strength. When a
|
|
5
|
+
// turned color doesn't fit in sRGB, its chroma comes down until it does.
|
|
6
|
+
//
|
|
7
|
+
// Grayish colors have hardly any chroma (strength) to keep, so their partners
|
|
8
|
+
// get at least MIN_CHROMA, and a gray day still has some color to go with it.
|
|
9
|
+
// A gray's hue is only a tiny tint, but it's always the same for that gray.
|
|
10
|
+
|
|
11
|
+
const MIN_CHROMA = 0.11;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A palette of colors that go with `hex`.
|
|
15
|
+
* @param {string} hex A color like "#3fa2c1"
|
|
16
|
+
*/
|
|
17
|
+
export function getPalette(hex) {
|
|
18
|
+
const { L, C, H } = linToOklch(hexToLin(hex));
|
|
19
|
+
const strength = Math.max(C, MIN_CHROMA);
|
|
20
|
+
const turn = (degrees) => fromOklch(L, strength, (H + degrees) % 360);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
color: hex.toLowerCase(),
|
|
24
|
+
contrast: contrastColor(hex),
|
|
25
|
+
complementary: turn(180),
|
|
26
|
+
splitComplementary: [turn(150), turn(210)],
|
|
27
|
+
analogous: [turn(330), turn(30)],
|
|
28
|
+
triadic: [turn(120), turn(240)],
|
|
29
|
+
tetradic: [turn(90), turn(180), turn(270)],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Black or white, whichever is easier to read on `hex` */
|
|
34
|
+
export function contrastColor(hex) {
|
|
35
|
+
const l = luminance(hex);
|
|
36
|
+
return (l + 0.05) / 0.05 >= 1.05 / (l + 0.05) ? "#000000" : "#ffffff";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The WCAG contrast ratio between two colors, from 1 to 21 */
|
|
40
|
+
export function contrastRatio(a, b) {
|
|
41
|
+
const x = luminance(a);
|
|
42
|
+
const y = luminance(b);
|
|
43
|
+
return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* `color` made readable on `background`: the same hue and chroma, made darker
|
|
48
|
+
* (on a light background) or lighter (on a dark one) until the contrast ratio
|
|
49
|
+
* is at least `ratio`. A color that's already readable comes back as it is.
|
|
50
|
+
* If no lightness is enough, you get black or white.
|
|
51
|
+
*
|
|
52
|
+
* `color` can be a hex code, or { l, c, h } to keep a chroma that wouldn't fit
|
|
53
|
+
* in sRGB at its starting lightness but might once it's darker or lighter.
|
|
54
|
+
* @param {string | {l: number, c: number, h: number}} color
|
|
55
|
+
* @param {string} background
|
|
56
|
+
* @param {number} [ratio] Defaults to 4.5, the WCAG AA level for text
|
|
57
|
+
*/
|
|
58
|
+
export function readableOn(color, background, ratio = 4.5) {
|
|
59
|
+
const { l, c, h } = typeof color === "string" ? toOklch(color) : color;
|
|
60
|
+
const dir = contrastColor(background) === "#000000" ? -1 : 1;
|
|
61
|
+
for (let step = 0; step <= 200; step++) {
|
|
62
|
+
const L = l + dir * step * 0.005;
|
|
63
|
+
if (L < 0 || L > 1) break;
|
|
64
|
+
const candidate = step === 0 && typeof color === "string" ? color.toLowerCase() : oklch(L, c, h);
|
|
65
|
+
if (contrastRatio(candidate, background) >= ratio) return candidate;
|
|
66
|
+
}
|
|
67
|
+
return dir < 0 ? "#000000" : "#ffffff";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A hex color's lightness (0 to 1), chroma (0 to about 0.37) and hue (0 to
|
|
72
|
+
* 360°) in OKLCH
|
|
73
|
+
*/
|
|
74
|
+
export function toOklch(hex) {
|
|
75
|
+
const { L, C, H } = linToOklch(hexToLin(hex));
|
|
76
|
+
return { l: L, c: C, h: H };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The hex code for an OKLCH color. If it doesn't fit in sRGB, its chroma comes
|
|
81
|
+
* down until it does, so the lightness and hue stay true.
|
|
82
|
+
*/
|
|
83
|
+
export function oklch(l, c, h) {
|
|
84
|
+
return fromOklch(Math.min(1, Math.max(0, l)), Math.max(0, c), ((h % 360) + 360) % 360);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function luminance(hex) {
|
|
88
|
+
const [r, g, b] = hexToLin(hex);
|
|
89
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ── sRGB <-> OKLCH ──────────────────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
function hexToLin(hex) {
|
|
95
|
+
const n = parseInt(hex.replace("#", ""), 16);
|
|
96
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((v) => {
|
|
97
|
+
v /= 255;
|
|
98
|
+
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function linToHex(rgb) {
|
|
103
|
+
return (
|
|
104
|
+
"#" +
|
|
105
|
+
rgb
|
|
106
|
+
.map((v) => {
|
|
107
|
+
v = Math.min(1, Math.max(0, v));
|
|
108
|
+
const s = v <= 0.0031308 ? 12.92 * v : 1.055 * v ** (1 / 2.4) - 0.055;
|
|
109
|
+
return Math.round(s * 255)
|
|
110
|
+
.toString(16)
|
|
111
|
+
.padStart(2, "0");
|
|
112
|
+
})
|
|
113
|
+
.join("")
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function linToOklch([r, g, b]) {
|
|
118
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
119
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
120
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
121
|
+
const A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s;
|
|
122
|
+
const B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s;
|
|
123
|
+
return {
|
|
124
|
+
L: 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
125
|
+
C: Math.hypot(A, B),
|
|
126
|
+
H: ((Math.atan2(B, A) * 180) / Math.PI + 360) % 360,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function oklchToLin(L, C, H) {
|
|
131
|
+
const a = C * Math.cos((H * Math.PI) / 180);
|
|
132
|
+
const b = C * Math.sin((H * Math.PI) / 180);
|
|
133
|
+
const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
|
|
134
|
+
const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
135
|
+
const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
136
|
+
return [
|
|
137
|
+
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
|
|
138
|
+
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
|
|
139
|
+
-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
|
|
140
|
+
];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function inGamut(rgb) {
|
|
144
|
+
return rgb.every((v) => v >= -1e-4 && v <= 1 + 1e-4);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// OKLCH -> hex, bringing chroma down until the color fits in sRGB
|
|
148
|
+
function fromOklch(L, C, H) {
|
|
149
|
+
let rgb = oklchToLin(L, C, H);
|
|
150
|
+
if (!inGamut(rgb)) {
|
|
151
|
+
let lo = 0;
|
|
152
|
+
let hi = C;
|
|
153
|
+
for (let i = 0; i < 20; i++) {
|
|
154
|
+
const mid = (lo + hi) / 2;
|
|
155
|
+
if (inGamut(oklchToLin(L, mid, H))) lo = mid;
|
|
156
|
+
else hi = mid;
|
|
157
|
+
}
|
|
158
|
+
rgb = oklchToLin(L, lo, H);
|
|
159
|
+
}
|
|
160
|
+
return linToHex(rgb);
|
|
161
|
+
}
|