colorhash

Package Version Hex Docs

Generate deterministic colors from strings - perfect for user avatars, data visualization, and category coloring.

gleam add colorhash@1

Demo

🎨 Try the interactive demo

Usage

Quick Start

import colorhash
import gleam_community/colour

pub fn main() {
  let hasher = colorhash.new()
  let assert Ok(color) = colorhash.to_color(hasher, "hello@example.com")

  colour.to_hsla(color)
  // -> #(0.3988995873452545, 0.35, 0.5, 1.0)

  colour.to_rgba(color)
  // -> #(0.32499999999999996, 0.675, 0.4626891334250345, 1.0)

  colour.to_rgb_hex_string(color)
  // -> "53AC76"
}

Custom Saturation

Control color intensity with custom saturation values:

// Grayscale only
let gray_hasher =
  colorhash.new()
  |> colorhash.with_saturations([0.0])

// Vivid colors only
let vivid_hasher =
  colorhash.new()
  |> colorhash.with_saturations([0.8, 0.9, 1.0])

Custom Lightness

Adjust brightness for different themes:

// Dark theme colors
let dark_hasher =
  colorhash.new()
  |> colorhash.with_lightnesses([0.2, 0.3, 0.4])

// Light theme colors
let light_hasher =
  colorhash.new()
  |> colorhash.with_lightnesses([0.7, 0.8, 0.9])

Custom Hue Ranges

Constrain colors to specific ranges:

// Warm colors only (reds, oranges, yellows)
let warm_hasher =
  colorhash.new()
  |> colorhash.with_hue_ranges([#(0.0, 0.17)])

// Cool colors only (blues, greens)
let cool_hasher =
  colorhash.new()
  |> colorhash.with_hue_ranges([#(0.33, 0.67)])

// Multiple distinct ranges
let brand_hasher =
  colorhash.new()
  |> colorhash.with_hue_ranges([
    #(0.0, 0.1),   // Reds
    #(0.55, 0.65), // Blues
  ])

Custom Hash Function

Use a different hash function for special requirements:

import gleam/string

// Simple hash based on string length
let length_hasher =
  colorhash.new()
  |> colorhash.with_hash_fun(string.length)

Further documentation can be found at https://hexdocs.pm/colorhash.

Development

gleam run   # Run the project
gleam test  # Run the tests
Search Document