feat(posters): 新增图案/纹理源 pattern.css(48) 与 Radix Colors(288 色),海报库达 2358/65
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 从开源 CSS 库提取静态素材(图案 / 配色),生成本地海报源
|
||||
*
|
||||
* 产物:
|
||||
* js/data/posters/patterncss/files/*.svg ← pattern.css(MIT)图案
|
||||
* js/data/posters/radix-colors/files/*.json ← Radix Colors(MIT)色阶
|
||||
*
|
||||
* pattern.css 的图案由 CSS 渐变定义(无 data URI SVG),
|
||||
* 本脚本按各家族几何规则还原为等价的可平铺 SVG(currentColor 着色)。
|
||||
*
|
||||
* 用法:node scripts/import-css-assets.js
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const https = require('https');
|
||||
|
||||
const ROOT = path.resolve(__dirname, '..');
|
||||
const OUT = path.join(ROOT, 'js', 'data', 'posters');
|
||||
|
||||
function fetchText(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
https.get(url, { headers: { 'User-Agent': 'drawlogo' }, timeout: 60000 }, res => {
|
||||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
||||
return fetchText(res.headers.location).then(resolve, reject);
|
||||
}
|
||||
let d = '';
|
||||
res.on('data', c => { d += c; });
|
||||
res.on('end', () => resolve(d));
|
||||
}).on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
function writeFile(rel, content) {
|
||||
const p = path.join(OUT, rel);
|
||||
fs.mkdirSync(path.dirname(p), { recursive: true });
|
||||
fs.writeFileSync(p, content);
|
||||
}
|
||||
|
||||
/* ================= 1. pattern.css 图案 → SVG ================= */
|
||||
/** 尺寸档位(与 pattern.css 的 sm/md/lg/xl 对应) */
|
||||
const SIZES = { sm: 10, md: 25, lg: 50, xl: 100 };
|
||||
|
||||
/** 圆点半径(pattern.css 中 sm/md/lg/xl 对应 0.5 / 1 / 1.5 / 2px,间距 = 10 倍半径) */
|
||||
const DOT_R = { sm: 0.5, md: 1, lg: 1.5, xl: 2 };
|
||||
|
||||
/** 数值去尾零(避免 0.00 这类非法/冗余输出) */
|
||||
function n(v) {
|
||||
return String(Math.round(v * 1000) / 1000);
|
||||
}
|
||||
|
||||
/** 组装可平铺 SVG 预览(4 个重复单元) */
|
||||
function wrap(w, h, body) {
|
||||
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${n(w)} ${n(h)}" fill="none">${body}</svg>`;
|
||||
}
|
||||
|
||||
const FAMILIES = {
|
||||
/* 棋盘格:2s 单元内对角填充 */
|
||||
checks(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let i = 0; i < 4; i++) {
|
||||
for (let j = 0; j < 4; j++) {
|
||||
if ((i + j) % 2 === 0) out.push(`<rect x="${n(i * s)}" y="${n(j * s)}" width="${n(s)}" height="${n(s)}" fill="currentColor"/>`);
|
||||
}
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 网格线:每 s 一条 1px 线 */
|
||||
grid(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = 0; k <= 4; k++) {
|
||||
out.push(`<path d="M${n(k * s)} 0V${n(area)}M0 ${n(k * s)}H${n(area)}" stroke="currentColor" stroke-width="1"/>`);
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 圆点阵:间距 = 10r */
|
||||
dots(s, key) {
|
||||
const r = DOT_R[key], sp = 10 * r, area = 4 * sp, out = [];
|
||||
for (let i = 0; i < 4; i++) {
|
||||
for (let j = 0; j < 4; j++) {
|
||||
out.push(`<circle cx="${n(r + i * sp)}" cy="${n(r + j * sp)}" r="${n(r)}" fill="currentColor"/>`);
|
||||
}
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 交错圆点:间距 20r,第二组偏移 10r */
|
||||
'cross-dots'(s, key) {
|
||||
const r = DOT_R[key], sp = 20 * r, area = 2 * sp, out = [];
|
||||
for (let i = 0; i < 2; i++) {
|
||||
for (let j = 0; j < 2; j++) {
|
||||
const x = r + i * sp, y = r + j * sp;
|
||||
out.push(`<circle cx="${n(x)}" cy="${n(y)}" r="${n(r)}" fill="currentColor"/>`);
|
||||
out.push(`<circle cx="${n(x + sp / 2)}" cy="${n(y + sp / 2)}" r="${n(r)}" fill="currentColor"/>`);
|
||||
}
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 竖线:每 s 一条 1px 线 */
|
||||
'vertical-lines'(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = 0; k <= 4; k++) out.push(`<path d="M${n(k * s)} 0V${n(area)}" stroke="currentColor" stroke-width="1"/>`);
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 横线:每 s 一条 1px 线 */
|
||||
'horizontal-lines'(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = 0; k <= 4; k++) out.push(`<path d="M0 ${n(k * s)}H${n(area)}" stroke="currentColor" stroke-width="1"/>`);
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 斜线:45°,水平间距 s */
|
||||
'diagonal-lines'(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = -4; k <= 4; k++) {
|
||||
const x = k * s;
|
||||
out.push(`<path d="M${n(x)} 0L${n(x + area)} ${n(area)}" stroke="currentColor" stroke-width="1"/>`);
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 竖条纹:每 s 内半宽填充 */
|
||||
'vertical-stripes'(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = 0; k < 4; k++) out.push(`<rect x="${n(k * s)}" y="0" width="${n(s / 2)}" height="${n(area)}" fill="currentColor"/>`);
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 横条纹:每 s 内半高填充 */
|
||||
'horizontal-stripes'(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = 0; k < 4; k++) out.push(`<rect x="0" y="${n(k * s)}" width="${n(area)}" height="${n(s / 2)}" fill="currentColor"/>`);
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 斜条纹:45°,带宽 s、水平周期 2s */
|
||||
'diagonal-stripes'(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let k = -4; k <= 4; k++) {
|
||||
const x = k * 2 * s;
|
||||
out.push(`<path d="M${n(x)} 0L${n(x + area)} ${n(area)}" stroke="currentColor" stroke-width="${n(s)}"/>`);
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
},
|
||||
|
||||
/* 锯齿:两条上下起伏的折线带 */
|
||||
zigzag(s) {
|
||||
const w = 4 * s, h = 2 * s, out = [];
|
||||
for (const y0 of [s / 2, (3 * s) / 2]) {
|
||||
out.push(`<path d="M0 ${n(y0)}L${n(s / 2)} ${n(y0 - s / 2)}L${n(s)} ${n(y0)}L${n((3 * s) / 2)} ${n(y0 - s / 2)}L${n(2 * s)} ${n(y0)}L${n((5 * s) / 2)} ${n(y0 - s / 2)}L${n(3 * s)} ${n(y0)}L${n((7 * s) / 2)} ${n(y0 - s / 2)}L${n(4 * s)} ${n(y0)}" stroke="currentColor" stroke-width="${n(s / 2)}"/>`);
|
||||
}
|
||||
return wrap(w, h, out.join(''));
|
||||
},
|
||||
|
||||
/* 三角:每 s 单元半格三角 */
|
||||
triangles(s) {
|
||||
const area = 4 * s, out = [];
|
||||
for (let i = 0; i < 4; i++) {
|
||||
for (let j = 0; j < 4; j++) {
|
||||
const x = i * s, y = j * s;
|
||||
out.push(`<polygon points="${n(x)},${n(y + s)} ${n(x + s)},${n(y + s)} ${n(x)},${n(y)}" fill="currentColor"/>`);
|
||||
}
|
||||
}
|
||||
return wrap(area, area, out.join(''));
|
||||
}
|
||||
};
|
||||
|
||||
/** 从 pattern.css 提取图案类名(依据真实 CSS 校验,避免收录不存在/工具类) */
|
||||
function patternNames(css) {
|
||||
const names = [...css.matchAll(/\.pattern-([a-z0-9-]+)\s*\{/g)].map(m => m[1]);
|
||||
const out = [];
|
||||
for (const name of names) {
|
||||
const m = name.match(/^([a-z-]+)-(sm|md|lg|xl)$/);
|
||||
if (!m) continue; // 过滤 pattern-w-* / pattern-h-* 等尺寸工具类
|
||||
if (!FAMILIES[m[1]]) continue; // 未知家族跳过
|
||||
if (out.indexOf(name) === -1) out.push(name);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function importPatternCss() {
|
||||
const css = await fetchText('https://unpkg.com/pattern.css@1.0.0/dist/pattern.css');
|
||||
const names = patternNames(css);
|
||||
let ok = 0;
|
||||
for (const name of names) {
|
||||
const m = name.match(/^([a-z-]+)-(sm|md|lg|xl)$/);
|
||||
const svg = FAMILIES[m[1]](SIZES[m[2]], m[2]);
|
||||
if (!/^<svg[\s\S]*<\/svg>$/.test(svg)) continue;
|
||||
writeFile('patterncss/files/' + name + '.svg', svg);
|
||||
ok++;
|
||||
}
|
||||
console.log(`pattern.css: 提取 ${ok} 个图案(共 ${names.length} 个图案类)`);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* ---------------- 2. Radix Colors 色阶 ---------------- */
|
||||
const RADIX = [
|
||||
'tomato', 'red', 'crimson', 'pink', 'plum', 'purple', 'violet', 'iris', 'indigo', 'blue',
|
||||
'cyan', 'teal', 'jade', 'green', 'grass', 'lime', 'mint', 'sky', 'gold', 'bronze',
|
||||
'brown', 'orange', 'amber', 'yellow'
|
||||
];
|
||||
|
||||
async function importRadixColors() {
|
||||
let n2 = 0;
|
||||
for (const color of RADIX) {
|
||||
let css;
|
||||
try { css = await fetchText(`https://unpkg.com/@radix-ui/colors@3.0.0/${color}.css`); }
|
||||
catch (e) { continue; }
|
||||
const vals = [];
|
||||
for (let i = 1; i <= 12; i++) {
|
||||
const m = css.match(new RegExp(`--${color}-${i}:\\s*([^;]+);`));
|
||||
if (m) vals.push(m[1].trim());
|
||||
}
|
||||
if (vals.length < 10) continue;
|
||||
writeFile(`radix-colors/files/${color}.json`, JSON.stringify({ name: color, colors: vals }, null, 2));
|
||||
n2++;
|
||||
}
|
||||
console.log(`Radix Colors: 提取 ${n2} 组色阶(${n2 * 12} 色)`);
|
||||
return n2;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
console.log('开始提取 CSS 静态素材…');
|
||||
const a = await importPatternCss();
|
||||
const b = await importRadixColors();
|
||||
console.log(`完成:图案 ${a} 个,色阶 ${b} 组`);
|
||||
})();
|
||||
Reference in New Issue
Block a user