diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 932101e..28e8e2d 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -513,15 +513,22 @@ // A `theme` event carries the signed-in user's appearance as `name=value` pairs joined by `;` // (the `` attributes). An empty value removes the attribute. Only data-* names are applied. function applyAppearance(data) { + var colours = null, sheet = null; String(data).split(';').forEach(function (pair) { var i = pair.indexOf('='); if (i < 0) { return; } var name = pair.slice(0, i), value = pair.slice(i + 1); // The custom colour sheet's href, or empty when no colour is a custom hue. - if (name === 'custom_css') { setCustomCss(value); return; } + if (name === 'custom_css') { sheet = value; return; } if (name.indexOf('data-') !== 0) { return; } + if (COLOUR_ATTRS.indexOf(name) >= 0) { colours = colours || {}; colours[name] = value; return; } if (value) { root.setAttribute(name, value); } else { root.removeAttribute(name); } }); + if (colours) { + var current = colourState(), next = {}; + COLOUR_ATTRS.forEach(function (n) { next[n] = n in colours ? colours[n] : current[n]; }); + showColours(next, sheet); + } } // A `language` event means the page text changed for this user: the page is re-rendered in place // (typed text, focus and scroll kept), as for any live change, even on a page with no stream. @@ -773,50 +780,218 @@ // The attributes match what the shell renders: theme `auto` has no data-theme, `dark` is dark, else light. var PREVIEW_ATTR = { accent_light: 'data-accent-light', accent_dark: 'data-accent-dark', base_light: 'data-base-light', base_dark: 'data-base-dark', sidebar_side: 'data-sidebar' }; - // The custom stylesheet sets the colours of the hue choices (`hue-N`). Its link follows the four - // colour attributes on : it is added when one of them is a hue, its href is kept in step, - // and it is removed when none is. - // An empty href removes the link. The server sends the href in the live payload (`custom_css`). - function setCustomCss(href) { - var link = document.head.querySelector('link[rel="stylesheet"][href^="/theme/custom.css"]'); + // The colour attributes a colour choice sets on , and the stylesheet that styles the hue choices + // (`hue-H` and `hue-H-vV`). That sheet is a whose href never changes once it is live. A new href + // gets a new link: its colours go on when it has loaded, and the live link stays until then, so a + // drag never paints the defaults. A request that a newer one overtakes before it loads is dropped. + var COLOUR_ATTRS = ['data-accent-light', 'data-accent-dark', 'data-base-light', 'data-base-dark']; + var colourWanted = null; // the colour attributes the page should show: the latest asked for + var colourLink = null; // the live sheet this script added (the server's is found in the head) + var pendingLink = null; // a sheet that is still loading + var colourSeq = 0; // bumps on every request; a load for an older request is dropped + var colourFrame = 0; + + function liveLink() { + if (!colourLink || !colourLink.isConnected) { + colourLink = document.head.querySelector('link[rel="stylesheet"][href^="/theme/custom.css"]'); + } + return colourLink; + } + + // The colour attributes the page should show now, read from the first time. + function colourState() { + if (!colourWanted) { + colourWanted = {}; + COLOUR_ATTRS.forEach(function (name) { colourWanted[name] = root.getAttribute(name) || ''; }); + } + return colourWanted; + } + + // The stylesheet href the four colours need, or '' when none of them is a hue. The server builds the same href. + function sheetHref(attrs) { + var vals = COLOUR_ATTRS.map(function (name) { return attrs[name] || ''; }); + if (!vals.some(function (v) { return /^hue-/.test(v); })) { return ''; } + return '/theme/custom.css?al=' + encodeURIComponent(vals[0]) + '&ad=' + encodeURIComponent(vals[1]) + + '&bl=' + encodeURIComponent(vals[2]) + '&bd=' + encodeURIComponent(vals[3]); + } + + function setColourAttrs(attrs) { + COLOUR_ATTRS.forEach(function (name) { + if (attrs[name]) { root.setAttribute(name, attrs[name]); } else { root.removeAttribute(name); } + }); + } + + // Shows a colour state. `href` is the sheet the server gave for it (a live payload), or null to build it here. + function showColours(attrs, href) { + colourWanted = attrs; + var seq = ++colourSeq; + if (href === null || href === undefined) { href = sheetHref(attrs); } + if (pendingLink) { pendingLink.remove(); pendingLink = null; } + var live = liveLink(); + var liveHref = live ? live.getAttribute('href') : ''; + if (href === liveHref) { setColourAttrs(attrs); return; } if (!href) { - if (link) { link.remove(); } + setColourAttrs(attrs); + if (live) { live.remove(); } + colourLink = null; return; } - if (!link) { - link = document.createElement('link'); - link.rel = 'stylesheet'; - document.head.appendChild(link); + var link = document.createElement('link'); + link.rel = 'stylesheet'; + link.setAttribute('href', href); + link.onload = function () { + if (pendingLink === link) { pendingLink = null; } + if (seq !== colourSeq) { link.remove(); return; } + setColourAttrs(attrs); + var old = liveLink(); + if (old && old !== link) { old.remove(); } + colourLink = link; + }; + link.onerror = function () { + if (pendingLink === link) { pendingLink = null; } + link.remove(); + }; + pendingLink = link; + document.head.appendChild(link); + } + + // A colour change sets one attribute. The request is made at most once a frame, however many events it takes. + function wantColour(name, value) { + var current = colourState(); + var next = {}; + COLOUR_ATTRS.forEach(function (n) { next[n] = current[n]; }); + next[name] = value; + colourWanted = next; + if (!colourFrame) { + colourFrame = requestAnimationFrame(function () { colourFrame = 0; showColours(colourWanted, null); }); } - if (link.getAttribute('href') !== href) { link.setAttribute('href', href); } } - // Builds the custom sheet's href from the four colour attributes on while a drag or a - // preset pick is shown before the save returns. The server builds the same href on the next load. - function syncCustomCss() { - var slots = ['data-accent-light', 'data-accent-dark', 'data-base-light', 'data-base-dark']; - var vals = slots.map(function (name) { return root.getAttribute(name) || ''; }); - var custom = vals.some(function (v) { return /^hue-\d+$/.test(v); }); - if (!custom) { setCustomCss(''); return; } - setCustomCss('/theme/custom.css?al=' + encodeURIComponent(vals[0]) + '&ad=' + encodeURIComponent(vals[1]) + - '&bl=' + encodeURIComponent(vals[2]) + '&bd=' + encodeURIComponent(vals[3])); + function clamp(v, lo, hi) { return Math.min(hi, Math.max(lo, v)); } + + // OKLCH and sRGB, as in the stylesheet's colours. A colour outside sRGB loses chroma, not lightness or hue. + function srgbEncode(v) { + v = clamp(v, 0, 1); + return v <= 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055; + } + function srgbDecode(v) { return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); } + function oklabToLinear(L, a, b) { + var l = L + 0.3963377774 * a + 0.2158037573 * b; + var m = L - 0.1055613458 * a - 0.0638541728 * b; + var s = L - 0.0894841775 * a - 1.2914855480 * b; + l = l * l * l; m = m * m * m; s = s * s * s; + return [ + 4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s, + -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s, + -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s + ]; + } + function oklchToLinear(L, C, h) { + var rad = h * Math.PI / 180; + return oklabToLinear(L, C * Math.cos(rad), C * Math.sin(rad)); + } + function inGamut(lin) { return lin.every(function (v) { return v >= -1e-4 && v <= 1 + 1e-4; }); } + function hexOfOklch(L, C, h) { + var lin = oklchToLinear(L, C, h); + if (!inGamut(lin)) { + var lo = 0, hi = C; + for (var i = 0; i < 24; i++) { + var mid = (lo + hi) / 2; + if (inGamut(oklchToLinear(L, mid, h))) { lo = mid; } else { hi = mid; } + } + lin = oklchToLinear(L, lo, h); + } + return '#' + lin.map(function (v) { + var byte = Math.round(srgbEncode(v) * 255); + return (byte < 16 ? '0' : '') + byte.toString(16); + }).join('').toUpperCase(); + } + function oklchOfHex(digits) { + var lin = [0, 2, 4].map(function (i) { return srgbDecode(parseInt(digits.substr(i, 2), 16) / 255); }); + var r = lin[0], g = lin[1], b = lin[2]; + var l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b); + var m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b); + var s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b); + var L = 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s; + var a = 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s; + var bb = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s; + var h = Math.atan2(bb, a) * 180 / Math.PI; + return { l: L, c: Math.sqrt(a * a + bb * bb), h: h < 0 ? h + 360 : h }; + } + + // The chroma a row's vividness stands for: the background's is small, as the stylesheet draws it. + function rowChroma(kind, vivid) { + return kind === 'base' ? 0.0004 * vivid : 0.03 + 0.16 * vivid / 100; + } + + function rowHue(row) { return parseInt(row.querySelector('input[data-hue-stripe]').value, 10) || 0; } + function rowVivid(row) { return parseInt(row.querySelector('input[data-vivid-stripe]').value, 10) || 0; } + + // Sets a row's sliders, its Custom value, and the row properties the tracks and knob read. The hex box + // shows the colour of the sliders when `fillHex` is set; otherwise it keeps what was typed. + function setRow(row, hue, vivid, fillHex) { + var hueSlider = row.querySelector('input[data-hue-stripe]'); + var vividSlider = row.querySelector('input[data-vivid-stripe]'); + var custom = row.querySelector('input[data-hue-custom]'); + var hex = row.querySelector('input[data-hex-input]'); + hueSlider.value = String(hue); + vividSlider.value = String(vivid); + vividSlider.setAttribute('aria-valuetext', vividSlider.getAttribute('data-vivid-label') + ' ' + vivid + '%'); + // Set through the CSSOM because the CSP forbids inline style attributes from the server. + row.style.setProperty('--hue', String(hue)); + row.style.setProperty('--v', String(vivid / 100)); + if (custom) { custom.value = 'hue-' + hue + '-v' + vivid; } + if (fillHex && hex) { + hex.value = hexOfOklch(parseFloat(row.getAttribute('data-lightness')), rowChroma(row.getAttribute('data-kind'), vivid), hue); + hex.removeAttribute('aria-invalid'); + } } - // A drag moves the stylesheet at most once a frame, however many input events it sends. - var previewFrame = 0; - function previewLater() { - if (previewFrame) { return; } - previewFrame = requestAnimationFrame(function () { previewFrame = 0; syncCustomCss(); }); + function clearNote(row) { + var note = row.querySelector('[data-hex-note]'); + if (note) { note.textContent = ''; } } - // Moves a row's stripe knob to a hue, and makes the row's Custom choice that hue (the value a save posts). - // The hue is also set on the row as `--hue`, which the stripe's knob and the Custom swatch read. + // A preset pick moves its row's knob to the preset's hue, without choosing Custom. function setKnob(row, hue) { - var range = row.querySelector('input[data-hue-stripe]'); + setRow(row, hue, rowVivid(row), true); + clearNote(row); + } + + // A hue or vividness slider: the row's Custom choice takes the new colour, and the colour shows on + // once its sheet has loaded. The save waits for the release (the change event). + function sliderMoved(el) { + var row = el.closest('[data-colour-row]'); + var custom = row ? row.querySelector('input[data-hue-custom]') : null; + if (!row || !custom) { return; } + setRow(row, rowHue(row), rowVivid(row), true); + clearNote(row); + custom.checked = true; + wantColour(PREVIEW_ATTR[custom.name], custom.value); + } + + // A typed hex: a valid one becomes the row's colour (its hue, and the vividness that its chroma gives), + // and the sliders follow. An invalid one is outlined and the last good colour stays. + function typedHex(el) { + var row = el.closest('[data-colour-row]'); + if (!row) { return; } + var match = /^#?([0-9a-fA-F]{6})$/.exec(el.value.trim()); + if (!match) { el.setAttribute('aria-invalid', 'true'); return; } + el.removeAttribute('aria-invalid'); + var lch = oklchOfHex(match[1]); + var kind = row.getAttribute('data-kind'); + var hue = Math.round(lch.h) % 360; + var vivid = kind === 'base' ? Math.round(lch.c / 0.0004) : Math.round((lch.c - 0.03) / 0.16 * 100); + vivid = clamp(vivid, 0, 100); + var note = row.querySelector('[data-hex-note]'); + if (note) { + var lightness = parseFloat(row.getAttribute('data-lightness')); + note.textContent = Math.abs(lch.l - lightness) > 0.08 ? note.getAttribute('data-message') : ''; + } + setRow(row, hue, vivid, false); var custom = row.querySelector('input[data-hue-custom]'); - if (range) { range.value = String(hue); } - row.style.setProperty('--hue', String(hue)); - if (custom) { custom.value = 'hue-' + hue; } + custom.checked = true; + wantColour(PREVIEW_ATTR[custom.name], custom.value); } document.addEventListener('change', function (e) { @@ -833,29 +1008,27 @@ return; } if (el.type === 'radio' && Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, el.name)) { - root.setAttribute(PREVIEW_ATTR[el.name], el.value); - // A preset moves its row's knob to the preset's hue, without choosing Custom. - var row = el.closest('[data-colour-row]'); - var hue = el.getAttribute('data-hue'); - if (row && hue) { setKnob(row, hue); } - syncCustomCss(); + var attr = PREVIEW_ATTR[el.name]; + if (COLOUR_ATTRS.indexOf(attr) >= 0) { + var row = el.closest('[data-colour-row]'); + var hue = el.getAttribute('data-hue'); + if (row && hue) { setKnob(row, parseInt(hue, 10)); } + wantColour(attr, el.value); + } else { + root.setAttribute(attr, el.value); + } } }, false); - // Dragging a stripe selects its Custom choice and shows the hue on while the drag lasts. - // The save waits for the release (the change event), which the autosubmit handler sends. + // Dragging a slider or typing a hex shows the colour as it goes. Both need a form that autosaves. document.addEventListener('input', function (e) { var el = e.target; - if (!el || el.type !== 'range' || !el.hasAttribute('data-hue-stripe') || !el.form || !el.form.hasAttribute('data-appearance')) { return; } - var row = el.closest('[data-colour-row]'); - var custom = row ? row.querySelector('input[data-hue-custom]') : null; - if (!custom) { return; } - setKnob(row, el.value); - custom.checked = true; - if (Object.prototype.hasOwnProperty.call(PREVIEW_ATTR, custom.name)) { - root.setAttribute(PREVIEW_ATTR[custom.name], custom.value); + if (!el || !el.form || !el.form.hasAttribute('data-appearance')) { return; } + if (el.type === 'range' && (el.hasAttribute('data-hue-stripe') || el.hasAttribute('data-vivid-stripe'))) { + sliderMoved(el); + } else if (el.hasAttribute('data-hex-input')) { + typedHex(el); } - previewLater(); }, false); // The theme the viewer sees: `dark` or `light` on , else the system's setting. @@ -873,9 +1046,9 @@ // The server renders each stripe's stored hue as the range value only, so the row's `--hue` (which // the Custom swatch reads) is set here from that value. Each row is set through the CSSOM because // the CSP forbids inline style attributes from the server. - document.querySelectorAll('.hue-row').forEach(function (row) { - var range = row.querySelector('input[data-hue-stripe]'); - if (range) { row.style.setProperty('--hue', range.value); } + document.querySelectorAll('[data-colour-row]').forEach(function (row) { + row.style.setProperty('--hue', String(rowHue(row))); + row.style.setProperty('--v', String(rowVivid(row) / 100)); }); document.querySelectorAll('[data-colour-group]').forEach(function (group) { var tabs = group.querySelectorAll('input[data-colour-tab]'); diff --git a/crates/forge-web/src/accent.rs b/crates/forge-web/src/accent.rs index 62f7426..a5d3327 100644 --- a/crates/forge-web/src/accent.rs +++ b/crates/forge-web/src/accent.rs @@ -76,7 +76,6 @@ pub fn custom_of(id: &str, default_vivid: u8) -> Option<(u16, u8)> { } /// The stored id of a custom accent or base in the `hue-H-vV` form, which `custom_of` reads back. -#[allow(dead_code)] // The settings picker writes its custom ids with this. pub fn custom_id(hue: u16, vivid: u8) -> String { format!("hue-{hue}-v{vivid}") } diff --git a/crates/forge-web/src/i18n/misc.rs b/crates/forge-web/src/i18n/misc.rs index 45b201c..414c720 100644 --- a/crates/forge-web/src/i18n/misc.rs +++ b/crates/forge-web/src/i18n/misc.rs @@ -32,6 +32,9 @@ pub enum K { AccentRowLabel, CustomLabel, HueLabel, + VividnessLabel, + HexLabel, + LightnessNote, BackgroundLabel, ChooseBase, // Appearance: which side the shell sidebar sits on. @@ -184,6 +187,9 @@ const ALL: &[K] = &[ K::AccentRowLabel, K::CustomLabel, K::HueLabel, + K::VividnessLabel, + K::HexLabel, + K::LightnessNote, K::BackgroundLabel, K::ChooseBase, K::SidebarLabel, @@ -346,6 +352,12 @@ pub fn t(lang: Lang, key: K) -> &'static str { (CustomLabel, Tr) => "Özel", (HueLabel, En) => "Hue", (HueLabel, Tr) => "Ton", + (VividnessLabel, En) => "Vividness", + (VividnessLabel, Tr) => "Canlılık", + (HexLabel, En) => "Hex", + (HexLabel, Tr) => "Hex", + (LightnessNote, En) => "Lightness is adjusted to keep text readable.", + (LightnessNote, Tr) => "Metnin okunur kalması için açıklık ayarlandı.", (BackgroundLabel, En) => "Background", (BackgroundLabel, Tr) => "Arka plan", (ChooseBase, En) => "Choose a background from the list.", diff --git a/crates/forge-web/src/pages/settings_appearance.rs b/crates/forge-web/src/pages/settings_appearance.rs index 0a32f17..9466c11 100644 --- a/crates/forge-web/src/pages/settings_appearance.rs +++ b/crates/forge-web/src/pages/settings_appearance.rs @@ -15,9 +15,10 @@ use topcoat::router::error::{SeeOther, see_other}; use topcoat::router::{page, route}; use topcoat::view::{View, component, view}; -use crate::accent::{self, hue_of, preset_accent_hue, preset_base_hue}; +use crate::accent::{self, custom_id, custom_of, hue_of, preset_accent_hue, preset_base_hue}; use crate::audit::{self, Actor, Target}; use crate::auth::{require_csrf, require_user}; +use crate::custom_colour; use crate::i18n::accents::{accent_name, base_name}; use crate::i18n::misc::{self as g, K}; use crate::i18n::{self, Lang}; @@ -76,27 +77,76 @@ struct Choice { hue: String, } -/// Where a hue stripe's knob sits. `custom` says the stored choice is that hue, rather than a -/// preset whose hue the knob was moved to. +/// Where a row's sliders sit: the hue, the vividness, and the hex box's colour. `custom` says the +/// stored choice is that colour, rather than a preset whose hue the knob was moved to. struct Stripe { hue: u16, + vivid: u8, custom: bool, + hex: String, } -/// The colour choices of one theme: a background and an accent, each a row of presets and a hue -/// stripe. `dark` picks the field names and the swatch attributes that belong to the dark theme. +/// The vividness a row starts at when its stored id gives none (a legacy `hue-N`): 50 for an accent, +/// 30 for a background. These are the defaults of `accent::custom_of`. +const ACCENT_VIVID: u8 = 50; +const BASE_VIVID: u8 = 30; + +/// The lightness a row's hex box and its note use, in one theme: the accent's, or the background's. +fn row_lightness(base: bool, dark: bool) -> f64 { + match (base, dark) { + (false, true) => 0.75, + (false, false) => 0.52, + (true, true) => 0.19, + (true, false) => 0.972, + } +} + +/// The `#RRGGBB` a row's hex box shows for a hue and vividness: the colour the sliders stand for, +/// at the row's lightness. The chroma is the one the stylesheet gives that vividness. +fn picker_hex(hue: u16, vivid: u8, base: bool, dark: bool) -> String { + let v = f64::from(vivid); + let chroma = if base { + 0.0004 * v + } else { + 0.03 + 0.16 * v / 100.0 + }; + custom_colour::colour(row_lightness(base, dark), chroma, f64::from(hue)) +} + +/// The colour choices of one theme: a background and an accent, each a row of presets and the hue, +/// vividness and hex controls. `dark` picks the field names and the swatch attributes that belong to +/// the dark theme. struct ColourGroup { dark: bool, background: &'static str, accent: &'static str, custom: &'static str, hue: &'static str, + vividness: &'static str, + hex: &'static str, + note: &'static str, bases: Vec, accents: Vec, base_stripe: Stripe, accent_stripe: Stripe, } +/// One row of a colour panel: its presets, then the Custom choice beside the hue, vividness and hex +/// controls. `base` picks the background's chips and the accent's when false. +struct ColourRow { + dark: bool, + base: bool, + legend: &'static str, + field: &'static str, + choices: Vec, + stripe: Stripe, + custom_label: &'static str, + hue_label: &'static str, + vivid_label: &'static str, + hex_label: &'static str, + note: &'static str, +} + struct AppearancePage { lang: Lang, csrf: String, @@ -185,15 +235,20 @@ fn shown<'a>(stored: &'a str, resolved: &'static str) -> &'a str { } } -/// The stripe of one row. A stored custom hue puts the knob on it; a preset puts the knob on the -/// preset's hue, and nothing is selected as custom. -fn stripe(stored: &str, preset: Option) -> Stripe { - match hue_of(stored) { - Some(hue) => Stripe { hue, custom: true }, - None => Stripe { - hue: preset.unwrap_or(0), - custom: false, - }, +/// The sliders of one row. A stored custom id (`hue-H-vV`, or the legacy `hue-H`) sets the hue and +/// vividness; a preset puts the hue knob on the preset's hue at the default vividness, and nothing +/// is selected as custom. +fn stripe(stored: &str, preset: Option, base: bool, dark: bool) -> Stripe { + let default_vivid = if base { BASE_VIVID } else { ACCENT_VIVID }; + let (hue, vivid, custom) = match custom_of(stored, default_vivid) { + Some((hue, vivid)) => (hue, vivid, true), + None => (preset.unwrap_or(0), default_vivid, false), + }; + Stripe { + hue, + vivid, + custom, + hex: picker_hex(hue, vivid, base, dark), } } @@ -209,6 +264,9 @@ fn colour_group(lang: Lang, dark: bool, base: &str, accent_id: &str) -> ColourGr accent: g::t(lang, K::AccentRowLabel), custom: g::t(lang, K::CustomLabel), hue: g::t(lang, K::HueLabel), + vividness: g::t(lang, K::VividnessLabel), + hex: g::t(lang, K::HexLabel), + note: g::t(lang, K::LightnessNote), bases: with_hues( choices(&base_codes, base_shown, |id| base_name(lang, id)), preset_base_hue, @@ -217,13 +275,84 @@ fn colour_group(lang: Lang, dark: bool, base: &str, accent_id: &str) -> ColourGr choices(&accent_codes, accent_shown, |id| accent_name(lang, id)), preset_accent_hue, ), - base_stripe: stripe(base, preset_base_hue(base_shown)), - accent_stripe: stripe(accent_id, preset_accent_hue(accent_shown)), + base_stripe: stripe(base, preset_base_hue(base_shown), true, dark), + accent_stripe: stripe(accent_id, preset_accent_hue(accent_shown), false, dark), } } -/// One theme's panel: a Background row and an Accent row. Each row is its presets as round -/// radios, then a hue stripe whose Custom radio carries the `hue-N` value. +/// One row of a colour panel: its presets as round radios, then the Custom choice, a hue slider, a +/// vividness slider and a hex box. The Custom radio carries `hue-H-vV`, and nav.js keeps it, the +/// sliders and the hex box in step while the row is used. +#[component] +async fn colour_row(row: ColourRow) -> Result { + let ColourRow { + dark, + base, + legend, + field, + choices, + stripe, + custom_label, + hue_label, + vivid_label, + hex_label, + note, + } = row; + let value = custom_id(stripe.hue, stripe.vivid); + let hue_value = stripe.hue.to_string(); + let vivid_value = stripe.vivid.to_string(); + let vivid_text = format!("{vivid_label} {}%", stripe.vivid); + let lightness = format!("{:.3}", row_lightness(base, dark)); + let kind = if base { "base" } else { "accent" }; + let custom_swatch = if base { + "base-swatch hue-swatch" + } else { + "accent-swatch hue-swatch" + }; + Ok(view! { +
+ (legend) +
+ for choice in choices.into_iter() { + + } +
+
+ +
+ +
+ + +
+ +
+
+
+ }) +} + +/// One theme's panel: a Background row and an Accent row. #[component] async fn colour_panel(group: ColourGroup) -> Result { let ColourGroup { @@ -232,6 +361,9 @@ async fn colour_panel(group: ColourGroup) -> Result { accent, custom, hue, + vividness, + hex, + note, bases, accents, base_stripe, @@ -240,58 +372,36 @@ async fn colour_panel(group: ColourGroup) -> Result { let base_field = if dark { "base_dark" } else { "base_light" }; let accent_field = if dark { "accent_dark" } else { "accent_light" }; let panel = if dark { "dark" } else { "light" }; - let base_custom = format!("hue-{}", base_stripe.hue); - let accent_custom = format!("hue-{}", accent_stripe.hue); + let base_row = ColourRow { + dark, + base: true, + legend: background, + field: base_field, + choices: bases, + stripe: base_stripe, + custom_label: custom, + hue_label: hue, + vivid_label: vividness, + hex_label: hex, + note, + }; + let accent_row = ColourRow { + dark, + base: false, + legend: accent, + field: accent_field, + choices: accents, + stripe: accent_stripe, + custom_label: custom, + hue_label: hue, + vivid_label: vividness, + hex_label: hex, + note, + }; Ok(view! {
-
- (background) -
- for choice in bases.into_iter() { - - } -
-
- - -
-
-
- (accent) -
- for choice in accents.into_iter() { - - } -
-
- - -
-
+ colour_row(row: base_row) + colour_row(row: accent_row)
}) } @@ -709,6 +819,25 @@ mod tests { assert_eq!(hue_of("sage"), None, "hue: preset id"); } + /// A stored `hue-95-v80` fills the hue and vividness sliders, and the Custom value is that id again. + #[test] + fn a_stored_hue_and_vividness_fill_the_sliders() { + let group = colour_group(Lang::En, true, "hue-95-v80", "hue-95-v80"); + let accent = &group.accent_stripe; + assert_eq!((accent.hue, accent.vivid), (95, 80), "accent: sliders"); + assert!(accent.custom, "accent: custom"); + assert_eq!( + custom_id(accent.hue, accent.vivid), + "hue-95-v80", + "accent: value" + ); + assert_eq!( + (group.base_stripe.hue, group.base_stripe.vivid), + (95, 80), + "base: sliders" + ); + } + #[test] fn labels_follow_the_page_language() { assert_eq!(theme_label(Lang::En, "dark"), "Dark"); diff --git a/crates/forge-web/tests/e2e/ui_prefs_e2e.rs b/crates/forge-web/tests/e2e/ui_prefs_e2e.rs index ca58fb3..afcc2e7 100644 --- a/crates/forge-web/tests/e2e/ui_prefs_e2e.rs +++ b/crates/forge-web/tests/e2e/ui_prefs_e2e.rs @@ -308,8 +308,10 @@ async fn light_and_dark_colours_save_independently_and_render_on_html() { .await; assert_eq!(saved.status, 303, "save: {}", saved.body); let page = env.http.get("/settings/appearance", Some(&session)).await; + // The Custom radio carries the hue at the default vividness; the stored id stays `hue-200`. + let checked = ["hue-200-v50", "gold", "spark", "dusk"].map(str::to_owned); + assert_eq!(checked_colours(&page.body), checked.map(Some)); let chosen = ["hue-200", "gold", "spark", "dusk"].map(str::to_owned); - assert_eq!(checked_colours(&page.body), chosen.clone().map(Some)); assert_eq!(html_colours(&page.body), chosen.map(Some)); // The newest accents and backgrounds save and show the same way. diff --git a/style/main.scss b/style/main.scss index 5f46801..515e96b 100644 --- a/style/main.scss +++ b/style/main.scss @@ -2504,28 +2504,61 @@ code { min-width: 0; } -// The hue stripe: a range input whose track is a rainbow, with a round knob. The background stripe -// is a pale, low-saturation band so it reads as a tint. nav.js sets --hue on the row: the knob and the -// Custom swatch take their colour from it. +// Each row's colour is drawn with oklch() from custom properties on the row. nav.js sets --hue (the +// hue) and --v (the vividness, 0..1) on the row; the panel sets the lightness, and the kind of row +// sets the chroma range. The background rows are drawn a little stronger than their real tint, so +// the hue shows on a pale page. The knob and the Custom swatch take the same colour as the track. [data-colour-row] { --hue: 0; + --v: 0.5; + --c-min: 0.03; + --c-span: 0.16; + --track-l: 0.75; + --track-c: calc(var(--c-min) + var(--c-span) * var(--v)); +} + +[data-colour-row][data-kind="base"] { + --v: 0.3; + --c-min: 0.02; + --c-span: 0.06; + --track-l: 0.8; +} + +[data-colour-panel="light"] [data-colour-row][data-kind="accent"] { + --track-l: 0.52; } .hue-row { - display: flex; - align-items: center; - gap: 12px; + display: grid; + grid-template-columns: 4.5rem minmax(0, 1fr); + align-items: start; + gap: 8px 12px; min-width: 0; - // The Custom choice takes the width of one preset, so the stripe lines up with the presets' row. + // The Custom choice takes the width of one preset, so the sliders line up beside the presets. > .colour-choice { - flex: none; width: 4.5rem; } } -.hue-stripe { - --track: linear-gradient(to right, hsl(0 85% 55%), hsl(60 90% 52%), hsl(120 65% 45%), hsl(180 70% 42%), hsl(240 80% 58%), hsl(300 75% 55%), hsl(360 85% 55%)); +// The sliders stack: the hue slider full width, then the vividness slider and the hex box on one line, +// then a note that shows only when a typed hex was lightened to keep text readable. +.tune { + display: grid; + gap: 6px; + min-width: 0; +} + +.tune-row { + display: flex; + align-items: center; + gap: 8px; + min-width: 0; +} + +// The hue and vividness tracks and knobs. Both are drawn from the row's custom properties. +.hue-stripe, +.vivid-stripe { -webkit-appearance: none; appearance: none; flex: 1 1 auto; @@ -2538,10 +2571,6 @@ code { background: transparent; cursor: pointer; - &.hue-stripe-base { - --track: linear-gradient(to right, hsl(0 45% 84%), hsl(60 45% 84%), hsl(120 35% 81%), hsl(180 35% 81%), hsl(240 40% 85%), hsl(300 35% 83%), hsl(360 45% 84%)); - } - &::-webkit-slider-runnable-track { height: 14px; border-radius: var(--radius-full); @@ -2563,7 +2592,7 @@ code { margin-top: -4px; border-radius: 50%; border: 3px solid var(--surface); - background: hsl(var(--hue) 80% 50%); + background: oklch(var(--track-l) var(--track-c) var(--hue)); box-shadow: 0 0 0 1px var(--border-strong); } @@ -2573,32 +2602,59 @@ code { height: 22px; border-radius: 50%; border: 3px solid var(--surface); - background: hsl(var(--hue) 80% 50%); + background: oklch(var(--track-l) var(--track-c) var(--hue)); box-shadow: 0 0 0 1px var(--border-strong); } - &.hue-stripe-base::-webkit-slider-thumb { - background: hsl(var(--hue) 40% 62%); - } - - &.hue-stripe-base::-moz-range-thumb { - background: hsl(var(--hue) 40% 62%); - } - &:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; } } -// The Custom swatch shows the hue the stripe is on. The accent one is saturated, the background -// one pale, like the presets beside it. -.hue-swatch { - background: hsl(var(--hue) 75% 50%); +// The hue track: the row's lightness and chroma, every 30 degrees around the wheel. +.hue-stripe { + --track: linear-gradient(to right, oklch(var(--track-l) var(--track-c) 0), oklch(var(--track-l) var(--track-c) 30), oklch(var(--track-l) var(--track-c) 60), oklch(var(--track-l) var(--track-c) 90), oklch(var(--track-l) var(--track-c) 120), oklch(var(--track-l) var(--track-c) 150), oklch(var(--track-l) var(--track-c) 180), oklch(var(--track-l) var(--track-c) 210), oklch(var(--track-l) var(--track-c) 240), oklch(var(--track-l) var(--track-c) 270), oklch(var(--track-l) var(--track-c) 300), oklch(var(--track-l) var(--track-c) 330), oklch(var(--track-l) var(--track-c) 360)); } -.base-swatch.hue-swatch { - background: hsl(var(--hue) 40% 82%); +// The vividness track: from no colour to full, at the current hue. +.vivid-stripe { + --track: linear-gradient(to right, oklch(var(--track-l) var(--c-min) var(--hue)), oklch(var(--track-l) calc(var(--c-min) + var(--c-span)) var(--hue))); +} + +// The hex box. A typed value that is not a colour is outlined in red and keeps the last good colour. +.hex-input { + flex: none; + box-sizing: border-box; + width: 96px; + height: 28px; + padding: 2px 8px; + border: 1px solid var(--border-strong); + border-radius: var(--radius-sm); + background: var(--surface); + color: var(--text); + font-family: var(--mono); + font-size: 0.8125rem; + + &[aria-invalid="true"] { + outline: 2px solid var(--danger); + outline-offset: 0; + } +} + +// The lightness note is empty, and so hidden, unless a typed colour was lightened. +.hex-note { + font-size: 0.75rem; + color: var(--muted); + + &:empty { + display: none; + } +} + +// The Custom swatch shows the colour its row's sliders stand for. +.hue-swatch { + background: oklch(var(--track-l) var(--track-c) var(--hue)); } .colour-group {