انتقل إلى المحتوى الرئيسي
الوصفة رقم 54

دليل الوصفات · الفصل 10 · الإخراج والدمج

أعد تلوين مستند كامل من لوحة ألوان واحدة

كل لون في الإعدادات مربوط بمدخل في لوحة الألوان، فيكفي تبديل لوحة الألوان لتنضيد برنامج مهرجان بالأحمر أو الفيروزي أو البنفسجي أو الرملي.

  • نموذج باللغة الإنجليزية: لا توجد طبعة عربية بعد
  • مقاس القص 200 × 200 مم
  • عمودان, فاصل 6 مم بين الأعمدة
  • Plus Jakarta Sans 9.4/13.4
  • Syne
  • صفحتان
  • المستوى
  • Postext 1.57.1
  • أُخرجت في 3 ms
  • أسطر الشيفرة: 178

باختصار

مطوية مربعة لمهرجان موسيقي معروضة بأربع مجموعات ألوان. تبيّن كيف تغيّر كل ألوان المستند دفعة واحدة باختيار مجموعة ألوان جديدة.

ما الذي ستنضده

برنامج Riverside Music Week، مهرجان متخيَّل في بلدة نهرية، مطبوعًا مطويةً مربعة قياسها 200 mm. للصفحة الأمامية حقل لوني يمتد على نصفها العلوي، فيه العنوان بخط Syne 800 بحجم 46 pt وموجة صوتية من أعمدة شاحبة مستديرة الأطراف؛ وتحته عمودان بحافة حرّة يسردان الأسبوع يومًا بيوم، ولكل فعالية وقت بالخط العريض وشارة للسعر. يرسم الظهر الأسبوع شبكةً من مربعات ملوّنة تحت شريط تعليق، ثم يسرد الأيام من الثلاثاء إلى الجمعة وينتهي بإطار للتذاكر تحت شريط ملوّن وبيانات الطبع. فوق الصفحات يعرض المثال أربع بروفات: بالأحمر والفيروزي والبنفسجي والرملي. تأتي ألوانها من سبعة مداخل في لوحة الألوان مسمّاة بحسب وظيفتها، إضافةً إلى main-color المضبوط على لون الحقل. انقر على بروفة فيُبنى المستند كله من جديد بلوحة الألوان تلك.

تجيب هذه الوصفة عن

  • كيف أمنع العناوين والكلمات العريضة والنقاط من الظهور باللون الأزرق؟
  • كيف ألوّن المصطلحات الرئيسية (العريضة أو المائلة) في المتن أو داخل الإطارات؟
  • كيف أضيف عيّنات لونية دليلية إلى النص أو التعليقات أو ملاحظات الجداول؟

الجواب المختصر

script.js · الأسطر 25–37في الشيفرة الكاملة
// col(id) links a colour to a palette entry. The link decides what is printed, in text, lists,
// boxes, chips, captions, table fills, swatches, design slots and references alike; the hex
// beside it (the house red's) only fills the field a colour must have.
const col = (id) => ({ hex: HOUSE[id], model: 'hex', paletteId: id });
const entries = (hexes) => Object.entries(hexes).map(([id, hex]) => ({ id, name: id,
  value: { hex, model: 'hex' } })); // the shape of config.colorPalette
// A colourway is a palette. retint() writes it into the one config; the next build sees the
// change and sets the whole programme in the new colours.
function retint(way) {
  const palette = { ...NEUTRALS, ...COLOURWAYS[way] };
  config.colorPalette = entries({ ...palette, 'main-color': palette.band }); // defaults: band
  return config;
}

المكونات

الخطوط
Syne, Plus Jakarta Sans (SIL OFL 1.1)
الأصول
لا شيء: كل صورة مرسومة بالشيفرة

طريقة التحضير

#1 · سمِّ الألوان بحسب وظيفتها

script.js · الأسطر 14–21في الشيفرة الكاملة
const NEUTRALS = { ink: '#1d1d1f', muted: '#5f5f66', paper: '#ffffff' };
const COLOURWAYS = { // band: colour fields · onBand: type on them · deep: accent type on paper
  red: { band: '#d7263d', onBand: '#ffffff', deep: '#b3122a', tint: '#fcdfe3' },
  teal: { band: '#2a9d8f', onBand: '#1d1d1f', deep: '#17695f', tint: '#d8eeeb' },
  violet: { band: '#6a4c93', onBand: '#ffffff', deep: '#5b3f86', tint: '#e7dff0' },
  sand: { band: '#f4a261', onBand: '#1d1d1f', deep: '#a14a16', tint: '#fde4cf' },
}; // white on teal is 3.3:1 and on sand 2.1:1, so those two set their band type in ink
const HOUSE = { ...NEUTRALS, ...COLOURWAYS.red }; // the house edition: red

يملأ band الحقول اللونية، ويلوّن onBand الحروف المنضَّدة عليها، وdeep هو لون الإبراز للحروف على الورق الأبيض، وtint هو التعبئة الفاتحة. تضبط كل مجموعة ألوان هذه الأربعة وتتشارك الألوان المحايدة الثلاثة. على الأبيض يبلغ تباين الحقل الفيروزي 3.3:1 والحقل الرملي 2.1:1، وهي نسبة أدنى مما يلزم لحروف بيضاء على الحقل أو لحروف بلون الحقل على الورق. تضبط هاتان اللوحتان onBand على لون الحبر، وتنضّد اللوحات الأربع الخط العريض والعناوين بلون deep، وهو درجة أغمق يتراوح تباينها على الأبيض بين 5.99:1 (الرملي) و8.40:1 (البنفسجي).

#2 · اربط كل ما كان المحرّك سيطبعه بالأزرق

script.js · الأسطر 68–94في الشيفرة الكاملة
const bodyText = { fontFamily: SANS, fontSize: pt(BODY), lineHeight: pt(LEAD),
  color: col('ink'), italicColor: col('ink'), // bold: the times, key terms, boxes (inherited)
  boldColor: col('deep'), referenceColor: col('deep'), textAlign: 'left', firstLineIndent: pt(0) };
const headings = { fontFamily: DISPLAY, color: col('deep'), levels: [
  // The programme opens on a recto.
  { level: 1, span: 'page', breakBefore: { enabled: true, parity: 'odd' },
    marginTop: pt(0), marginBottom: pt(0), advancedDesign: opener },
  { level: 2, fontSize: pt(12.5), lineHeight: lines(1), marginTop: lines(1), marginBottom: pt(0) },
] };
const unorderedLists = { color: col('band'), indent: mm(3.6), gap: mm(1.4),
  marginTop: pt(0), marginBottom: pt(0) };
const chip = { fontSize: pt(7.5), bold: true, borderWidth: pt(0), borderRadius: mm(1.6),
  paddingX: mm(1.3), paddingY: pt(0.9), gap: mm(1) };
const chipStyles = [ // band = a ticket, tint = free: the same code as the grid overleaf
  { id: 'ticket', background: col('band'), color: col('onBand'), ...chip },
  { id: 'free', background: col('tint'), color: col('deep'), ...chip,
    borderWidth: pt(0.6), borderColor: col('band') }, // the outline keeps it off the white
];
const corner = (edge, y) => ({ anchor: { to: 'page', edge }, offset: { x: mm(OUTER), y: mm(y) } });
const furniture = (id, content, edge, y, style) => ({ elements: [{ kind: 'text', id, content,
  parity: 'even', pages: 'body', overflow: 'wrap', placement: corner(edge, y), ...style }] });
const header = furniture('head', '{title} · {subtitle}', 'top-left', 12, // page 2's head
  { ...caps(7.5), color: col('deep') });
const footer = furniture('folio', '{pageNumber}', 'bottom-left', -12, { fontFamily: SANS,
  fontSize: pt(8), fontWeight: 700, color: col('onBand'), box: { backgroundColor: col('band'),
    borderRadius: mm(2.4), padding: { top: mm(0.9), right: mm(2.4), bottom: mm(0.9),
      left: mm(2.4) } } });

أزرق المحرّك مدخل في لوحة الألوان، main-color (#295AA3)، وقيمه الافتراضية توجّه إليه العناوين والخط العريض والمائل والإحالات ونقاط القوائم والترويسة. هنا لكل واحد من هذه معرّف من لوحة الألوان: الأوقات والمصطلحات الأساسية بالعريض بلون deep، والمائل بلون الحبر، ونقاط القوائم بلون band، وفي الصفحة 2 ترويسة بلون deep ورقم الصفحة بلون onBand على كبسولة بلون band. توجّه retint() أيضًا main-color إلى لون الحقل؛ ولأن هذه الإعدادات تضبط كل لون، لا يتغير شيء في هذه الصفحات، لكن أي نمط يُضاف لاحقًا من غير لون يأخذ لون الحقل بدل الأزرق. لا يضبط إطار التذاكر لونًا خاصًا للخط العريض، فيرث bodyText.boldColor مع معرّفه في لوحة الألوان، ويتغير خطه العريض مع مجموعة الألوان.

#3 · ارسم العمل الفني بصناديق التصميم

script.js · الأسطر 48–64في الشيفرة الكاملة
const text = (id, content, x, y, width, style) => ({ kind: 'text', id, content, align: 'left',
  overflow: 'wrap', color: col('onBand'), placement: { anchor: { to: 'page', edge: 'top-left' },
    offset: { x: mm(x), y: mm(y) }, size: { width: mm(width) } }, ...style });
const opener = { enabled: true,
  minHeight: lines(18), // 85.1 mm: text 7 mm under the band (2.4 mm with the band box alone)
  slot: { elements: [
    { kind: 'box', id: 'band', style: { backgroundColor: col('band') },
      placement: { anchor: { to: 'bleed', edge: 'top-left' },
        size: { width: 'fill', height: mm(BAND) } } },
    ...waveform(), // boxes filled with col('tint'): one more link the palette decides
    text('kicker', '{attr.kicker}', INNER, 14, 150, caps(8)),
    text('title', '{titleText}', INNER, 20, TRIM - INNER - OUTER, { fontFamily: DISPLAY,
      fontSize: pt(46), fontWeight: 800, lineHeight: 0.92 }),
    text('standfirst', '{attr.standfirst}', INNER, 71, 92, { fontFamily: SANS, fontSize: pt(10),
      lineHeight: 1.36 }),
  ] },
};

صفحة الافتتاح خانة تصميم تحوي صندوقًا ينزف خارج الحافة العليا، و29 عمودًا مستدير الأطراف للموجة الصوتية، وثلاثة عناصر نصية: نص العنوان نفسه عنوانًا، والسطر التمهيدي والمقدمة من سماته. الأعمدة صناديق لا SVG، فتعبئتها رابط آخر إلى tint يتبع لوحة الألوان؛ أما SVG فكان سيلزم رسمه وتسجيله من جديد لكل مجموعة ألوان. لو تُرك صندوق الحقل وحده لبدأ النص على سطر الشبكة الواقع 2.4 mm تحت الحقل؛ أما minHeight، أي 18 سطرًا أو 85.1 mm تحت الهامش العلوي، فيحجز مساحة أكبر وينقل السطر الأول إلى 7 mm تحته.

#4 · دع لوحة الألوان تملأ الجدول

script.js · الأسطر 176–199في الشيفرة الكاملة
const DAYS = t({ en: ['Sat 12', 'Sun 13', 'Mon 14', 'Tue 15', 'Wed 16', 'Thu 17', 'Fri 18'],
  es: ['Sáb 12', 'Dom 13', 'Lun 14', 'Mar 15', 'Mié 16', 'Jue 17', 'Vie 18'] });
const WEEK = [ // t: a ticketed concert, f: a free event, one mark a day from Saturday
  [t({ en: 'Quay Stage', es: 'Escenario del Muelle' }), 't.f.fft'],
  [t({ en: 'Iron Bridge steps', es: 'Escalinata del Puente' }), 'f.....f'],
  [t({ en: 'Market Hall', es: 'Mercado de Abastos' }), '.f...f.'],
  [t({ en: 'St Clare’s Cloister', es: 'Claustro de Santa Clara' }), '.t.....'],
  [t({ en: 'Tannery Yard', es: 'Patio de la Curtiduría' }), '...t...'],
  [t({ en: 'Boathouse', es: 'Casa de las Barcas' }), '.ft..t.'],
];
const fill = { t: col('band'), f: col('tint') }; // cell fills are palette links too
const resources = [{ id: 'week', typeId: 'table', kind: 'table', createdAt: 0, updatedAt: 0,
  placement: { position: 'top', span: 'page' }, // cited on page 1, it heads page 2
  caption: t({ en: 'The week at a glance', es: 'La semana de un vistazo' }),
  note: t({ en: ':swatch{color="band"} ticketed concert   :swatch{color="tint"} free event',
    es: ':swatch{color="band"} concierto con entrada   :swatch{color="tint"} acto gratuito' }),
  altText: t({ en: 'A grid of six venues by seven days; filled squares mark the events.',
    es: 'Una cuadrícula de seis escenarios por siete días; los cuadros rellenos son los actos.' }),
  table: { model: { headerRowCount: 1, columnWidths: [2.6, 1, 1, 1, 1, 1, 1, 1], rows: [
    [{ content: '', isHeader: true, background: col('paper') }, ...DAYS.map((day) => ({
      content: day, isHeader: true, align: 'center' }))],
    ...WEEK.map(([venue, marks]) => [{ content: venue, align: 'right' }, ...[...marks].map((m) =>
      (m === '.' ? { content: '' } : { content: '', background: fill[m] }))]),
  ] } } }];

الشبكة مورد من نوع جدول، خارج الإعدادات. ومع ذلك في تعبئات خلاياها معرّفات من لوحة الألوان، ويأخذ المحرّك قيمها من لوحة ألوان المستند حين يُخرج الجدول؛ وكذلك تُؤخذ قيم :swatch{color="band"} و:swatch{color="tint"} في مفتاح ملحوظة الجدول. تستعمل الشارات في القوائم المعرّفين نفسيهما، فيتطابق المربع المملوء والشارة بجانب الوقت وعيّنة المفتاح في كل مجموعة ألوان.

#5 · بدّل لوحة الألوان وابنِ من جديد

script.js · الأسطر 236–265في الشيفرة الكاملة
const NAMES = t({ en: { red: 'Red', teal: 'Teal', violet: 'Violet', sand: 'Sand' },
  es: { red: 'Rojo', teal: 'Verde azulado', violet: 'Violeta', sand: 'Arena' } });
document.getElementById('pages').insertAdjacentHTML('beforebegin', `<section id="editions">
  <div class="desk"><header><p class="kicker">${t({ en: 'Riverside Music Week · proofs',
    es: 'Música en la Ribera · pruebas' })}</p><h2>${TITLE}</h2></header><canvas id="live"
  role="img"></canvas><div class="buttons" role="group"></div></div></section>`);
const paint = (canvas, doc) => renderPageToCanvas(doc.pages[0], doc, canvas,
  { scale: (canvas.clientWidth * Math.min(devicePixelRatio, 2)) / doc.pages[0].width });
const buttons = Object.keys(COLOURWAYS).map((way) => {
  const button = document.querySelector('#editions .buttons')
    .appendChild(Object.assign(document.createElement('button'), { type: 'button' }));
  button.innerHTML = `<canvas></canvas><span>${NAMES[way]}<i>${['band', 'deep', 'tint']
    .map((id) => `<b style="background:${COLOURWAYS[way][id]}"></b>`).join('')}</i></span>`;
  paint(button.firstChild, docs[way]);
  // The same config with another palette on every click; the fonts are loaded by now.
  button.onclick = () => show(way, buildDocument({ markdown, resources }, retint(way)));
  return [way, button];
});
const live = document.getElementById('live');
let shown; // the document on the live page
function show(way, doc) {
  paint(live, (shown = doc));
  live.ariaLabel = `${NAMES[way]}, ${t({ en: 'page 1', es: 'página 1' })}`;
  for (const [id, button] of buttons) button.ariaPressed = String(id === way);
  showPages(doc, { title: `${TITLE} · ${NAMES[way]}` });
}
show('red', docs.red);
new ResizeObserver(() => { // canvases are bitmaps: repaint them at the desk's new size
  paint(live, shown); buttons.forEach(([way, button]) => paint(button.firstChild, docs[way]));
}).observe(live);

يستدعي كل زر retint()، التي تُسند مداخل مجموعة الألوان إلى colorPalette في كائن الإعدادات الوحيد، ثم يبني من جديد. يحلّل المحرّك الإعدادات مرة واحدة ويخزّن النتيجة مقترنةً بذلك الكائن؛ ومنذ postext 1.25 يتحقق كل بناء أولًا من أن الكائن ما زال كما كان حينها، فيُحلَّل من جديد كل تغيير يُجرى في الكائن نفسه. وتتبعه الروابط كلها: النص والقوائم والشارات وشريط التعليق، ومعها الحقل والموجة الصوتية والترويسة وكبسولة رقم الصفحة و«Table 1». وتحتفظ البروفات الأربع بألوانها حين تتغير لوحة الألوان مرة أخرى، لأن كل مستند يحتفظ بالألوان التي أُخرج بها. يبني المثال هذه البروفات بـbuildDocumentWithFonts؛ وعند النقر على زر تكون الخطوط قد حُمّلت، فتكفي buildDocument.

الوصفة كاملة

Sandbox
// ═══ Postext Cookbook · Nº 054 · Retint a whole document from one palette ═══════════
// https://postext.dev/en/cookbook/live-palette-retint
// Code: MIT · Text: original (CC BY 4.0) · Artwork: design boxes generated in code (CC BY 4.0)
// Fonts: Syne, Plus Jakarta Sans (SIL OFL 1.1) · Needs postext ≥ 1.25.0
import {
  buildDocument, buildDocumentWithFonts, renderPageToCanvas, defaultResourceTypes,
} from 'https://esm.sh/postext';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'live-palette-retint';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: three neutrals every edition shares, four entries each colourway sets
const NEUTRALS = { ink: '#1d1d1f', muted: '#5f5f66', paper: '#ffffff' };
const COLOURWAYS = { // band: colour fields · onBand: type on them · deep: accent type on paper
  red: { band: '#d7263d', onBand: '#ffffff', deep: '#b3122a', tint: '#fcdfe3' },
  teal: { band: '#2a9d8f', onBand: '#1d1d1f', deep: '#17695f', tint: '#d8eeeb' },
  violet: { band: '#6a4c93', onBand: '#ffffff', deep: '#5b3f86', tint: '#e7dff0' },
  sand: { band: '#f4a261', onBand: '#1d1d1f', deep: '#a14a16', tint: '#fde4cf' },
}; // white on teal is 3.3:1 and on sand 2.1:1, so those two set their band type in ink
const HOUSE = { ...NEUTRALS, ...COLOURWAYS.red }; // the house edition: red
// #endregion

// #region answer: every colour is a link to a palette entry; retint() swaps the palette
// col(id) links a colour to a palette entry. The link decides what is printed, in text, lists,
// boxes, chips, captions, table fills, swatches, design slots and references alike; the hex
// beside it (the house red's) only fills the field a colour must have.
const col = (id) => ({ hex: HOUSE[id], model: 'hex', paletteId: id });
const entries = (hexes) => Object.entries(hexes).map(([id, hex]) => ({ id, name: id,
  value: { hex, model: 'hex' } })); // the shape of config.colorPalette
// A colourway is a palette. retint() writes it into the one config; the next build sees the
// change and sets the whole programme in the new colours.
function retint(way) {
  const palette = { ...NEUTRALS, ...COLOURWAYS[way] };
  config.colorPalette = entries({ ...palette, 'main-color': palette.band }); // defaults: band
  return config;
}
// #endregion

const [DISPLAY, SANS, BODY, LEAD] = ['Syne', 'Plus Jakarta Sans', 9.4, 13.4]; // pt: 33 lines
const [TRIM, TOP, BOTTOM, INNER, OUTER, GUTTER] = [200, 22, 22, 17, 15, 6]; // mm: square, mirrored
const BAND = 100; // mm, trim top to the colour field's foot ('bleed' is the trim: no cut lines)
const lines = (n) => pt(n * LEAD);
const caps = (size) => ({ fontFamily: SANS, fontSize: pt(size), fontWeight: 700,
  letterSpacing: pt(size * 0.16), textTransform: 'uppercase' });

// #region opener: the colour field, a waveform of palette-linked boxes, the title on top
const text = (id, content, x, y, width, style) => ({ kind: 'text', id, content, align: 'left',

يعمل ملف script.js المجمّع كما هو: الصقه في سكربت الوحدة (module) لأي صفحة، أو افتح الوصفة على CodePen. مجلد الوصفة على GitHub ↗ (يفتح في تبويب جديد)

تنويعات

#أبقِ الخط العريض بلون الحبر

اترك لون الإبراز للعناوين ونقاط القوائم والشارات، ونضّد الأوقات والمصطلحات الأساسية بلون النص.

-  boldColor: col('deep'), referenceColor: col('deep'), textAlign: 'left', firstLineIndent: pt(0) };
+  boldColor: col('ink'), referenceColor: col('deep'), textAlign: 'left', firstLineIndent: pt(0) };

#أعطِ كل جزء من الكتاب لونه الخاص

للألوان التي تتغير داخل مستند واحد جزءًا بعد جزء، انظر أجزاء ملوّنة من سمة واحدة.

أخطاء شائعة

  • تحتفظ الصورة بالألوان التي رُسمت بها. لا يتبع ملف SVG ولا الصورة الفوتوغرافية لوحة الألوان؛ لذلك صُنعت الموجة الصوتية من صناديق تصميم.
  • المعرّف الذي لا تحويه لوحة الألوان يعود إلى قيمة hex المكتوبة بجانبه. إن أخطأت في كتابة paletteId بقي ذلك اللون بالأحمر الأصلي في كل مجموعات الألوان.

الحقوق

الوصفة
Ignacio Ferro
النص
نص أصلي, CC BY 4.0
الخطوط
Syne (SIL OFL 1.1) · Plus Jakarta Sans (SIL OFL 1.1)
الشيفرة
MIT، مثل Postext
Sandbox