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
-
style ⇢ dictionary:
ANSI font styles available for console applications.
-
text_color ⇢ dictionary:
Standard ANSI text colors available for console applications.
-
background ⇢ dictionary:
Standard ANSI background colors available for console applications.
Functions
text(value, color, bg)
Returns a terminal printable text with the given color (or style) and background if given.
Parameters
- string value
- int? color
- int? bg
Returns
- string
Notes
- The color argument can be replace with a style.
rgb_to_ansi256(r, g, b)
Converts RGB color to ASI-256 color number.
Parameters
- int r
- int g
- int b
Returns
- number
ansi256_to_ansi(code)
Converts ANSI-256 color number to ANSI-16 color number.
Parameters
- int code
Returns
- number
hex_to_rgb(h)
Converts the hexadecimal string h to its RGBA component
Parameters
- string h
Returns
- list
hex_to_ansi256(color)
Converts the given hexadecimal color to its ANSI-256 number.
Parameters
- string color
Returns
- number
hex_to_ansi(color)
Converts the given hexadecimal color to its ANSI-16 number.
Parameters
- string color
Returns
- number
Notes
- For use with
text()
, this should be prefered overhex_to_ansi256
hex(color)
Converts the given hexadecimal color to its terminal compatible color.
Parameters
- string color
Returns
- number
Notes
- For use with
text()
, this should be prefered overhex_to_ansi256
andhex_to_ansi
- color can include the '#' character. E.g.
#ff0
.
rgb(r, g, b)
Converts the given RGB color to its terminal compatible color.
Parameters
- number r
- number g
- number b
Returns
- number
hsl(h, s, l)
Converts the given HSL color to its terminal compatible color.
Parameters
- number h
- number s
- number l
Returns
- number
hsv(h, s, v)
Converts the given HSV color to its terminal compatible color.
Parameters
- number h
- number s
- number v
Returns
- number
hwb(h, w, b)
Converts the given HWB color to its terminal compatible color.
Parameters
- number h
- number w
- number b
Returns
- number
cmyk(c, m, y, k)
Converts the given CMYK color to its terminal compatible color.
Parameters
- number c
- number m
- number y
- number k
Returns
- number
xyz(x, y, z)
Converts the given XYZ color to its terminal compatible color.
Parameters
- number x
- number y
- number z
Returns
- number
rgb_to_hex(r, g, b, a)
Converts a RGB components into its corresponding hexadecimal color.
Parameters
- int r
- int g
- int b
- int? a
Returns
- string
rgb_to_hsl(r, g, b)
Converts a RGB color into its corresponding HSL components.
Parameters
- int r
- int g
- int b
Returns
- list[float]
rgb_to_hsv(r, g, b)
Converts a RGB color into its corresponding HSV components.
Parameters
- int r
- int g
- int b
Returns
- list[float]
rgb_to_hwb(r, g, b)
Converts a RGB color into its corresponding HWB components.
Parameters
- int r
- int g
- int b
Returns
- list[float]
rgb_to_cmyk(r, g, b)
Converts a RGB color into its corresponding CMYK components.
Parameters
- int r
- int g
- int b
Returns
- list[float]
rgb_to_xyz(r, g, b)
Converts a RGB color into its corresponding XYZ color space components.
Parameters
- int r
- int g
- int b
Returns
- list[float]
rgb_to_lab(r, g, b)
Converts a RGB color into its corresponding LAB color components.
Parameters
- int r
- int g
- int b
Returns
- list[float]
hsl_to_rgb(h, s, l)
Converts a HSL color into its corresponding RGB color components.
Parameters
- number h
- number s
- number l
Returns
- list[float]
hsl_to_hsv(h, s, l)
Converts a HSL color into its corresponding HSV color components.
Parameters
- number h
- number s
- number l
Returns
- list[float]
hsv_to_rgb(h, s, v)
Converts a HSV color into its corresponding RGB color components.
Parameters
- number h
- number s
- number v
Returns
- list[float]
hsv_to_hsl(h, s, v)
Converts a HSV color into its corresponding HSL color components.
Parameters
- number h
- number s
- number v
Returns
- list[float]
hwb_to_rgb(h, w, b)
Converts a HWB color into its corresponding RGB color components.
Parameters
- number h
- number w
- number b
Returns
- list[float]
cmyk_to_rgb(c, m, y, k)
Converts a CMYK color into its corresponding RGB color components.
Parameters
- number c
- number m
- number y
- number k
Returns
- list[float]
xyz_to_rgb(x, y, z)
Converts a XYZ color into its corresponding RGB color components.
Parameters
- number x
- number y
- number z
Returns
- list[float]