goopylib.color

Color

Create colors by passing RGB arguments or a hexstring.

ColorRGB

Create an RGB color by passing RGB arguments with an optional alpha parameter.

ColorHex

Create a Hexadecimal color by passing a hexstring with an optional alpha parameter.

ColorCMYK

Create a CMYK color by passing cyan, magenta, yellow, key and optionally, alpha.

ColorHSV

Create an HSV color by passing hue (0-360), saturation (0-1), value (0-1) and optionally, alpha (0-1)

ColorHSL

Create an HSL color by passing hue (0-360), saturation (0-1), luminance (0-1) and optionally, alpha (0-1)

Random Colors

Use this module for simple random color generation in different formats.

random_rgb() ColorRGB
Returns

ColorRGB – a random RGB color

random_hex() ColorHex
Returns

ColorHex – a random Hexadecimal color

random_cmyk() ColorCMYK
Returns

ColorCMYK – a random CMYK color

random_hsv() ColorHSV
Returns

ColorHSV – a random HSV color

random_hsl() ColorHSL
Returns

ColorHSL – a random HSL color

RGB Conversions

rgb_to_hex(red: int, green: int, blue: int) str

Converts a color from RGB to a Hexadecimal string

Parameters
  • red (int) – between 0 and 255

  • green (int) – between 0 and 255

  • blue (int) – between 0 and 255

Returns

str – a hexstring in the format "#rrggbb"

Raises

ValueError – if the inputs are outside their bounds

rgb_to_cmyk(red: int, green: int, blue: int) tuple[float, float, float, float]

Converts a color from RGB to CMYK

Parameters
  • red – between 0 and 255

  • green – between 0 and 255

  • blue – between 0 and 255

Returns

a tuple of CMYK values between 0 and 1

Raises

ValueError – if the inputs are outside their bounds

rgb_to_hsv(red: int, green: int, blue: int) tuple[int, float, float]

Converts a color from RGB to HSV

Parameters
  • red – between 0 and 255

  • green – between 0 and 255

  • blue – between 0 and 255

Returns

a tuple of HSV values

Raises

ValueError – if the inputs are outside their bounds

rgb_to_hsl(red: int, green: int, blue: int) tuple[int, float, float]

Converts a color from RGB to HSL

Parameters
  • red – between 0 and 255

  • green – between 0 and 255

  • blue – between 0 and 255

Returns

a tuple of HSL values

Raises

ValueError – if the inputs are outside their bounds

Hex Conversions

hex_to_rgb(hexstring: str) tuple[int, int, int]

Converts a color hexstring to RGB

Parameters

hexstring – a valid color hexadecimal string

Returns

a tuple of RGB values between 0 and 255

Raises

ValueError – if the hexstring is invalid

hex_to_cmyk(hexstring: str) tuple[float, float, float, float]

Converts a color hexstring to CMYK

Parameters

hexstring – a valid color hexadecimal string

Returns

a tuple of CMYK values between 0 and 1

Raises

ValueError – if the hexstring is invalid

hex_to_hsv(hexstring: str) tuple[int, float, float]

Converts a color hexstring to HSV

Parameters

hexstring – a valid color hexadecimal string

Returns

a tuple of HSV values

Raises

ValueError – if the hexstring is invalid

hex_to_hsl(hexstring: str) tuple[int, float, float]

Converts a color hexstring to HSL

Parameters

hexstring – a valid color hexadecimal string

Returns

a tuple of HSL values

Raises

ValueError – if the hexstring is invalid

CMYK Conversions

cmyk_to_rgb(cyan: float, magenta: float, yellow: float, key: float) tuple[int, int, int]

Converts a color from CMYK to RGB

Parameters
  • cyan – between 0 and 1

  • magenta – between 0 and 1

  • yellow – between 0 and 1

  • key – between 0 and 1

Returns

a tuple of RGB values between 0 and 255

Raises

ValueError – if the inputs are outside their bounds

cmyk_to_hex(cyan: float, magenta: float, yellow: float, key: float) str

Converts a color from CMYK to a Hexadecimal String

Parameters
  • cyan (float) – between 0 and 1

  • magenta (float) – between 0 and 1

  • yellow (float) – between 0 and 1

  • key (float) – between 0 and 1

Returns

str – a hexstring in the format "#rrggbb"

Raises

ValueError – if the inputs are outside their bounds

cmyk_to_hsv(cyan: float, magenta: float, yellow: float, key: float) tuple[int, float, float]

Converts a color from CMYK to HSV

Parameters
  • cyan – between 0 and 1

  • magenta – between 0 and 1

  • yellow – between 0 and 1

  • key – between 0 and 1

Returns

a tuple of HSV values

Raises

ValueError – if the inputs are outside their bounds

cmyk_to_hsl(cyan: float, magenta: float, yellow: float, key: float) tuple[int, float, float]

Converts a color from CMYK to HSL

Parameters
  • cyan – between 0 and 1

  • magenta – between 0 and 1

  • yellow – between 0 and 1

  • key – between 0 and 1

Returns

a tuple of HSL values

Raises

ValueError – if the inputs are outside their bounds

HSV Conversions

hsv_to_rgb(hue: int, saturation: float, value: float) tuple[int, int, int]

Converts a color from HSV to RGB

Parameters
  • hue – between 0 and 360

  • saturation – between 0 and 1

  • value – between 0 and 1

Returns

a tuple of RGB values between 0 and 255

Raises

ValueError – if the inputs are outside their bounds

hsv_to_hex(hue: int, saturation: float, value: float) str

Converts a color from HSV to a Hexadecimal string

Parameters
  • hue (int) – between 0 and 360

  • saturation (float) – between 0 and 1

  • value (float) – between 0 and 1

Returns

str – a hexstring in the format "#rrggbb"

Raises

ValueError – if the inputs are outside their bounds

hsv_to_cmyk(hue: int, saturation: float, value: float) tuple[float, float, float, float]

Converts a color from HSV to CMYK

Parameters
  • hue – between 0 and 360

  • saturation – between 0 and 1

  • value – between 0 and 1

Returns

a tuple of CMYK values between 0 and 1

Raises

ValueError – if the inputs are outside their bounds

hsv_to_hsl(hue: int, saturation: float, value: float) tuple[int, float, float]

Converts a color from HSV to HSL

Parameters
  • hue – between 0 and 360

  • saturation – between 0 and 1

  • value – between 0 and 1

Returns

a tuple of HSL values

Raises

ValueError – if the inputs are outside their bounds

HSL Conversions

hsl_to_rgb(hue: int, saturation: float, luminance: float) tuple[int, int, int]

Converts a color from HSL to RGB

Parameters
  • hue – between 0 and 360

  • saturation – between 0 and 1

  • luminance – between 0 and 1

Returns

a tuple of RGB values between 0 and 255

Raises

ValueError – if the inputs are outside their bounds

hsl_to_hex(hue: int, saturation: float, luminance: float) str

Converts a color from HSL to a Hexadecimal string

Parameters
  • hue (int) – between 0 and 360

  • saturation (float) – between 0 and 1

  • luminance (float) – between 0 and 1

Returns

str – a hexstring in the format "#rrggbb"

Raises

ValueError – if the inputs are outside their bounds

hsl_to_cmyk(hue: int, saturation: float, luminance: float) tuple[float, float, float, float]

Converts a color from HSL to CMYK

Parameters
  • hue – between 0 and 360

  • saturation – between 0 and 1

  • luminance – between 0 and 1

Returns

a tuple of CMYK values between 0 and 1

Raises

ValueError – if the inputs are outside their bounds

hsl_to_hsv(hue: int, saturation: float, luminance: float) tuple[int, float, float]

Converts a color from HSL to HSV

Parameters
  • hue – between 0 and 360

  • saturation – between 0 and 1

  • luminance – between 0 and 1

Returns

a tuple of HSV values

Raises

ValueError – if the inputs are outside their bounds