diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 0ccdca091c8c7..eb2c15e996482 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -4569,6 +4569,7 @@ R_API int r_core_config_init(RCore *core) { SETCB ("scr.color.grep", "false", &cb_scr_color_grep, "enable colors when using ~grep"); SETB ("scr.color.pipe", "false", "enable colors when using pipes"); SETB ("scr.color.ops", "true", "colorize numbers and registers in opcodes"); + SETB ("scr.color.ops.unique", "false", "use unique colors for each immediate value"); SETCB ("scr.color.ophex", "false", &cb_scr_color_ophex, "colorize in hexdump depending on opcode type (px)"); SETB ("scr.color.args", "true", "colorize arguments and variables of functions"); SETB ("scr.color.bytes", "true", "colorize bytes that represent the opcodes of the instruction"); diff --git a/libr/core/disasm.c b/libr/core/disasm.c index ef4814db8b339..22b3545918576 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -728,6 +728,7 @@ static RDisasmState *ds_init(RCore *core) { ds->show_color_bytes = r_config_get_i (core->config, "scr.color.bytes"); ds->show_color_args = r_config_get_i (core->config, "scr.color.args"); ds->colorop = r_config_get_b (core->config, "scr.color.ops"); + core->print->unique_colors = r_config_get_b (core->config, "scr.color.ops.unique"); ds->show_utf8 = r_config_get_i (core->config, "scr.utf8"); ds->acase = r_config_get_b (core->config, "asm.ucase"); ds->capitalize = r_config_get_b (core->config, "asm.capitalize"); diff --git a/libr/include/r_util/r_print.h b/libr/include/r_util/r_print.h index 17e0b38c829b4..20f5f02d4e1a1 100644 --- a/libr/include/r_util/r_print.h +++ b/libr/include/r_util/r_print.h @@ -110,6 +110,7 @@ typedef struct r_print_t { int cols; int flags; bool use_comments; + bool unique_colors; int addrmod; int col; int stride; diff --git a/libr/util/print.c b/libr/util/print.c index 09a9d134566a4..ae2aed78a3705 100644 --- a/libr/util/print.c +++ b/libr/util/print.c @@ -2314,6 +2314,29 @@ static bool is_flag(const char *p) { return len > 3; } +// Helper function to generate unique color for immediate values +static const char* get_unique_imm_color(ut64 value) { + // Define a palette of visually distinct colors for immediates + static const char *color_palette[] = { + Color_BYELLOW, // Bright Yellow + Color_BCYAN, // Bright Cyan + Color_BMAGENTA, // Bright Magenta + Color_BGREEN, // Bright Green + Color_BRED, // Bright Red + Color_BBLUE, // Bright Blue + Color_YELLOW, // Yellow + Color_CYAN, // Cyan + Color_MAGENTA, // Magenta + Color_BLUE, // Blue + }; + + // Hash the value to get a consistent color index + const size_t palette_size = sizeof(color_palette) / sizeof(color_palette[0]); + size_t color_idx = value % palette_size; + + return color_palette[color_idx]; +} + R_API char* r_print_colorize_opcode(RPrint *print, char *p, const char *reg, const char *num, bool partial_reset, ut64 func_addr) { bool expect_reg = true; int i, j, k, is_mod, is_float = 0, is_arg = 0; @@ -2360,6 +2383,9 @@ R_API char* r_print_colorize_opcode(RPrint *print, char *p, const char *reg, con const char *name = print->offname (print->user, n)? color_flag: NULL; if (name) { num2 = name; + } else if (print->unique_colors) { + // Use unique color for each immediate value + num2 = get_unique_imm_color (n); } const size_t nlen = strlen (num2); if (nlen + j >= sizeof (o)) {