Skip to content

Commit 3adc6d0

Browse files
More versatile approach to colours using hex
1 parent 8a7b291 commit 3adc6d0

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

calendar.js

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,50 @@ async function createEvents(auth, eventBodiesArray) {
3030
}
3131
}
3232

33-
function getColorId(colorName) {
34-
const colorMap = {
35-
'lightpurple': 1, 'lavender': 1, 'lilac': 1, 'violet': 1,
36-
'lime': 2, 'sage': 2, 'lightgreen': 2, 'mint': 2,
37-
'darkpurple': 3, 'grape': 3, 'purple': 3, 'plum': 3,
38-
'salmon': 4, 'flamingo': 4, 'pink': 4, 'rose': 4,
39-
'gold': 5, 'banana': 5, 'yellow': 5, 'lemon': 5,
40-
'tangerine': 6, 'orange': 6, 'peach': 6,
41-
'lightblue': 7, 'peacock': 7, 'teal': 7, 'aqua': 7,
42-
'black': 8, 'graphite': 8, 'gray': 8, 'charcoal': 8,
43-
'darkblue': 9, 'blueberry': 9, 'blue': 9, 'navy': 9,
44-
'darkgreen': 10, 'basil': 10, 'green': 10, 'emerald': 10,
45-
'tomato': 11, 'red': 11, 'crimson': 11
46-
};
47-
48-
return colorMap[colorName.toLowerCase()] || null;
33+
const colors = {
34+
1: ["#E6E6FA"], // lavender
35+
2: ["#BCB88A", "#B2AC88"], // sage
36+
3: ["#6F2DA8", "#6F2D91"], // grape
37+
4: ["#FC8EAC", "#FC74E7"], // flamingo
38+
5: ["#FFE135"], // banana
39+
6: ["#F28500", "#F08035", "#FFA500"], // tangerine
40+
7: ["#004B49", "#005377", "#004B77"], // peacock
41+
8: ["#636466", "#4B4E53", "#4B4B4B"], // graphite
42+
9: ["#0000FF", "#4F86F7", "#4B0082"], // blueberry
43+
10: ["#32612D", "#5CB85C", "#007320"], // basil
44+
11: ["#FF6347"] // tomato
45+
};
46+
47+
function hexToRgb(hex) {
48+
let bigint = parseInt(hex.slice(1), 16);
49+
let r = (bigint >> 16) & 255;
50+
let g = (bigint >> 8) & 255;
51+
let b = bigint & 255;
52+
return [r, g, b];
53+
}
54+
55+
function getClosestColor(hex) {
56+
let inputRgb = hexToRgb(hex);
57+
let closestColorNumber = '';
58+
let shortestDistance = Infinity;
59+
60+
for (let colorNumber in colors) {
61+
for (let colorHex of colors[colorNumber]) {
62+
let colorRgb = hexToRgb(colorHex);
63+
let distance = Math.sqrt(
64+
Math.pow(inputRgb[0] - colorRgb[0], 2) +
65+
Math.pow(inputRgb[1] - colorRgb[1], 2) +
66+
Math.pow(inputRgb[2] - colorRgb[2], 2)
67+
);
68+
69+
if (distance < shortestDistance) {
70+
shortestDistance = distance;
71+
closestColorNumber = colorNumber;
72+
}
73+
}
74+
}
75+
76+
return closestColorNumber;
4977
}
5078

5179
function createEventObjects(optionsArray) {
@@ -114,8 +142,8 @@ function createEventObjects(optionsArray) {
114142
};
115143
}
116144

117-
if (options.colour) {
118-
event.colorId = getColorId(options.colour);
145+
if (options.color) {
146+
event.colorId = getClosestColor(options.color);
119147
}
120148

121149
events.push(event);

interaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class ChatWindow {
173173
let today = new Date().toISOString().split('T')[0];
174174
let time = new Date().toTimeString().split(' ')[0];
175175
let day = new Date().toLocaleString('en-us', { weekday: 'long' });
176-
let newEventsPrompt = `Given the following current date and time: ${day}, ${today}T${time}:00 and planning prompt: '${prompt}', format the prompt's contents as JSON objects with the following keys: summary, description (short, in 2nd person and timeless), start, end (don't write timezone.), allDay (boolean), colour (only if asked for), and location (if provided) in an array that can be parsed to create calendar events. Please use 1-2 emojis per complex sentence in the title's lhs and description to make them more personal.`;
176+
let newEventsPrompt = `Given the following current date and time: ${day}, ${today}T${time}:00 and planning prompt: '${prompt}', format the prompt's contents as JSON objects with the following keys: summary, description (short, in 2nd person and timeless), start, end (don't write timezone.), allDay (boolean), color (hex, only if asked for), and location (if provided) in an array that can be parsed to create calendar events. Please use 1-2 emojis per complex sentence in the title's lhs and description to make them more personal.`;
177177
return newEventsPrompt;
178178
}
179179

0 commit comments

Comments
 (0)