Skip to main content

A simple module to handle colors

Project description

Verna

PyPI Build Status License: MIT

A simple module to handle colors

Only RGBA colors are supported at the moment.


Installation

You need Python>=3.6 to use Verna.

It can be installed from PyPI with pip using

pip install verna

Usage

Colors are represented using objects of class Color.

The color value is essentially stored as an integer as the integer attribute.

Alpha channel value can be set to denote opacity level.

The following properties can be used to access the different color components.

color.alpha
color.red
color.green
color.blue

where color is an instance of Color.

The different color components can be edited with one of the following values

  • an integer from 0 to 255 (eg: 0xff, 255)
  • a float from 0.0 to 1.0 (eg: 0.4)

A value outside of valid range would cause exception.

So, the following are valid:

color = Color(0x00ffff)
color.alpha = 0x80
color.alpha = 0.5 
color.red = 0xff        # Same as color.red = 255
color.green = 217
color.blue = 0xf5
color.blue = 0.75

whereas the following will cause error:

color = Color(0x00ffff)
color.alpha = 0x1ff    # > 0xff
color.alpha = -1       # < 0.0
color.alpha = 1.2      # > 1.0
color.alpha = True     # Invalid type: bool

Color component value conversions

Following functions can be used for basic inter-conversion for the color component values:

> Color.to_int()

Example:

>>> color = Color.from_name("red")      # 0xff0000
>>> red_int = Color.to_int(color.red)   # 255

Convert a float in the range of 0.0 till 1.0 towards an int in the range of 0 to 255.

> Color.to_float()

Example:

>>> color = Color.from_name("darkolivegreen")   # 0x556b2f
>>> red_float = Color.to_float(color.red)       # 0.1843137254901961

Convert an int in the range of 0 till 255 towards a float in the range of 0 to 0.1

Conversion to percentage values can be done by multiplying the float value obtained with Color.to_float() by 100.

>>> float_val = Color.to_float(54)  # 0.21176470588235294
>>> percentage = float_val * 100    # 21.176470588235294

Creating `Color` object

A Color object may be created in multiple ways.

From integer color code

For example, cyan (solid), which has color code 0x00ffff can be created like

Color(0x00ffff)

which is same as

Color(65535)
From color name

Color.from_name() can be used to create Color objects from a CSS3 color name.

For example, cyan can be created with

Color.from_name('cyan')
From RGBA values

Color.from_rgba() can be used to create an instance of Color from RGBA values.

Color.from_rgba(255, 255, 0)         # solid yellow
Color.from_rgba(255, 255, 0, 0.5)    # translucent yellow

Other methods

Color.replace(red, green, blue, alpha)

Create a new Color object by replacing some of the component values of a Color object.

Example:

>>> color = Color(0x8000ffff)
>>> color.replace(red=0x80, green=59, blue=0.5)   # color with hex value 0x80803b80

Color.rgba()

Returns the color component values of red, green, blue, alpha as a tuple.

Example:

>>> color = Color(0x8000ffff)
>>> color.rgba()
(0, 255, 255, 0.5)

hex()

Return hexcode of the color value in ARGB format as a string.

Example:

>>> color = Color(0x8000ffff)
>>> hex(color)
0x8000ffff

int()

Return color of the color value in ARGB format as an integer.

Example:

>>> color = Color(0x8000ffff)
>>> int(color)
2147549183

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

verna-1.0.0a1.tar.gz (8.8 kB view hashes)

Uploaded Source

Built Distribution

verna-1.0.0a1-py3-none-any.whl (7.8 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