Link Search Menu Expand Document

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

Properties


colors.style
ANSI font styles available for console applications
colors.text_color
Standard ANSI text colors available for console applications
colors.background
Standard ANSI background colors available for console applications

Functions


colors.text(value: string [, color: int [, bg: int]])
Returns a terminal printable text with the given color (or style) and background if given.
  • The color argument can be replace with a style.
return string
colors.rgb_to_ansi256(r: int, g: int, b: int)
converts RGB color to ASI-256 color number.
return number
colors.**ansi256to_ansi**(_code: int)
converts ANSI-256 color number to ANSI-16 color number
colors.hex_to_rgb(h: string)
converts the hexadecimal string h to its RGBA component
return list
colors.hex_to_ansi256(color: string)
Converts the given hexadecimal color to its ANSI-256 number
return number
colors.hex_to_ansi(color: string)
Converts the given hexadecimal color to its ANSI-16 number.
  • For use with text(), this should be prefered over hex_to_ansi256
return number
colors.hex(color: string)
Converts the given hexadecimal color to its terminal compatible color.
  • For use with text(), this should be prefered over hex_to_ansi256 and hex_to_ansi
  • color can include the ‘#’ character. E.g. #ff0.
return number
colors.rgb(r: int, g: int, b: int)
Converts the given RGB color to its terminal compatible color.
return number
colors.hsl(h: number, s: number, l: number)
Converts the given HSL color to its terminal compatible color.
return number
colors.hsv(h: number, s: number, v: number)
Converts the given HSV color to its terminal compatible color.
return number
colors.hwb(h: number, s: number, v: number)
Converts the given HWB color to its terminal compatible color.
return number
colors.cmyk(c: number, m: number, y: number, k: number)
Converts the given CMYK color to its terminal compatible color.
return number
colors.xyz(x: number, y: number, z: number)
Converts the given XYZ color to its terminal compatible color.
return number
colors.rgb_to_hex(r: int, g: int, b: int [, a: int])
Converts a RGB components into its corresponding hexadecimal color.
return string
colors.rgb_to_hsl(r: int, g: int, b: int)
Converts a RGB color into its corresponding HSL components.
return list[float]
colors.rgb_to_hsv(r: int, g: int, b: int)
Converts a RGB color into its corresponding HSV components.
return list[float]
colors.rgb_to_hwb(r: int, g: int, b: int)
Converts a RGB color into its corresponding HWB components.
return list[float]
colors.rgb_to_cmyk(r: int, g: int, b: int)
Converts a RGB color into its corresponding CMYK components.
return list[float]
colors.rgb_to_xyz(r: int, g: int, b: int)
Converts a RGB color into its corresponding XYZ color space components.
return list[float]
colors.rgb_to_lab(r: int, g: int, b: int)
Converts a RGB color into its corresponding LAB color components.
return list[float]
colors.hsl_to_rgb(h: number, s: number, l: number)
Converts a HSL color into its corresponding RGB color components.
return list[float]
colors.hsl_to_hsv(h: number, s: number, l: number)
Converts a HSL color into its corresponding HSV color components.
return list[float]
colors.hsv_to_rgb(h: number, s: number, v: number)
Converts a HSV color into its corresponding RGB color components.
return list[float]
colors.hsv_to_hsl(h: number, s: number, v: number)
Converts a HSV color into its corresponding HSL color components.
return list[float]
colors.hwb_to_rgb(h: number, w: number, b: number)
Converts a HWB color into its corresponding RGB color components.
return list[float]
colors.cmyk_to_rgb(c: number, m: number, y: number, k: number)
Converts a CMYK color into its corresponding RGB color components.
return list[float]
colors.xyz_to_rgb(x: number, y: number, z: number)
Converts a XYZ color into its corresponding RGB color components.
return list[float]

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.