Skip to main content

Easily create gradients under the form of a list with given-input colors

Project description

rgb-gradient

A Python package to create gradients.

Installation

pip install rgb-gradient

Usage

from rgb_gradient import get_linear_gradient

intermediate_colors = [(255, 0, 0), '#c86496', (160, 150, 80), '#14f0b4', '#0000ff'] # colors format rgb or hex

gradient = get_linear_gradient(colors=intermediate_colors, nb_colors=10, return_format='hex')

print(gradient)

Output:

['#ff0000', '#dc4161', '#c86496', '#b1816e', '#a09650', '#65bc7a', '#14f0b4', '#13e7b7', '#0a74db', '#0000ff']

Specification

Parameters Type/Format
colors A list containing:
 • tuples (3 int elements from 0 to 255)
or/and
 • string (representing the hexadecimal color value under the format '#rrggbb'
 e.g. red = '#ff0000').

Requirements: len(colors) <= nb_colors and len(colors) >= 2
nb_colors An int indicating how many colors will compose the gradient (including the colors already given).

Requirements: nb_colors >= 3 and nb_colors >= len(colors)
return_format A string to indicate in which format the colors composing the gradient are returned.
It can take the value 'rgb' (decimal format) or 'hex' (hexadecimal format).

Default value: 'rgb'
Requirements: return_format == 'rgb' or return_format == 'hex'

How it works

The RGB color model is composed of 3 values (red, green, blue) from 0 to 255. With that in mind, we can easily represent a color as a point on a 3D plan.
With the input of the previous example, it looks like that:

All input colors of the previous example placed as points on a 3D plan

The get_linear_gradient function first determine the global distance from the first point to the last one, passing through the other colors given (if any).

See on the image above, the global distance is all black lines added together.

With that distance, we can figure out how far from the starting point to place each missing point (simply place a point every global_distance/(nb_points_to_add+1)).

Here's the colors we got by calling the function get_linear_gradient with the arguments of the previous example:

All output colors of the previous example placed as points on a 3D plan

We can see a total of 10 colors, including 5 colors given and 5 new colors.

Tips

Ok, that doesn't look like a real gradient for now. Let's call get_linear_gradient again with a total of 1000 colors (nb_colors=1000).

1000 colors placed as points on a 3D plan, forming a real gradient

Yeah, this is better ;) Of course, the more you ask for colors, the better your gradient will be.

Support

If you need help or found a bug, consider opening an issue on the project.

License

The source code for this project is licensed under the GPLv3 license, which you can find in the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rgb-gradient-0.0.1.tar.gz (41.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rgb_gradient-0.0.1-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file rgb-gradient-0.0.1.tar.gz.

File metadata

  • Download URL: rgb-gradient-0.0.1.tar.gz
  • Upload date:
  • Size: 41.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for rgb-gradient-0.0.1.tar.gz
Algorithm Hash digest
SHA256 6c260beaac7bf09b193d86544b6ccff77b191b9132830418fe5714461f522be2
MD5 7df00ed7c8166b93867f5173917c3d13
BLAKE2b-256 371a329dc5277856ece72f27e54c6ab003ccd0793e5e331073061568914047e4

See more details on using hashes here.

File details

Details for the file rgb_gradient-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: rgb_gradient-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for rgb_gradient-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d79b3e7cd5d2519829ba7ae6b884b6f1260947b56547be77bdc19e4d2b046747
MD5 8858ebb1479e811b00faa6c3a8942b1d
BLAKE2b-256 9e53d181dcf4f9e59b026e49b5985e826483ad8910c454aa1b95ccc7df97f643

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page