Skip to main content

random_color_hex

A random color generator that produces visually distinct colors, unlike most libraries that rely on arbitrary gradient RGB separation.

This library is special in that it uses Gaurav Sharma's 2001 paper CIEDE2000 to calculate the distance between colors actually looks different instead of just being mathmatically different. Many versions of RGB generators do the latter, being separated by only some arbitrary number between colors: if they have smart color separation at all.

The library is also designed for easy integration with Matplotlib, or any plotting tool that accepts 6 digit hex codes. Perfect for adding something unique to plotting loss of an AI model, which was actually why it was originally created.

Version 3.0 is the final update (minus small bug fixes) for this library. Enjoy your colors all!

Installation

pip install random-color-hex

Quick Start

Plot Integrated Usage:

import random_color_hex as RCH
import matplotlib.pyplot as plt

x=list(range(1,13))
y1=[x**3-1 for x in x]
y2=[x**2-2 for x in x]
y3=[x-3 for x in x]

# Multiple distinct colors

plt.plot(x, y1, color=RCH.main())
plt.plot(x, y2, color=RCH.main())
plt.plot(x, y3, color=RCH.main())
plt.title("X Equations")
plt.show()

It will automatically separate the colors!

Using color=RCH.main(), as integrated into the plot function, is the intended use. You can make it a variable by "Variable=RCH.main()", but its designed for easy integration with matplotlib (color=RCH.main()).

To adjust how different the colors are, simply do:

"""
All parameters (like how_different_should_colors_be) are optional,
only RCH.main() is required to make a color.
"""
plt.plot(x, y1, color=RCH.main(how_different_should_colors_be='L'))
plt.plot(x, y2, color=RCH.main(how_different_should_colors_be='L'))

Jupyter/Non Local Usage:

import random_color_hex as RCH; RCH.jupyter_reset()

This is needed because the Smart Color Separation subroutine stores its colors as a class variable. When you restart your script on a local machine, it is designed to reset this variable so you can run it many times without storing the colors from past runs. However, in some online environments, this doesnt occur because of how they were designed. So, I made a function that specifically clears this to prevent problems in different environments.

Non-Seperated, RGB generated Color Seperated Colors

import random_color_hex as RCH

print(RCH.basic_main()) #Will print a random hex code

This generates a random color via RGB which is not separated. This is only put in to expand what the library can do, not really as a drawing feature.

Key Features/Input Parameters

Smart Color Separation

Uses CIEDE2000 algorithm to ensure colors are visually distinct, not just mathematically different.

# Control color separation, as shown earlier
how_different_should_colors_be='s'   # Slight difference (~975 colors)
how_different_should_colors_be='m'   # Clear difference (~99 colors, default)
how_different_should_colors_be='l'   # Very different (~51 colors)
how_different_should_colors_be='sl'  # Extremely different (~26 colors)

Brightness Control

# Avoid light colors (great for white backgrounds)
RCH.main(super_light_colors_allowed=False)

# Avoid dark colors (great for dark mode)
RCH.main(super_dark_colors_allowed=False)

# Mid-tones only
RCH.main(super_light_colors_allowed=False, super_dark_colors_allowed=False)

Tone Control

For all of these tone parameters: the higher the number, the more strict it is. So .5 will allow it to be more lenient than 2 (1 is default for all of these).

# kL: CIEDE2000 lightness weight. >1 = more tolerant of lightness differences.
RCH.main(kl=1) #Default is 1

# kC: CIEDE2000 chroma weight. >1 = more tolerant of saturation differences.
RCH.main(kc=2) #Default is 1

# kH: CIEDE2000 hue weight. >1 = more tolerant of hue differences.
RCH.main(kh=3) #Default is 1

Other Functions

# Get package credits and author information
RCH.credits()

# Display usage examples and help
RCH.help()

Examples

Multi-line Plot

import matplotlib.pyplot as plt
import random_color_hex as RCH

x = [1, 2, 3, 4, 5]
plt.plot(x, [1, 2, 3, 4, 5], color=RCH.main(), label='Linear')
plt.plot(x, [1, 4, 9, 16, 25], color=RCH.main(), label='Quadratic')
plt.plot(x, [1, 8, 27, 64, 125], color=RCH.main(), label='Cubic')
plt.legend()
plt.show()

Bar Chart

import matplotlib.pyplot as plt
import random_color_hex as RCH

categories = ['Python', 'R', 'Java', 'C++'] #Based off personal opinion :)
values = [100, 2, -100, 70]

for cat, val in zip(categories, values):
    plt.bar(cat, val, color=RCH.main(super_light_colors_allowed=False))
plt.show()

Stateful Generation

# Track color history across calls
generator = RCH.RandomColorHex()
color1 = generator.main()  # First color
color2 = generator.main()  # Guaranteed different from color1
color3 = generator.main()  # Different from both

This is an alternative to the much more simple RCH.main() setup. This comes from an older version of this, but I left it in as an option.

Technical Details

  • Zero dependencies - stdlib only
  • Python ≥3.11
  • Cryptographically random using secrets module
  • Auto-fallback: If color separation is too restrictive, falls back to simple random generation
  • License: Unlicense (public domain)

Metadata

Release files for random-color-hex 3.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for random-color-hex 3.0.5
File Size Uploaded
random_color_hex-3.0.5.tar.gz 12.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for random-color-hex 3.0.5
File Interpreter ABI Platform
random_color_hex-3.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 23.8 kB

Release files / random_color_hex-3.0.5.tar.gz

Download URL random_color_hex-3.0.5.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7dc5d6de0198d75024d53374ac6d5e412e096e98902fc8b9099ebc54895514c4
BLAKE2b-256 checksum
How to use checksums
7f4f8c60ac03df3429363b38da7a8ccf08c51aaff1e25073d1c6d2ecad0ae4b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / random_color_hex-3.0.5-py3-none-any.whl

Download URL random_color_hex-3.0.5-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3644a3892ddd04da7ec2f1465a7021199c8587b2f3b57459c5b06745fdde6297
BLAKE2b-256 checksum
How to use checksums
dfa6e8df1bd76f56b3ea29d18ce8ee645c371d3f6bd2e6cd89629cc21c0383bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

3.0.5 This release

2 release files

3.0.4

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.2.4

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0.9

2 release files

1.0.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page