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.2.tar.gz (41.8 kB view hashes)

Uploaded Source

Built Distribution

rgb_gradient-0.0.2-py3-none-any.whl (28.7 kB view hashes)

Uploaded Python 3

Supported by

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