color

Functions

color2hex(color)

Convert a color in either supported format to a HEX string.

generate_rgb(exist_colors)

Generate new color which oppositely by exist colors.

get_predefined_colors(n)

hex2rgb(hex_value)

Convert HEX RGB string to integer RGB format.

random_rgb([fix_satlight])

Generate RGB color with fixed saturation and lightness.

rgb2hex(color)

Convert integer color format to HEX string.

validate_channel_value(value)

Generates ValueError if value not between 0 and 255.

Description

Color utilities.

color2hex(color)[source]

Convert a color in either supported format to a HEX string.

Accepts a HEX string or an RGB list. Useful on API boundaries, where the server expects HEX while the SDK stores RGB. Both forms are validated, so an invalid color fails here rather than as a server-side 400.

Parameters:
color : str or List[int, int, int]

HEX RGB string or list of integers in RGB format.

Raises:

ValueError – If the string is not a valid HEX RGB color, or a channel of the RGB list is out of range.

Returns:

HEX RGB string

Return type:

str

Usage Example:
import supervisely as sly

sly.color.color2hex([128, 64, 255])  # '#8040FF'
sly.color.color2hex('#8040FF')       # '#8040FF'
generate_rgb(exist_colors)[source]

Generate new color which oppositely by exist colors.

Parameters:
exist_colors : list

List of existing colors in RGB format.

Returns:

RGB integer values

Return type:

List[int, int, int]

Usage Example:
import supervisely as sly

exist_colors = [[0, 0, 0], [128, 64, 255]]
color = sly.color.generate_rgb(exist_colors)
print(color)
# Output: [15, 138, 39]
hex2rgb(hex_value)[source]

Convert HEX RGB string to integer RGB format.

Parameters:
hex_value : str

HEX RGB string.

Returns:

RGB integer values

Return type:

List[int, int, int]

Usage Example:
import supervisely as sly

hex_color = '#8040FF'
color = sly.color.hex2rgb(hex_color)
print(color)
# Output: [128, 64, 255]
random_rgb(fix_satlight=True)[source]

Generate RGB color with fixed saturation and lightness.

Returns:

RGB integer values

Return type:

List[int, int, int]

Usage Example:
import supervisely as sly

color = sly.color.random_rgb()
print(color)
# Output: [138, 15, 123]
rgb2hex(color)[source]

Convert integer color format to HEX string.

Parameters:
color : List[int, int, int]

List of existing colors in RGB format.

Returns:

HEX RGB string

Return type:

str

Usage Example:
import supervisely as sly

hex_color = sly.color.rgb2hex([128, 64, 255])
print(hex_color)
# Output: #8040FF
validate_channel_value(value)[source]

Generates ValueError if value not between 0 and 255.

Parameters:
value : int

Input channel value.

Raises:

ValueError – if value not between 0 and 255.

Returns:

None

Return type:

None