Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Perlkonig committed Dec 9, 2024
2 parents 8c46cf4 + b04ea36 commit 23f050a
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 336 deletions.
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"graphology": "^0.25.4",
"honeycomb-grid": "4.1.1",
"json-stringify-deterministic": "^1.0.12",
"monotone-chain-convex-hull": "^1.1.0",
"tinycolor2": "^1.6.0",
"transformation-matrix-js": "github:epistemex/transformation-matrix-js",
"uuid": "^9.0.0"
Expand Down
31 changes: 31 additions & 0 deletions src/common/colours.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { rgb as convert_rgb, hsl as convert_hsl, hex as convert_hex } from "color-convert";

export const getRandomColor = ():string => {
const letters = '0123456789ABCDEF';
let color = '#';
Expand All @@ -6,3 +8,32 @@ export const getRandomColor = ():string => {
}
return color;
}

export type RGB = [number,number,number];

export const afterOpacity = (fg: RGB,o: number,bg: RGB = [255,255,255]): RGB => {
return fg.map((colFg,idx)=>o*colFg+(1-o)*bg[idx]) as RGB;
}

export const rgb2hex = (rgb: RGB): string => {
return "#" + convert_rgb.hex(rgb);
}

export const hex2rgb = (hex: string): RGB => {
return convert_hex.rgb(hex);
}

const unlogit = (x: number) => {
return Math.log(x / (1 - x));
}

const logit = (x: number) => {
return 1 / (1 + Math.exp(-x));
}

export const lighten = (rgb: [number, number, number], ds: number, dl: number): RGB => {
const hsl = convert_rgb.hsl(rgb);
const l = 100 * logit(unlogit(hsl[2] / 100) + dl);
const s = 100 * logit(unlogit(hsl[1] / 100) + ds);
return convert_hsl.rgb([hsl[0], s, l]);
}
42 changes: 42 additions & 0 deletions src/common/polys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Poly } from "../grids";
import { circle2poly } from "./plotting";
import turfUnion from "@turf/union";
import { polygon as turfPoly, Properties, Feature, Polygon, MultiPolygon } from "@turf/helpers";
import getConvexHull from "monotone-chain-convex-hull";

export const unionPolys = (polys: Poly[]): [number,number][] => {
const turfed = polys.flat().map(p => {
let pts: [number,number][];
if (p.type === "circle") {
pts = circle2poly(p.cx, p.cy, p.r);
} else {
pts = [...p.points.map(pt => [pt.x, pt.y] as [number,number])];
}
if (pts[0] !== pts[pts.length - 1]) {
pts.push(pts[0])
}
return turfPoly([pts]);
});
let union: Feature<Polygon|MultiPolygon, Properties>|null = turfed.pop()!;
while (turfed.length > 0) {
const next = turfed.pop()! as Feature<Polygon|MultiPolygon, Properties>;
union = turfUnion(union, next);
if (union === null) {
throw new Error(`Got null while joining polygons in backFill()`);
}
}
return union.geometry.coordinates[0] as [number,number][];
}

export const convexHullPolys = (polys: Poly[]): [number,number][] => {
const allPts: [number,number][] = polys.flat().map(p => {
let pts: [number,number][];
if (p.type === "circle") {
pts = circle2poly(p.cx, p.cy, p.r);
} else {
pts = [...p.points.map(pt => [pt.x, pt.y] as [number,number])];
}
return pts;
}).flat();
return getConvexHull(allPts);
}
Loading

0 comments on commit 23f050a

Please sign in to comment.