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 details)

Uploaded Source

Built Distribution

verna-1.0.0a1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file verna-1.0.0a1.tar.gz.

File metadata

  • Download URL: verna-1.0.0a1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.9

File hashes

Hashes for verna-1.0.0a1.tar.gz
Algorithm Hash digest
SHA256 2b2e3ac504ff9018065aa6c57e0c74d59c589ce06d8141ca0771f04b456e852e
MD5 97a14bcc4265c6db3410112b681bde24
BLAKE2b-256 bea73ef381c25cdd97a6cf6f1ecac09925c505f81fe88fe534ccb48afe70d314

See more details on using hashes here.

File details

Details for the file verna-1.0.0a1-py3-none-any.whl.

File metadata

  • Download URL: verna-1.0.0a1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.9

File hashes

Hashes for verna-1.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 f65fd58df5bf43ed7bfcfe5a247465471b37502b01605fc53c3cc3733407eb93
MD5 5c0fa2f19bc9aa8dab58a71ac9ba204b
BLAKE2b-256 26a4ad608efbd256ff738469eb48d6a197d13ffbdcf8c6cbfe3cd0027bffb3e4

See more details on using hashes here.

Supported by

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