You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1.6 KiB

draft title aliases date series series_order author authorTwitter cover tags keywords summary showFullContent
true Deterministically Mapping Strings to RGB Colors
Deterministically Mapping Strings to RGB Colors
2025-01-25
strings-to-colors
1 Nick Dumas
golang
golang
Using Go so I don't have to think about color palettes. false

{{< lead >}} {{ </ lead >}}

Inspiration

The XMPP site has a document describing https://xmpp.org/extensions/xep-0392.html#algorithm-genpalette

The Plan

  • Take an arbitrary UTF-8 string
  • Generate an RGB or RGBA color code for it in a deterministic way
    • SHA1 the string
    • Interpret equally sized chunks of the hash as integers
    • Scale those integers into an 8-bit unsigned space

The Implementation

Hashing

{{< highlight go >}} {{</ highlight >}}

Interpolation

  • Not sure if interpolation is the right technical term
  • Using generics {{< highlight go >}} {{</ highlight >}}

The Results

Below is a sample of "lorem ipsum" with each word colored using this algorithm.

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

It doesn't look too bad.

Possible Improvements

  • Provide HSV or HSL color values
  • Light/dark mode awareness
  • Accessibility. The colors chosen are effectively random with no regard for color schemes, background, display device capability