Moved ColourNamer to its own file as it was breaking the web version

This commit is contained in:
Mike Lynch 2025-03-26 18:32:26 +11:00
parent f159f7d22c
commit 547baa9586
3 changed files with 43 additions and 36 deletions

View File

@ -12,7 +12,8 @@ const xlinkns = "http://www.w3.org/1999/xlink";
const svgns = "http://www.w3.org/2000/svg";
import {RADIUS_OPTS, RADIUS_DESC, DotMaker} from './src/components/dots.js';
import {PALETTES, ColourNamer} from './src/components/palettes.js';
import {PALETTES} from './src/components/palettes.js';
import {ColourNamer} from './src/components/colour_namer.js';
const CELL = 10;
const MAG = 2;
@ -68,7 +69,6 @@ function parse_histogram(convert_out) {
function colour_visible(hist, colour) {
const hexcolour = colour.formatHex().toUpperCase();
console.log(colour, hexcolour, hist.get(hexcolour));
return hist.get(hexcolour) > VISIBLE_DOG;
}
@ -125,7 +125,7 @@ function poptimal_svg(params) {
params.patterns.map((p) => {
const dots = dm.dots(1 / p.m, p.n);
const dots = dm.dots(1 / p.m, p.n, false);
const dots_g = svg.append("g")
.attr("id", `dots${p.i}`);

View File

@ -0,0 +1,39 @@
import { promises } from "fs";
import * as d3 from "d3-color";
import * as d3cd from "d3-color-difference";
class ColourNamer {
constructor() {
this.colmap = new Map();
}
async load_colours(colourfile) {
const colourBuff = await promises.readFile(colourfile);
const colours = colourBuff.toString();
const seen = new Set();
colours.split(/\n/).map((l) => {
const m = l.match(/(\d+)\s+(\d+)\s+(\d+)\s+(.*)/);
if( m ) {
const sig = `${m[1]},${m[2]},${m[3]}`;
if( !seen.has(sig) ) {
this.colmap.set(m[4], d3.rgb(m[1], m[2], m[3]));
seen.add(sig);
}
}
});
}
colour_to_text(d3color) {
const diffs = []
for( const [ name, colour] of this.colmap) {
diffs.push({name: name, dist: d3cd.differenceCie76(colour, d3color)});
}
diffs.sort((a, b) => a.dist - b.dist);
return diffs[0].name;
}
}
export { ColourNamer}

View File

@ -2,9 +2,7 @@
import * as d3 from "d3-color";
import shuffle from "lodash.shuffle";
import random from "random";
import { promises } from "fs";
import * as d3cd from "d3-color-difference";
const PALETTES = new Map([
[ "random RGB", palette_random ],
@ -78,36 +76,6 @@ function palette_cmy() {
return shuffle(cols);
}
class ColourNamer {
constructor() {
this.colmap = new Map();
}
async load_colours(colourfile) {
const colourBuff = await promises.readFile(colourfile);
const colours = colourBuff.toString();
const seen = new Set();
colours.split(/\n/).map((l) => {
const m = l.match(/(\d+)\s+(\d+)\s+(\d+)\s+(.*)/);
if( m ) {
const sig = `${m[1]},${m[2]},${m[3]}`;
if( !seen.has(sig) ) {
this.colmap.set(m[4], d3.rgb(m[1], m[2], m[3]));
seen.add(sig);
}
}
});
}
colour_to_text(d3color) {
const diffs = []
for( const [ name, colour] of this.colmap) {
diffs.push({name: name, dist: d3cd.differenceCie76(colour, d3color)});
}
diffs.sort((a, b) => a.dist - b.dist);
return diffs[0].name;
}
}
export { PALETTES, ColourNamer }
export { PALETTES }