Compare commits

..

No commits in common. "f397af24cdf38aa0809d3c28ee20606989b69bed" and "007da37674093454283e1d8da871f696b68152b0" have entirely different histories.

3 changed files with 41 additions and 63 deletions

View File

@ -18,7 +18,7 @@ import {ColourNamer} from './src/components/colour_namer.js';
const CELL = 10; const CELL = 10;
const MAG = 2; const MAG = 2;
const WIDTH = 200; const WIDTH = 20;
const HEIGHT = WIDTH; const HEIGHT = WIDTH;
// number of pixels which have to be visible for a colour to be // number of pixels which have to be visible for a colour to be
// mentioned in the alt text // mentioned in the alt text
@ -36,12 +36,10 @@ function randomise_params() {
f: random.choice(RADIUS_OPTS), f: random.choice(RADIUS_OPTS),
r: random.float(0, 0.4), r: random.float(0, 0.4),
}}); }});
const cell = 5 + random.float() * random.float() * 55;
return { return {
background: palette[0], background: palette[0],
palette: palette_name, palette: palette_name,
patterns: patterns, patterns: patterns
cell: cell,
} }
} }
@ -136,21 +134,18 @@ function poptimal_svg(params) {
const document = window.document; const document = window.document;
const container = d3.select(document.body).append("div"); const container = d3.select(document.body).append("div");
const width = WIDTH / params.cell; const dm = new DotMaker(WIDTH);
const height = WIDTH / params.cell;
const dm = new DotMaker(width, height);
const svg = container.append("svg") const svg = container.append("svg")
.attr("width", WIDTH * MAG) .attr("width", WIDTH * CELL * MAG)
.attr("height", HEIGHT * MAG) .attr("height", HEIGHT * CELL * MAG)
.attr("viewBox", [ 0, 0, width, height ]); .attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);
const background = svg.append("rect") const background = svg.append("rect")
.attr("x", 0) .attr("x", 0)
.attr("y", 0) .attr("y", 0)
.attr("width", width) .attr("width", WIDTH)
.attr("height", height) .attr("height", WIDTH)
.attr("fill", params.background); .attr("fill", params.background);
@ -232,6 +227,7 @@ async function main() {
const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour']; const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour'];
const namer = new ColourNamer(); const namer = new ColourNamer();
console.log(`Loading colours ${colourf}`);
await namer.load_colours(colourf); await namer.load_colours(colourf);
@ -254,7 +250,7 @@ async function main() {
// so we don't include obscured colours // so we don't include obscured colours
const hist = await get_histogram(imgfile); const hist = await get_histogram(imgfile);
const alt_text = image_description(namer, params, hist); const alt_text = image_description(namer, params, hist);
params.alt_text = alt_text;
await save_params(paramsfile, params); await save_params(paramsfile, params);
console.log(alt_text); console.log(alt_text);
console.log(imgfile); console.log(imgfile);

View File

@ -1,6 +1,5 @@
// calculate tiles // calculate tiles
const SIN_PERIOD = 100 / Math.PI;
const RADIUS_OPTS = [ const RADIUS_OPTS = [
"const", "const",
@ -12,14 +11,9 @@ const RADIUS_OPTS = [
"right-down", "right-down",
"left-up", "left-up",
"left-down", "left-down",
"circle-in", "in",
"circle-out", "out",
"hyper-in",
"hyper-out",
"noise", "noise",
"diamonds",
"horizontal-bands",
"vertical-bands",
]; ];
const RADIUS_DESC = { const RADIUS_DESC = {
@ -32,21 +26,15 @@ const RADIUS_DESC = {
"right-down": "getting larger towards the lower right", "right-down": "getting larger towards the lower right",
"left-up": "getting larger towards the upper left", "left-up": "getting larger towards the upper left",
"left-down": "getting larger towards the lower left", "left-down": "getting larger towards the lower left",
"circle-in": "forming a circle at the centre", "in": "getting larger in the centre",
"circle-out": "leaving a circular gap at the centre", "out": "getting larger at the edges",
"hyper-in": "forming a starlike pattern at the centre",
"hyper-out": "leaving a starlike pattern gap in the centre",
"noise": "of random sizes", "noise": "of random sizes",
"diamonds": "in a grid tilted at 45 degrees",
"horizontal-bands": "in horizontal bands",
"vertical-bands": "in a vertical bands",
} }
function distance(dx, dy) { function distance(dx, dy) {
return Math.sqrt(dx ** 2 + dy ** 2); return Math.sqrt(dx ** 2 + dy ** 2);
} }
function int_range(v1, v2) { function int_range(v1, v2) {
const vs = [v1, v2]; const vs = [v1, v2];
vs.sort((a, b) => a - b); vs.sort((a, b) => a - b);
@ -57,6 +45,7 @@ function int_range(v1, v2) {
class DotMaker { class DotMaker {
constructor(width, height) { constructor(width, height) {
console.log(width, height);
this.width = width; this.width = width;
this.height = height; this.height = height;
this.wh = 0.5 * (width + height); this.wh = 0.5 * (width + height);
@ -83,44 +72,40 @@ class DotMaker {
return ps; return ps;
} }
radius(func) { radius(d, func, maxr) {
switch (func) { switch (func) {
case "const": case "const":
return (d, r) => r; return maxr;
case "right": case "right":
return (d, r) => r * d.x / this.width; return maxr * d.x / this.width;
case "left": case "left":
return (d, r) => r * (this.width - d.x) / this.width; return maxr * (this.width - d.x) / this.width;
case "down": case "down":
return (d, r) => r * d.y / this.height; return maxr * d.y / this.height;
case "up": case "up":
return (d, r) => r * (this.height - d.y) / this.height; return maxr * (this.height - d.y) / this.height;
case "right-up": case "right-up":
return (d, r) => 0.5 * r * (d.x + this.height - d.y) / this.wh; return 0.5 * maxr * (d.x + this.height - d.y) / this.wh;
case "left-up": case "left-up":
return (d, r) => 0.5 * r * (this.width - d.x + this.height - d.y) / this.wh; return 0.5 * maxr * (this.width - d.x + this.height - d.y) / this.wh;
case "right-down": case "right-down":
return (d, r) => 0.5 * r * (d.x + d.y) / this.wh; return 0.5 * maxr * (d.x + d.y) / this.wh;
case "left-down": case "left-down":
return (d, r) => 0.5 * r * (this.width - d.x + d.y) / this.wh; return 0.5 * maxr * (this.width - d.x + d.y) / this.wh;
case "circle-out": case "out":
return (d, r) => 2 * r * distance((d.x - this.cx), (d.y - this.cy)) / this.wh; return 2 * maxr * distance((d.x - this.cx), (d.y - this.cy)) / this.wh;
case "circle-in": case "in":
return (d, r) => 2 * r * (0.5 * this.wh - distance((d.x - this.cx), (d.y - this.cy))) / this.wh; return 2 * maxr * (0.5 * this.wh - distance((d.x - this.cx), (d.y - this.cy))) / this.wh;
case "hyper-in":
return (d, r) => r * Math.abs((d.x - this.cx) * (d.y - this.cy)) / this.wh; // case "hyper-out":
case "hyper-out": // return 2 * maxr * Math.abs(d.x - this.cx) (d.y - this.cy)) / this.width;
return (d, r) => r * (1 - Math.abs((d.x - this.cx) * (d.y - this.cy)) / this.wh); // case "hyper-in":
// return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
case "noise": case "noise":
return (d, r) => r * Math.random(); return maxr * Math.random();
case "horizontal-bands":
return (d, r) => r * (1 + Math.cos(SIN_PERIOD *(d.y - this.cy)/ this.wh));
case "vertical-bands":
return (d, r) => r * (1 + Math.cos(SIN_PERIOD *(d.x - this.cx)/ this.wh));
case "grid":
return (d, r) => r * (0.5 + 0.5 * (Math.cos(SIN_PERIOD *(d.x - this.cx)/ this.wh) + Math.cos(SIN_PERIOD * (d.y - this.cy)/ this.wh)));
default: default:
return (d, r) => r; return maxr;
} }
} }
} }

View File

@ -144,8 +144,8 @@ const dots2 = dm.dots(1 / m2, n2, false);
const svg = d3.create("svg") const svg = d3.create("svg")
.attr("width", WIDTH * MAG) .attr("width", width * cell * MAG)
.attr("height", HEIGHT * MAG) .attr("height", height * cell * MAG)
.attr("viewBox", [ 0, 0, width, height ]); .attr("viewBox", [ 0, 0, width, height ]);
@ -210,11 +210,8 @@ display(svg.node());
```js ```js
// separate code block for when I understand transitions better // separate code block for when I understand transitions better
const rfunc1 = dm.radius(f1); dots_g1.selectAll("circle").attr("r", (d) => dm.radius(d, f1, r1));
const rfunc2 = dm.radius(f2); dots_g2.selectAll("circle").attr("r", (d) => dm.radius(d, f2, r2));
dots_g1.selectAll("circle").attr("r", (d) => rfunc1(d, r1));
dots_g2.selectAll("circle").attr("r", (d) => rfunc2(d, r2));
``` ```