ColorItem

Introduction

We keep color information and color logic together. ColorItem is a dataclass to simplify handling of color elements (RGB and others).

The class keeps native information about the RGB parts in dedicated fields. It can represent a color in one of the formats listed below.

  • RGB - ColorItem instance: this is the native representation of a color

  • RGB - tuple: You can retrieve color information as an RGB tuple. A red would become (255, 0, 0)

  • RGB - normalized tuple: a color in some systems has to be represented by float values from 0 to 1. The normalized tuple can represent this. The red example would look as follows: (1.0, 0.0, 0.0)

  • RGBA - tuple: PIL and others sometimes work with RGBA tuples. The color information is enriched with the Alpha information, in this release hardcoded as ‘255’. Our red would be represented as (255, 0, 0, 255)

  • RGB hexadecimal: the color can be sent as an RGB hexadecimal string.

A few conversion color methods are bundled together with color information.

Specification

ColorItem is a dataclass containing one Color definition and color converters.

The object contains the following values:

  • red: red component of color (integer)

  • green: green component of color (integer)

  • blue: blue component of color (integer)

Further, color format conversions - in between RGB, RGBA (integer representation) and float representations - are provided.

class colorwheels.color_item.ColorItem(red: int, green: int, blue: int)

Color object (dataclass) for easy color handling.

Parameters
  • red (int) – red color component. The native format is an integer 0-255

  • green (int) – green color component. The native format is an integer 0-255

  • blue (int) – blue color component. The native format is an integer 0-255

property color

The color property return color as tuple.

Returns

A tuple of 3 elements, red, green, blue. A red is returned as (255, 0, 0)

Return type

tuple

property color_hex

Return hexadecimal representation of color.

Returns

Hexadecimal coded string. A red is returned as #ff0000

Return type

str

property complement

Return complement (opposite) color tuple.

Returns

Finds a complementing color to current, and returns it as a tuple. Our example red color - (255, 0, 0) is complemented by (0, 255, 255) - cyan.

Return type

tuple

from_float(colrgb)

Convert a float RGB tuple the native format (int tuple)

This method comes in handy, if you use libraries like ‘Colour’ in your code.

The Colour library uses RGB float values, encoded in tuples ranging from 0.0-1.0. We convert these values to an int tuple, i.e. int values ranging from 0-255