Skip to content

Add a Hash property transform to color content from a palette - #1065

Open
ChrisJr404 wants to merge 1 commit into
mierak:masterfrom
ChrisJr404:feat/hash-property-transform
Open

Add a Hash property transform to color content from a palette#1065
ChrisJr404 wants to merge 1 commit into
mierak:masterfrom
ChrisJr404:feat/hash-property-transform

Conversation

@ChrisJr404

Copy link
Copy Markdown

Closes #839.

What

Adds a new Hash property transform. It renders its content and applies a foreground color chosen from a configured colors palette based on a stable hash of the rendered text. The same value always maps to the same color while different values spread across the palette, which makes it easy to tell artists, years or albums apart at a glance in a list view.

Transform(Hash(
    content: Property(Artist),
    colors: ["red", "green", "blue", "magenta", "cyan"],
))

How

It mirrors the existing Truncate/Replace transforms:

  • a new Hash variant on both TransformFile (config) and Transform (runtime); the palette strings are parsed into ratatui colors once, during config conversion, in both properties.rs and queue_table.rs;
  • render hooks in the string / span / line paths (panes/mod.rs, song_ext.rs). The string path returns the content unchanged; the span and line paths override the foreground with the hashed color and leave the rest of the style intact.

The color is selected with a small FNV-1a hash so the mapping is stable across runs and platforms. An empty palette is a no-op (the content stays unstyled), so it degrades gracefully.

Tests

  • unit tests for the selection helper: determinism, palette membership, empty palette, single-color palette, and spread across the palette;
  • as_span tests for content pass-through, deterministic coloring from the palette, and the empty-palette no-op.

cargo test, cargo fmt (nightly) and cargo clippy are all clean.

@mierak mierak left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Documentation is needed before this can be merged too.

/// The same content always maps to the same color, while different content is
/// spread across the palette. Returns `None` when `colors` is empty so callers
/// can leave the content unstyled.
pub(crate) fn hashed_color(content: &str, colors: &[Color]) -> Option<Color> {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should live in the ui module not the config since its used during the application's lifecycle and not on startup.

content: Box<PropertyFile<T>>,
replacements: Vec<ReplacementFile<T>>,
},
Hash {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name Hash does not really describe the behavior at all. Something like Colorize/Palette/PaletteColor or something similar would be better.

},
Hash {
content: Box<PropertyFile<T>>,
colors: Vec<String>,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a whole Style instead of a simple fg color. Gives more freedom to users and lets them avoid contrast issues.

The new `Hash` transform renders its content and picks a foreground color
from a configured palette based on a stable hash of the resulting text, so
the same value (an artist, year, album, ...) always gets the same color
while different values are spread across the palette.

It follows the existing `Truncate`/`Replace` transforms: it wraps a
`content` property, takes a `colors` list, and plugs into the same config
conversion and string/span/line rendering paths.
@ChrisJr404

Copy link
Copy Markdown
Author

Rebased onto master to clear the conflict. Docs live in the separate site repo now, so I added the Hash transform to the Transforms table there in rmpc-org/rmpc-org.github.io#75. Thanks!

@mierak

mierak commented Aug 20, 2026

Copy link
Copy Markdown
Owner

What about the other points?

@mierak

mierak commented Sep 2, 2026

Copy link
Copy Markdown
Owner

Hi, are you still going to finish this?

@ChrisJr404

Copy link
Copy Markdown
Author

Yes, still on it, sorry for the delay. I agree with all three points: I will move it into the ui module since it runs during the application lifecycle rather than at startup, rename it to something that describes the behavior (Palette or Colorize rather than Hash), and take a full Style instead of just a foreground color so users can avoid contrast issues. I will push the revision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Style by hash of content

2 participants