From 2ba48f8784fec27b01da616329c9a2d6d0b1ba3e Mon Sep 17 00:00:00 2001 From: silverweed Date: Tue, 30 Jul 2024 11:34:08 +0200 Subject: [PATCH] brighten color on hover instead of using col_highlight --- src/render.cpp | 59 +++++++++++++++++++++++++++++------ src/root/RMicroFileReader.hxx | 2 ++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/render.cpp b/src/render.cpp index 0eb452a..ebd552f 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,3 +1,38 @@ +constexpr f32 max3(f32 a, f32 b, f32 c) +{ + return (a > b) ? (a > c) ? a : (b > c) ? b : c : (b > c) ? b : c; +} + +struct Color_Rgb { f32 r, g, b; }; + +// https://stackoverflow.com/questions/141855/programmatically-lighten-a-color +internal +Color_Rgb redist_rgb(Color_Rgb c) +{ + f32 r = c.r; + f32 g = c.g; + f32 b = c.b; + f32 threshold = 255.999; + f32 m = max3(r, g, b); + if (m <= threshold) + return { r, g, b }; + f32 total = r + g + b; + if (total >= 3 * threshold) + return { threshold, threshold, threshold }; + f32 x = (3 * threshold - total) / (3 * m - total); + f32 gray = threshold - x * m; + return { gray + x * r, gray + x * g, gray + x * b }; +} + +internal +Color_Rgb brighten(Color_Rgb c, f32 amt) +{ + f32 a = 1.f + amt; + Color_Rgb cb = { c.r * a, c.g * a, c.b * a }; + cb = redist_rgb(cb); + return cb; +} + internal void accum_dt_ms(Delta_Time_Accum &accum, f32 dt) { @@ -22,9 +57,13 @@ f32 calc_avg_dt_ms(const Delta_Time_Accum &accum) } internal -ImColor imcol(const f32 col[3]) +ImColor imcol(const f32 col[3], f32 brighten_amt = 0) { - return ImColor(col[0], col[1], col[2]); + Color_Rgb c = { col[0], col[1], col[2] }; + if (brighten_amt != 0) + c = brighten(c, brighten_amt); + + return ImColor(c.r, c.g, c.b); } internal @@ -33,17 +72,18 @@ u32 mem_edit_bg_color_fn(const u8 *, u64 off, void *user_data) App_State *app = reinterpret_cast(user_data); off += app->base_display_addr; + f32 brighten = 0; if (app->viewer.hovered_range.start <= off && off < app->viewer.hovered_range.end()) - return imcol(app->viewer.col_highlight); + brighten = 0.3; i64 hilite_cluster = app->viewer.highlight_cluster ? app->viewer.highlighted_cluster : -1; Section section = find_section(*app, off, hilite_cluster); - if (section.highlighted) return imcol(app->viewer.col_highlight); - if (section.id == Sec_Page && off == section.range.start) return imcol(app->viewer.col_page_start); - if (off < section.range.start) return imcol(app->viewer.col_key); - if (section.range.end() - section.post_size <= off && off < section.range.end()) return imcol(app->viewer.col_checksum); - return imcol(app->viewer.col_section[section.id]); + if (section.highlighted) return imcol(app->viewer.col_highlight, brighten); + if (section.id == Sec_Page && off == section.range.start) return imcol(app->viewer.col_page_start, brighten); + if (off < section.range.start) return imcol(app->viewer.col_key, brighten); + if (section.range.end() - section.post_size <= off && off < section.range.end()) return imcol(app->viewer.col_checksum, brighten); + return imcol(app->viewer.col_section[section.id], brighten); } internal @@ -101,8 +141,7 @@ void make_viewer(App_State &app, u16 n_cols) internal void viewer_jump_to(App_State &app, u64 addr) { - app.base_display_addr = addr; - app.viewer.mem_edit.GotoAddr = 0; + app.viewer.mem_edit.GotoAddr = addr; } internal diff --git a/src/root/RMicroFileReader.hxx b/src/root/RMicroFileReader.hxx index d8f7328..d68f48c 100644 --- a/src/root/RMicroFileReader.hxx +++ b/src/root/RMicroFileReader.hxx @@ -2,6 +2,8 @@ #include +// We are forced to use our own RNTuple anchor struct because we cannot +// write into the private members of the real RNTuple class from RMicroFileReader. struct RNTuple_Anchor { /// Version of the RNTuple binary format that the writer supports (see specification). /// Changing the epoch indicates backward-incompatible changes