color - Colors

Color Class

class color.Color(*args: Any, **kwargs: Any)[source]

A color in 24-bit RGB.

You can create a new color from red, green, and blue:

yellow = Color(255, 255, 0)

You can get the HTML representation for a color using:

html_repr = str(Color(128, 128, 128))
assert html_repr == "#808080"
red

Red component of this color.

green

Green component of this color.

blue

Blue component of this color.

classmethod from_hex(color)[source]

Create a color from a hexadecimal number.

This function is available both as a class method and directly inside the color module:

Example:

from color import Color

# Magenta
Color.from_hex(0xff00ff)
import color

# Cyan
color.from_hex(0x00ffff)
classmethod from_hsv(hue, saturation, value)[source]

Create a color from a HSV tuple (hue, saturation, value).

This function is available both as a class method and directly inside the color module:

Example:

from color import Color

# Magenta
Color.from_hsv(300, 1, 1)
import color

# Cyan
color.from_hsv(180, 1, 1)

Code via https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative

classmethod from_hsl(hue, saturation, lightness)[source]

Create a color from a HSL tuple (hue, saturation, lightness).

This function is available both as a class method and directly inside the color module:

Example:

from color import Color

# Magenta
Color.from_hsl(300, 1, 0.5)
import color

# Cyan
color.from_hsv(180, 1, 0.5)

Code via https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative

to_hsv()[source]

Create a HSV tuple (hue, saturation, value) from this color.

Example:

from color import Color

# orange
c = Color.from_hex(0xff9900)
print(c.to_hsv())
# [36, 1.0, 1.0]
to_hsl()[source]

Create a HSL tuple (hue, saturation, lightness) from this color.

Example:

from color import Color

# orange
c = Color.from_hex(0xff9900)
print(c.to_hsl())
# [36, 1.0, 0.5]
change_hue(change)[source]

Change the hue of this color (doesn’t matter if HSL or HSV). Hue is always between 0 and 360.

Example:

from color import Color

# red
c = Color.from_hex(0xff0000)
print(c)
# #ff0000
c.change_hue(90)
print(c)
# #80ff00
change_saturation_hsv(change)[source]

Change the saturation of this color (in HSV). Saturation is always between 0 and 1.

Example:

from color import Color

# red
c = Color.from_hex(0xff0000)
print(c)
# #ff0000
c.change_saturation_hsv(-0.5)
print(c)
# #ff8080
change_saturation_hsl(change)[source]

Change the saturation of this color (in HSL). Saturation is always between 0 and 1.

Example:

from color import Color

# red
c = Color.from_hex(0xff0000)
print(c)
# #ff0000
c.change_saturation_hsl(-0.5)
print(c)
# #bf4040
change_value(change)[source]

Change the value of this color (in HSV). Value is always between 0 and 1.

Example:

from color import Color

# red
c = Color.from_hex(0xff0000)
print(c)
# #ff0000
c.change_value(-0.5)
print(c)
# #800000
change_lightness(change)[source]

Change the lightness of this color (in HSL). Lightness is always between 0 and 1.

Example:

from color import Color

# red
c = Color.from_hex(0xff0000)
print(c)
# #ff0000
c.change_lightness(0.2)
print(c)
# #ff6666
get_complementary()[source]

Get the complementary color of this color.

Example:

from color import Color

# red
c = Color.from_hex(0xff0000)
print(c)
# #ff0000
c.get_complementary()
print(c)
# #00ffff
darken(change)[source]

Darken a color. Uses change_saturation_hsl.

brighten(change)[source]

Brighten a color. Uses change_saturation_hsl.

color.from_hex(color)

Create a color from a hexadecimal number.

This function is available both as a class method and directly inside the color module:

Example:

from color import Color

# Magenta
Color.from_hex(0xff00ff)
import color

# Cyan
color.from_hex(0x00ffff)
color.from_hsl(hue, saturation, lightness)

Create a color from a HSL tuple (hue, saturation, lightness).

This function is available both as a class method and directly inside the color module:

Example:

from color import Color

# Magenta
Color.from_hsl(300, 1, 0.5)
import color

# Cyan
color.from_hsv(180, 1, 0.5)

Code via https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative

color.from_hsv(hue, saturation, value)

Create a color from a HSV tuple (hue, saturation, value).

This function is available both as a class method and directly inside the color module:

Example:

from color import Color

# Magenta
Color.from_hsv(300, 1, 1)
import color

# Cyan
color.from_hsv(180, 1, 1)

Code via https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative

Constants

The color module also contains a few constanst for commonly used colors:

Camp Colors

color.CHAOSBLUE
color.CHAOSBLUE_DARK
color.COMMYELLOW
color.COMMYELLOW_DARK
color.CAMPGREEN
color.CAMPGREEN_DARK

General

color.BLACK
color.WHITE
color.RED
color.GREEN
color.YELLOW
color.BLUE
color.MAGENTA
color.CYAN

htmlcolor - Color Constants

The htmlcolor module contains even more color constants. Note that loading the htmlcolor module will require ~12K of RAM.

htmlcolor.ALICEBLUE
htmlcolor.ANTIQUEWHITE
htmlcolor.AQUA
htmlcolor.AQUAMARINE
htmlcolor.AZURE
htmlcolor.BEIGE
htmlcolor.BISQUE
htmlcolor.BLACK
htmlcolor.BLANCHEDALMOND
htmlcolor.BLUE
htmlcolor.BLUEVIOLET
htmlcolor.BROWN
htmlcolor.BURLYWOOD
htmlcolor.CADETBLUE
htmlcolor.CHARTREUSE
htmlcolor.CHOCOLATE
htmlcolor.CORAL
htmlcolor.CORNFLOWERBLUE
htmlcolor.CORNSILK
htmlcolor.CRIMSON
htmlcolor.CYAN
htmlcolor.DARKBLUE
htmlcolor.DARKCYAN
htmlcolor.DARKGOLDENROD
htmlcolor.DARKGRAY
htmlcolor.DARKGREEN
htmlcolor.DARKKHAKI
htmlcolor.DARKMAGENTA
htmlcolor.DARKOLIVEGREEN
htmlcolor.DARKORANGE
htmlcolor.DARKORCHID
htmlcolor.DARKRED
htmlcolor.DARKSALMON
htmlcolor.DARKSEAGREEN
htmlcolor.DARKSLATEBLUE
htmlcolor.DARKSLATEGRAY
htmlcolor.DARKTURQUOISE
htmlcolor.DARKVIOLET
htmlcolor.DEEPPINK
htmlcolor.DEEPSKYBLUE
htmlcolor.DIMGRAY
htmlcolor.DODGERBLUE
htmlcolor.FIREBRICK
htmlcolor.FLORALWHITE
htmlcolor.FORESTGREEN
htmlcolor.FUCHSIA
htmlcolor.GAINSBORO
htmlcolor.GHOSTWHITE
htmlcolor.GOLD
htmlcolor.GOLDENROD
htmlcolor.GRAY
htmlcolor.GREEN
htmlcolor.GREENYELLOW
htmlcolor.HONEYDEW
htmlcolor.HOTPINK
htmlcolor.INDIANRED
htmlcolor.INDIGO
htmlcolor.IVORY
htmlcolor.KHAKI
htmlcolor.LAVENDER
htmlcolor.LAVENDERBLUSH
htmlcolor.LAWNGREEN
htmlcolor.LEMONCHIFFON
htmlcolor.LIGHTBLUE
htmlcolor.LIGHTCORAL
htmlcolor.LIGHTCYAN
htmlcolor.LIGHTGOLDENRODYELLOW
htmlcolor.LIGHTGRAY
htmlcolor.LIGHTGREEN
htmlcolor.LIGHTPINK
htmlcolor.LIGHTSALMON
htmlcolor.LIGHTSEAGREEN
htmlcolor.LIGHTSKYBLUE
htmlcolor.LIGHTSLATEGRAY
htmlcolor.LIGHTSTEELBLUE
htmlcolor.LIGHTYELLOW
htmlcolor.LIME
htmlcolor.LIMEGREEN
htmlcolor.LINEN
htmlcolor.MAGENTA
htmlcolor.MAROON
htmlcolor.MEDIUMAQUAMARINE
htmlcolor.MEDIUMBLUE
htmlcolor.MEDIUMORCHID
htmlcolor.MEDIUMPURPLE
htmlcolor.MEDIUMSEAGREEN
htmlcolor.MEDIUMSLATEBLUE
htmlcolor.MEDIUMSPRINGGREEN
htmlcolor.MEDIUMTURQUOISE
htmlcolor.MEDIUMVIOLETRED
htmlcolor.MIDNIGHTBLUE
htmlcolor.MINTCREAM
htmlcolor.MISTYROSE
htmlcolor.MOCCASIN
htmlcolor.NAVAJOWHITE
htmlcolor.NAVY
htmlcolor.OLDLACE
htmlcolor.OLIVE
htmlcolor.OLIVEDRAB
htmlcolor.ORANGE
htmlcolor.ORANGERED
htmlcolor.ORCHID
htmlcolor.PALEGOLDENROD
htmlcolor.PALEGREEN
htmlcolor.PALETURQUOISE
htmlcolor.PALEVIOLETRED
htmlcolor.PAPAYAWHIP
htmlcolor.PEACHPUFF
htmlcolor.PERU
htmlcolor.PINK
htmlcolor.PLUM
htmlcolor.POWDERBLUE
htmlcolor.PURPLE
htmlcolor.RED
htmlcolor.ROSYBROWN
htmlcolor.ROYALBLUE
htmlcolor.SADDLEBROWN
htmlcolor.SALMON
htmlcolor.SANDYBROWN
htmlcolor.SEAGREEN
htmlcolor.SEASHELL
htmlcolor.SIENNA
htmlcolor.SILVER
htmlcolor.SKYBLUE
htmlcolor.SLATEBLUE
htmlcolor.SLATEGRAY
htmlcolor.SNOW
htmlcolor.SPRINGGREEN
htmlcolor.STEELBLUE
htmlcolor.TAN
htmlcolor.TEAL
htmlcolor.THISTLE
htmlcolor.TOMATO
htmlcolor.TURQUOISE
htmlcolor.VIOLET
htmlcolor.WHEAT
htmlcolor.WHITE
htmlcolor.WHITESMOKE
htmlcolor.YELLOW
htmlcolor.YELLOWGREEN