Next | Previous

colors

This module provides functionalities for color conversion and manipulation. This module also provides functionalities that enable cross-platform colored terminal outputs that will allow you create beautiful console apps that are user friendly. RGB conversion to other colors that return a floating point or a list of floating points do so to allow users get absolute precision since its really easy for callers to do a math.round() on the components of the resulting list.

Example

The example below uses this module to create a success message that will print correctly on almost all terminals (Only Windows 10 version 1901+ supported. All linux and OSX terminals are supported). Try it out!

import colors
colors.text('Successful!', colors.text_color.green)

The text() function can be nested. For example,

colors.text(colors.text('Successful!', colors.style.bold), colors.text_color.green)

The module also features multiple functions for color conversion. For example,

%> import colors
%> colors.rgb_to_cmyk(103, 13, 69)
[0, 87.37864077669903, 33.00970873786409, 59.6078431372549]

The terminal colors also have simple wrappers that allow supplied colors to text() from various color formats. For example, we can specify the color from the HTML hexadecimal color.

import colors
colors.text('Colored text!', colors.hex('#fc0'))

Fields

colors.styledictionary
ANSI font styles available for console applications.
colors.text_colordictionary
Standard ANSI text colors available for console applications.
colors.backgrounddictionary
Standard ANSI background colors available for console applications.

Functions

colors.text(value, color, bg)

Returns a terminal printable text with the given color (or style) and background if given.

@notes:

  • The color argument can be replace with a style.
  • @params:
    • string value
    • int? color
    • int? bg
  • @returns: string
colors.rgb_to_ansi256(r, g, b)

Converts RGB color to ASI-256 color number.

  • @params:
    • int r
    • int g
    • int b
  • @returns: number
colors.ansi256_to_ansi(code)

Converts ANSI-256 color number to ANSI-16 color number.

  • @params:
    • int code
  • @returns: number
colors.hex_to_rgb(h)

Converts the hexadecimal string h to its RGBA component

  • @params:
    • string h
  • @returns: list
colors.hex_to_ansi256(color)

Converts the given hexadecimal color to its ANSI-256 number.

  • @params:
    • string color
  • @returns: number
colors.hex_to_ansi(color)

Converts the given hexadecimal color to its ANSI-16 number.

@notes:

  • For use with text(), this should be preferred over hex_to_ansi256
  • @params:
    • string color
  • @returns: number
colors.hex(color)

Converts the given hexadecimal color to its terminal compatible color.

@notes:

  • For use with text(), this should be preferred over hex_to_ansi256 and hex_to_ansi
  • color can include the '#' character. E.g. #ff0.
  • @params:
    • string color
  • @returns: number
colors.rgb(r, g, b)

Converts the given RGB color to its terminal compatible color.

  • @params:
    • number r
    • number g
    • number b
  • @returns: number
colors.hsl(h, s, l)

Converts the given HSL color to its terminal compatible color.

  • @params:
    • number h
    • number s
    • number l
  • @returns: number
colors.hsv(h, s, v)

Converts the given HSV color to its terminal compatible color.

  • @params:
    • number h
    • number s
    • number v
  • @returns: number
colors.hwb(h, w, b)

Converts the given HWB color to its terminal compatible color.

  • @params:
    • number h
    • number w
    • number b
  • @returns: number
colors.cmyk(c, m, y, k)

Converts the given CMYK color to its terminal compatible color.

  • @params:
    • number c
    • number m
    • number y
    • number k
  • @returns: number
colors.xyz(x, y, z)

Converts the given XYZ color to its terminal compatible color.

  • @params:
    • number x
    • number y
    • number z
  • @returns: number
colors.rgb_to_hex(r, g, b, a)

Converts a RGB components into its corresponding hexadecimal color.

  • @params:
    • int r
    • int g
    • int b
    • int? a
  • @returns: string
colors.rgb_to_hsl(r, g, b)

Converts a RGB color into its corresponding HSL components.

  • @params:
    • int r
    • int g
    • int b
  • @returns: list[float]
colors.rgb_to_hsv(r, g, b)

Converts a RGB color into its corresponding HSV components.

  • @params:
    • int r
    • int g
    • int b
  • @returns: list[float]
colors.rgb_to_hwb(r, g, b)

Converts a RGB color into its corresponding HWB components.

  • @params:
    • int r
    • int g
    • int b
  • @returns: list[float]
colors.rgb_to_cmyk(r, g, b)

Converts a RGB color into its corresponding CMYK components.

  • @params:
    • int r
    • int g
    • int b
  • @returns: list[float]
colors.rgb_to_xyz(r, g, b)

Converts a RGB color into its corresponding XYZ color space components.

  • @params:
    • int r
    • int g
    • int b
  • @returns: list[float]
colors.rgb_to_lab(r, g, b)

Converts a RGB color into its corresponding LAB color components.

  • @params:
    • int r
    • int g
    • int b
  • @returns: list[float]
colors.hsl_to_rgb(h, s, l)

Converts a HSL color into its corresponding RGB color components.

  • @params:
    • number h
    • number s
    • number l
  • @returns: list[float]
colors.hsl_to_hsv(h, s, l)

Converts a HSL color into its corresponding HSV color components.

  • @params:
    • number h
    • number s
    • number l
  • @returns: list[float]
colors.hsv_to_rgb(h, s, v)

Converts a HSV color into its corresponding RGB color components.

  • @params:
    • number h
    • number s
    • number v
  • @returns: list[float]
colors.hsv_to_hsl(h, s, v)

Converts a HSV color into its corresponding HSL color components.

  • @params:
    • number h
    • number s
    • number v
  • @returns: list[float]
colors.hwb_to_rgb(h, w, b)

Converts a HWB color into its corresponding RGB color components.

  • @params:
    • number h
    • number w
    • number b
  • @returns: list[float]
colors.cmyk_to_rgb(c, m, y, k)

Converts a CMYK color into its corresponding RGB color components.

  • @params:
    • number c
    • number m
    • number y
    • number k
  • @returns: list[float]
colors.xyz_to_rgb(x, y, z)

Converts a XYZ color into its corresponding RGB color components.

  • @params:
    • number x
    • number y
    • number z
  • @returns: list[float]