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.style ➝ dictionary
- ANSI font styles available for console applications.
- colors.text_color ➝ dictionary
- Standard ANSI text colors available for console applications.
- colors.background ➝ dictionary
- 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
- @params:
- colors.ansi256_to_ansi(code)
-
Converts ANSI-256 color number to ANSI-16 color number.
- @params:
- int code
- @returns: number
- @params:
- colors.hex_to_rgb(h)
-
Converts the hexadecimal string h to its RGBA component
- @params:
- string h
- @returns: list
- @params:
- colors.hex_to_ansi256(color)
-
Converts the given hexadecimal color to its ANSI-256 number.
- @params:
- string color
- @returns: number
- @params:
- colors.hex_to_ansi(color)
-
Converts the given hexadecimal color to its ANSI-16 number.
@notes:
- For use with
text()
, this should be preferred overhex_to_ansi256
- @params:
- string color
- @returns: number
- For use with
- colors.hex(color)
-
Converts the given hexadecimal color to its terminal compatible color.
@notes:
- For use with
text()
, this should be preferred overhex_to_ansi256
andhex_to_ansi
- color can include the '#' character. E.g.
#ff0
.
- @params:
- string color
- @returns: number
- For use with
- colors.rgb(r, g, b)
-
Converts the given RGB color to its terminal compatible color.
- @params:
- number r
- number g
- number b
- @returns: number
- @params:
- colors.hsl(h, s, l)
-
Converts the given HSL color to its terminal compatible color.
- @params:
- number h
- number s
- number l
- @returns: number
- @params:
- colors.hsv(h, s, v)
-
Converts the given HSV color to its terminal compatible color.
- @params:
- number h
- number s
- number v
- @returns: number
- @params:
- colors.hwb(h, w, b)
-
Converts the given HWB color to its terminal compatible color.
- @params:
- number h
- number w
- number b
- @returns: number
- @params:
- 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
- @params:
- colors.xyz(x, y, z)
-
Converts the given XYZ color to its terminal compatible color.
- @params:
- number x
- number y
- number z
- @returns: number
- @params:
- 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
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params:
- 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]
- @params: