diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-23 11:12:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-23 17:10:58 -0300 |
| commit | a53a1aafb9804e943bdcd720c2bb688ecb90a888 (patch) | |
| tree | 197ee0ac4106037d49c5c9fc7a2b6be4186173bf /src/colors.zig | |
| parent | a0d367b6c54ff29737662fd7cf5a78d4d183cb20 (diff) | |
| download | pardes-a53a1aafb9804e943bdcd720c2bb688ecb90a888.tar.gz pardes-a53a1aafb9804e943bdcd720c2bb688ecb90a888.zip | |
Audit (colours, drawing): one Surface.tint, shared colour maths, the helix copies of hand-ported themes removed
The 22 <name>_helix themes that duplicate a hand-written port are gone;
monokai, ayu_dark and nord stay, since others inherit from them.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/colors.zig')
| -rw-r--r-- | src/colors.zig | 123 |
1 files changed, 30 insertions, 93 deletions
diff --git a/src/colors.zig b/src/colors.zig index 28136975..c016434b 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -4,12 +4,10 @@ const pardes = @import("pardes.zig"); const tagline = @import("tagline.zig"); const panes = @import("panes.zig"); const std = @import("std"); -const layout = @import("layout.zig"); const animation = @import("animation.zig"); const Color = @import("surface.zig").Color; const FontRole = @import("surface.zig").FontRole; const config = @import("config.zig"); -const limits = @import("memory.zig").limits; const pdf_enabled = panes.Pdf.enabled; const platform = pardes.platform; const theme_animation = @import("pardes_config").theme_animation; @@ -34,7 +32,7 @@ pub fn contrast(a: [3]u8, b: [3]u8) f32 { return (@max(x, y) + 0.05) / (@min(x, y) + 0.05); } -fn lerp(a: [3]u8, b: [3]u8, t: f32) [3]u8 { +pub fn lerp(a: [3]u8, b: [3]u8, t: f32) [3]u8 { var out: [3]u8 = undefined; for (&out, a, b) |*o, x, y| o.* = @intFromFloat(std.math.clamp(@round(@as(f32, @floatFromInt(x)) + (@as(f32, @floatFromInt(y)) - @as(f32, @floatFromInt(x))) * t), 0, 255)); return out; @@ -498,22 +496,7 @@ fn fold(comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme { } } -/// The imported themes (tools/gen_themes.zig), less any name one of ours -/// takes: a faithful port (`dracula`) keeps the plain name, and helix's -/// looser copy of it is still there as `dracula_helix` (the zed ones already -/// carry `_zed`). -fn foldImports(comptime ours: anytype, comptime C: type) [@typeInfo(C).@"struct".decl_names.len]Theme { - comptime { - @setEvalBranchQuota(400000); - var out = fold(C); - for (&out) |*g| for (ours) |c| if (std.mem.eql(u8, c.name, g.name)) { - g.name = g.name ++ "_helix"; - }; - return out; - } -} - -pub const themes = fold(curated) ++ foldImports(fold(curated), @import("generated_themes")); +pub const themes = fold(curated) ++ fold(@import("generated_themes")); /// The first `native_count` of `themes` are pardes's own; the rest imported. pub const native_count = @typeInfo(curated).@"struct".decl_names.len; @@ -589,18 +572,6 @@ test "lapis_plain is lapis on flat grounds, framed and shadowed as lapis" { /// Original and classic-inspired Pardes palettes precede retained imports. pub const native_theme_count = 15; -fn themeContrast(a: [3]u8, b: [3]u8) f64 { - var luminance: [2]f64 = .{ 0, 0 }; - for ([2][3]u8{ a, b }, 0..) |rgb, i| { - for (rgb, [3]f64{ 0.2126, 0.7152, 0.0722 }) |channel, weight| { - const srgb = @as(f64, @floatFromInt(channel)) / 255; - const linear = if (srgb <= 0.04045) srgb / 12.92 else std.math.pow(f64, (srgb + 0.055) / 1.055, 2.4); - luminance[i] += weight * linear; - } - } - return (@max(luminance[0], luminance[1]) + 0.05) / (@min(luminance[0], luminance[1]) + 0.05); -} - comptime { if (themes.len > std.math.maxInt(u16) + 1) @compileError("runtime config theme index no longer fits u16"); @@ -714,21 +685,11 @@ pub fn requestThemeFile(p: *Pardes, id: usize, argument: []const u8) void { var joined: [4095]u8 = undefined; const resolved = if (absolute) input else path: { const dir = p.opts.config_dir.?; - const separator = std.Io.Dir.path.sep; - const needs_separator = dir.len > 0 and dir[dir.len - 1] != separator; - const len = dir.len + @intFromBool(needs_separator) + input.len; - if (len > joined.len) { + const separator = if (dir.len > 0 and dir[dir.len - 1] != std.Io.Dir.path.sep) std.Io.Dir.path.sep_str else ""; + break :path std.fmt.bufPrint(&joined, "{s}{s}{s}", .{ dir, separator, input }) catch { p.reportError(id, "theme file", error.PathTooLong); return; - } - @memcpy(joined[0..dir.len], dir); - var at = dir.len; - if (needs_separator) { - joined[at] = separator; - at += 1; - } - @memcpy(joined[at..][0..input.len], input); - break :path joined[0..len]; + }; }; if (!p.theme_file_path.set(resolved)) { p.reportError(id, "theme file", error.PathTooLong); @@ -787,9 +748,7 @@ fn themeFileFailure(p: *Pardes, request: ThemeFileRequest, why: []const u8) void fn revertThemeFile(p: *Pardes) void { const loaded = p.theme_file_loaded.get(); if (std.mem.eql(u8, loaded, p.theme_file_path.get())) return; - var keep: [limits.host_path_cap]u8 = undefined; - @memcpy(keep[0..loaded.len], loaded); - if (loaded.len == 0) p.theme_file_path.clear() else _ = p.theme_file_path.set(keep[0..loaded.len]); + if (loaded.len == 0) p.theme_file_path.clear() else _ = p.theme_file_path.set(loaded); const generation = nextThemeFileGeneration(p); p.emit(.{ .theme_file = .{ .generation = generation, .on = loaded.len > 0 } }); } @@ -836,9 +795,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo const old = p.custom_theme; p.custom_theme = parsed; if (old) |theme_value| p.gpa.free(theme_value.name); - var settled: [limits.host_path_cap]u8 = undefined; - @memcpy(settled[0..request.path.len], request.path); - _ = p.theme_file_loaded.set(settled[0..request.path.len]); + _ = p.theme_file_loaded.set(request.path); invalidateThemeDependentRasters(p); _ = p.scratch.reset(.{ .retain_with_limit = pardes.memory.limits.scratch_kept }); p.sync(); @@ -1115,58 +1072,38 @@ test "native themes keep text and UI roles readable on their own surfaces" { for (themes[0..names.len], names) |th, name| { try std.testing.expectEqualStrings(name, th.name); const chrome = ChromeTheme.fromTheme(&th); - const minimum: f64 = if (std.mem.eql(u8, name, "ink") or std.mem.eql(u8, name, "daybreak")) 7 else 4.5; + const minimum: f32 = if (std.mem.eql(u8, name, "ink") or std.mem.eql(u8, name, "daybreak")) 7 else 4.5; const text_colors = [_][3]u8{ th.fg.?, th.kw, th.str, th.num, th.comment, chrome.diagnostic_error, chrome.diagnostic_warning, chrome.diagnostic_info, chrome.diagnostic_hint, }; - for (text_colors) |fg| try std.testing.expect(themeContrast(fg, th.bg.?) >= minimum); + for (text_colors) |fg| try std.testing.expect(contrast(fg, th.bg.?) >= minimum); // Gutters are orientation aids, not source text: stay visible without // competing with comments or pulling attention away from the body. - try std.testing.expect(themeContrast(th.lineno, th.bg.?) >= 2.5); - try std.testing.expect(themeContrast(th.lineno, th.bg.?) <= 4); - try std.testing.expect(themeContrast(th.lineno_active.?, th.bg.?) >= themeContrast(th.lineno, th.bg.?)); - try std.testing.expect(themeContrast(th.lineno_active.?, th.bg.?) <= 4.5); - try std.testing.expect(themeContrast(th.comment, th.bg.?) >= 5); - try std.testing.expect(themeContrast(th.comment, th.bg.?) >= themeContrast(th.lineno, th.bg.?) + 2); - try std.testing.expect(themeContrast(th.tag_fg, th.tag_bg) >= minimum); - try std.testing.expect(themeContrast(chrome.tag_active_fg, chrome.tag_active_bg) >= minimum); + try std.testing.expect(contrast(th.lineno, th.bg.?) >= 2.5); + try std.testing.expect(contrast(th.lineno, th.bg.?) <= 4); + try std.testing.expect(contrast(th.lineno_active.?, th.bg.?) >= contrast(th.lineno, th.bg.?)); + try std.testing.expect(contrast(th.lineno_active.?, th.bg.?) <= 4.5); + try std.testing.expect(contrast(th.comment, th.bg.?) >= 5); + try std.testing.expect(contrast(th.comment, th.bg.?) >= contrast(th.lineno, th.bg.?) + 2); + try std.testing.expect(contrast(th.tag_fg, th.tag_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_active_fg, chrome.tag_active_bg) >= minimum); try std.testing.expect(th.tag_name_fg != null); - try std.testing.expect(themeContrast(chrome.tag_name_fg, th.tag_bg) >= minimum); - try std.testing.expect(themeContrast(chrome.tag_active_name_fg, chrome.tag_active_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_name_fg, th.tag_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_active_name_fg, chrome.tag_active_bg) >= minimum); // Focus stays on the same tonal surface, rather than inverting a // whole command strip. Text remains readable in both states. - try std.testing.expect(themeContrast(chrome.tag_active_bg, th.tag_bg) <= 1.6); - try std.testing.expect(themeContrast(chrome.tag_active_fg, th.tag_fg) <= 1.6); - try std.testing.expect(themeContrast(th.sel_fg, th.sel_bg) >= minimum); - try std.testing.expect(themeContrast(chrome.search_fg, chrome.search_bg) >= minimum); + try std.testing.expect(contrast(chrome.tag_active_bg, th.tag_bg) <= 1.6); + try std.testing.expect(contrast(chrome.tag_active_fg, th.tag_fg) <= 1.6); + try std.testing.expect(contrast(th.sel_fg, th.sel_bg) >= minimum); + try std.testing.expect(contrast(chrome.search_fg, chrome.search_bg) >= minimum); // ANSI black can intentionally match the page (for backgrounds). All // other ANSI foregrounds, including bright black, remain legible. - for (th.palette.?[1..]) |fg| try std.testing.expect(themeContrast(fg, th.bg.?) >= minimum); + for (th.palette.?[1..]) |fg| try std.testing.expect(contrast(fg, th.bg.?) >= minimum); } } test "native filename tints change hue without changing perceived lightness" { - const Perceptual = struct { - fn lab(rgb: [3]u8) [3]f64 { - var linear: [3]f64 = undefined; - for (rgb, 0..) |channel, i| { - const value = @as(f64, @floatFromInt(channel)) / 255; - linear[i] = if (value <= 0.04045) value / 12.92 else std.math.pow(f64, (value + 0.055) / 1.055, 2.4); - } - const r = linear[0]; - const g = linear[1]; - const b = linear[2]; - const l = std.math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b); - const m = std.math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b); - const n = std.math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b); - return .{ - 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * n, - 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * n, - 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * n, - }; - } - }; for (themes[0..native_theme_count]) |th| { try std.testing.expect(th.tag_name_fg != null and th.tag_active_name_fg != null); const chrome = ChromeTheme.fromTheme(&th); @@ -1175,8 +1112,8 @@ test "native filename tints change hue without changing perceived lightness" { .{ chrome.tag_active_fg, chrome.tag_active_name_fg }, }; for (pairs) |pair| { - const base = Perceptual.lab(pair[0]); - const name = Perceptual.lab(pair[1]); + const base = toOklab(pair[0]); + const name = toOklab(pair[1]); // Half a lightness point allows 8-bit quantization without a // basename becoming visibly brighter or darker than its tag. try std.testing.expectApproxEqAbs(base[0], name[0], 0.005); @@ -1193,15 +1130,15 @@ test "native filename tints change hue without changing perceived lightness" { test "native dark palettes choose near-black or deliberately soft contrast" { for (themes[0..native_theme_count]) |th| { - if (themeContrast(th.bg.?, .{ 0, 0, 0 }) > 10) continue; // light paper + if (contrast(th.bg.?, .{ 0, 0, 0 }) > 10) continue; // light paper const soft = std.mem.eql(u8, th.name, "dusk") or std.mem.eql(u8, th.name, "lagoon") or std.mem.eql(u8, th.name, "solarium") or std.mem.eql(u8, th.name, "forge_soft"); if (soft) { - try std.testing.expect(themeContrast(th.fg.?, th.bg.?) >= 4.5); - try std.testing.expect(themeContrast(th.fg.?, th.bg.?) <= 7); + try std.testing.expect(contrast(th.fg.?, th.bg.?) >= 4.5); + try std.testing.expect(contrast(th.fg.?, th.bg.?) <= 7); } else { for (th.bg.?) |channel| try std.testing.expect(channel <= 0x14); - try std.testing.expect(themeContrast(th.fg.?, th.bg.?) >= 14); + try std.testing.expect(contrast(th.fg.?, th.bg.?) >= 14); } } const forge = curated.forge.theme; |
