let cols = [...Array(n)].map((e) => Array(n));
let palette = ["#2b67af", "#f9d531", "#ef562f", "#29ac9f", "#f589a3"];
for (let j = 0; j < n; j++) {
for (let i = 0; i < n; i++) {
if (i > 1) neighborColors.push(cols[i-2][j]);
if (j > 1) neighborColors.push(cols[i][j-2]);
if (i > 0 && j > 0) neighborColors.push(cols[i-1][j-1]);
if (i < n-1 && j > 0) neighborColors.push(cols[i+1][j-1]);
} while (neighborColors.indexOf(col) != -1)
makeHexagon(width/2, height/2, 175, n, [[[5], cols], [["#fffbe6"], [5]], [cols, ["#fffbe6"]]]);
function makeHexagon(x0, y0, s, n, cols) {
for (let theta = 0; theta < TAU; theta += TAU/3) {
makeFacet(x0, y0, s, n, theta, cols.pop());
function makeFacet(xA, yA, s, n, rot, cols) {
let rw = s*sqrt(3)/2, rh = s/2;
let [xi, yi] = lerpPoints(xA, yA, xB, yB, 1/n);
let [xj, yj] = lerpPoints(xA, yA, xD, yD, 1/n);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
let xa = i*xi + j*xj, ya = i*yi + j*yj;
let xb = (i+1)*xi + j*xj, yb = (i+1)*yi + j*yj;
let xc = (i+1)*xi + (j+1)*xj, yc = (i+1)*yi + (j+1)*yj;
let xd = i*xi + (j+1)*xj, yd = i*yi + (j+1)*yj;
let colChoice = cols[(i+j)%2];
let col = colChoice.length == 1 ? colChoice[0] : colChoice[i][j];
quad(xa, ya, xb, yb, xc, yc, xd, yd);
function lerpPoints(x1, y1, x2, y2, t) {
return [x1*(1-t)+x2*t, y1*(1-t)+y2*t];