Skip to content

Commit

Permalink
more workers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcnuttandrew committed Jan 22, 2024
1 parent c374e79 commit 5be508a
Show file tree
Hide file tree
Showing 17 changed files with 354 additions and 336 deletions.
8 changes: 5 additions & 3 deletions src/content-modules/Eval.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
$: colorNames = colorNameSimple(colors);
$: palType = $colorStore.currentPal.type;
$: evalConfig = $colorStore.currentPal.evalConfig;
$: checks = runLintChecks($colorStore.currentPal).filter((x) =>
x.taskTypes.includes(palType)
);
$: checks = [];
$: runLintChecks($colorStore.currentPal).then((result) => {
console.log(result);
checks = result.filter((x) => x.taskTypes.includes(palType));
});
$: colorsToIssues = colors.map((x) => {
const hex = `${x.toHex()}`;
Expand Down
28 changes: 24 additions & 4 deletions src/lib/api-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,31 @@ import ViteWorker from "./heavy-computation.worker?worker";
const tsWorker = new ViteWorker();

// send and receive messages from the worker
tsWorker.postMessage({ type: "test", content: "hellow" });
const message = { type: "test", content: "hellow" };
const randID = () => Math.random().toString(36).substring(7);
function workerDispatch() {
const waitingCallbacks: { [key: string]: (msg: string) => void } = {};
tsWorker.addEventListener("message", (msg: MessageEvent<string>) => {
const { id, content } = msg.data;
if (waitingCallbacks[id]) {
waitingCallbacks[id](content);
delete waitingCallbacks[id];
}
});

tsWorker.addEventListener("message", (msg: MessageEvent<string>) => {
console.log(msg.data); // how now, brown cow?
});
return async function caller(msg: typeof message) {
const id = randID();
tsWorker.postMessage({ ...msg, id });
return new Promise<string>((resolve) => {
waitingCallbacks[id] = resolve;
});
};
}
const dispatch = workerDispatch();

export function colorName(c: Color) {
return dispatch({ type: "color-name", content: c.toHex() });
}

type Engine = "openai" | "google";
type SimplePal = { background: string; colors: string[] };
Expand Down
292 changes: 0 additions & 292 deletions src/lib/color-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,295 +65,3 @@ function deltaE94(c1: ChromaColor, c2: ChromaColor) {
}

/////////// JEFF'S CODE ///////////////////////

// // const jsonData = require("../assets/c3-data.json");
// import jsonData from "../assets/c3-data.json";

// function c3_api() {
// const C = c3.color.length;
// const W = c3.terms.length;
// const T = c3.T;
// const A = c3.A;
// const ccount = c3.color.count;
// const tcount = c3.terms.count;

// c3.count = (c: number, w: number) => T[c * W + w] || 0;
// c3.terms.prob = (w: number, c: number) => (T[c * W + w] || 0) / tcount[w];

// c3.terms.entropy = function (w: number) {
// let H = 0;
// let p;
// for (let c = 0; c < C; ++c) {
// p = (T[c * W + w] || 0) / tcount[w];
// if (p > 0) H += (p * Math.log(p)) / Math.LN2;
// }
// return H;
// };

// c3.terms.perplexity = (w: any) => Math.pow(2, -c3.terms.entropy(w));

// c3.terms.cosine = function (a: number, b: number) {
// let sa = 0;
// let sb = 0;
// let sc = 0;
// let ta: number;
// let tb: number;
// for (let c = 0; c < C; ++c) {
// ta = T[c * W + a] || 0;
// tb = T[c * W + b] || 0;
// sa += ta * ta;
// sb += tb * tb;
// sc += ta * tb;
// }
// return sc / Math.sqrt(sa * sb);
// };

// c3.color.cosine = function (a: number, b: number) {
// let sa = 0;
// let sb = 0;
// let sc = 0;
// let ta: number;
// let tb: number;
// for (let w = 0; w < W; ++w) {
// ta = T[a * W + w] || 0;
// tb = T[b * W + w] || 0;
// sa += ta * ta;
// sb += tb * tb;
// sc += ta * tb;
// }
// return sc / Math.sqrt(sa * sb);
// };

// c3.color.prob = (c: number, w: number) => (T[c * W + w] || 0) / ccount[c];

// c3.color.entropy = function (c: number) {
// let H = 0;
// let p: number;
// for (let w = 0; w < W; ++w) {
// p = (T[c * W + w] || 0) / ccount[c];
// if (p > 0) H += (p * Math.log(p)) / Math.LN2;
// }
// return H;
// };

// c3.terms.hellinger = function (a: number, b: number) {
// let bc = 0;
// let pa: number;
// let pb: number;
// let z = Math.sqrt(tcount[a] * tcount[b]);
// for (let c = 0; c < C; ++c) {
// pa = T[c * W + a] || 0;
// pb = T[c * W + b] || 0;
// bc += Math.sqrt(pa * pb);
// }
// return Math.sqrt(1 - bc / z);
// };

// c3.color.perplexity = (c: any) => Math.pow(2, -c3.color.entropy(c));

// c3.color.hellinger = function (a: number, b: number) {
// let bc = 0;
// let pa: number;
// let pb: number;
// let z = Math.sqrt(ccount[a] * ccount[b]);
// for (let w = 0; w < W; ++w) {
// pa = T[a * W + w] || 0;
// pb = T[b * W + w] || 0;
// bc += Math.sqrt(pa * pb);
// }
// return Math.sqrt(1 - bc / z);
// };

// c3.terms.relatedTerms = function (w: number, limit: number | undefined) {
// const c = c3.terms.center[w];
// const list = [];
// for (let i = 0; i < W; ++i) {
// if (i != w) list.push({ index: i, score: A[i * W + w] });
// }
// list.sort(function (a, b) {
// let ca: { de00: (arg0: any) => number };
// let cb: { de00: (arg0: any) => number };

// let cmp = b.score - a.score;
// if (Math.abs(cmp) < 0.00005) {
// // break near ties by distance between centers
// ca = c3.terms.center[a.index];
// cb = c3.terms.center[b.index];
// cmp = ca.de00(c) - cb.de00(c);
// }
// return cmp;
// });
// list.unshift({ index: w, score: A[w * W + w] });
// return limit ? list.slice(0, limit) : list;
// };

// c3.terms.relatedColors = function (w: number, limit: number | undefined) {
// const list = [];
// for (let c = 0; c < C; ++c) {
// const s = (T[c * W + w] || 0) / ccount[c];
// if (s > 0) list.push({ index: c, score: s });
// }
// list.sort(function (a, b) {
// return b.score - a.score;
// });
// return limit ? list.slice(0, limit) : list;
// };

// c3.color.relatedTerms = function (
// c: number,
// limit: number | undefined,
// minCount: number
// ) {
// const cc = c * W;
// let list = [];
// let sum = 0;
// let s: any;
// let cnt = c3.terms.count;
// for (let w = 0; w < W; ++w) {
// if ((s = T[cc + w]) !== undefined) {
// list.push({ index: w, score: s });
// sum += s;
// }
// }
// if (minCount) {
// list = list.filter(function (d) {
// return cnt[d.index] > minCount;
// });
// }
// list.sort(function (a, b) {
// return b.score - a.score;
// });
// list.forEach(function (d) {
// d.score /= sum;
// });
// return limit ? list.slice(0, limit) : list;
// };

// // compute representative colors
// c3.terms.center = Array.from({ length: W }, (_, i) => i).map(function (w) {
// const list = c3.terms
// .relatedColors(w, 5)
// .map(function (d: { index: string | number }) {
// return c3.color[d.index];
// });
// let L = 0;
// let a = 0;
// let b = 0;
// let N = list.length;
// list.forEach(function (c: { L: any; a: any; b: any }) {
// L += c.L;
// a += c.a;
// b += c.b;
// });
// return chroma.lab(Math.round(L / N), Math.round(a / N), Math.round(b / N));
// // return d3.lab(Math.round(L / N), Math.round(a / N), Math.round(b / N));
// });
// }

// function c3_init(json: {
// color: string | any[];
// terms: any;
// T: string | any[];
// A: any;
// }) {
// let i: number;
// let C: number;
// let W: number;
// let T: any[];
// let A: any;
// let ccount: any[];
// let tcount: any[];

// // parse colors
// c3.color = [];
// for (i = 0; i < json.color.length; i += 3) {
// c3.color[i / 3] = chroma.lab(
// json.color[i],
// json.color[i + 1],
// json.color[i + 2]
// );
// }
// C = c3.color.length;

// // parse terms
// c3.terms = json.terms;
// W = c3.terms.length;

// // parse count table
// c3.T = T = [];
// for (let i = 0; i < json.T.length; i += 2) {
// T[json.T[i]] = json.T[i + 1];
// }

// // construct counts
// c3.color.count = ccount = [];
// for (i = 0; i < C; ++i) ccount[i] = 0;
// c3.terms.count = tcount = [];
// for (i = 0; i < W; ++i) tcount[i] = 0;
// T.forEach((_x, idx) => {
// const c = Math.floor(idx / W);
// const w = Math.floor(idx % W);
// const v = T[idx] || 0;
// ccount[c] += v;
// tcount[w] += v;
// });

// // parse word association matrix
// c3.A = A = json.A;

// const labToString = (L: number, a: number, b: number) =>
// [5 * Math.round(L / 5), 5 * Math.round(a / 5), 5 * Math.round(b / 5)].join(
// ","
// );
// var map: Record<string, any> = {};
// for (var c = 0; c < c3.color.length; ++c) {
// const [L, a, b] = chroma(c3.color[c]).lab();
// map[labToString(L, a, b)] = c;
// }

// function index(c: string) {
// const [L, a, b] = chroma(c).lab();
// const s = labToString(L, a, b);
// return map[s];
// }

// c3.colorIdentity = (colorString: string) => {
// // NOTE: entropy min/max currently hard-wired to XKCD results
// const minE = -4.5;
// const maxE = 0;
// var c = index(colorString);
// var h = (c3.color.entropy(c) - minE) / (maxE - minE);
// var t = c3.color
// .relatedTerms(c, 1)
// .map((x: any) => ({ score: x.score, word: c3.terms[x.index] }));
// var [L, a, b] = chroma(colorString).lab();
// const z = ~~L + ", " + ~~a + ", " + ~~b;

// return { x: colorString, c: c, h: h, terms: t, z: z };
// };
// }

// export function colorNameDiscrimCheck(
// colorNames: { word: string }[]
// ): false | string {
// const colorNameCounts = colorNames.reduce((acc, colorName) => {
// if (!acc[colorName.word]) {
// acc[colorName.word] = 0;
// }
// acc[colorName.word] += 1;
// return acc;
// }, {} as Record<string, number>);

// const remaining = Object.entries(colorNameCounts as Record<string, number>)
// .filter((x) => x[1] > 1)
// .map((x) => x[0]);
// if (remaining.length === 0) {
// return false;
// }
// const counts = remaining.map((x) => `${x} (${colorNameCounts[x]})`);
// return `Color Name discriminability check failed. The following color names are repeated: ${counts}`;
// }

// export const c3: any = {};
// c3_init(jsonData);
// c3_api();
Loading

0 comments on commit 5be508a

Please sign in to comment.