Implementation:Google deepmind Dm control Variation Colors
| Knowledge Sources | |
|---|---|
| Domains | Reinforcement_Learning, Domain_Randomization |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
Variation classes for randomizing colors in RGB, HSV, and grayscale color spaces, always producing RGBA output arrays for use in MuJoCo visual domain randomization.
Description
The colors module provides three Variation subclasses that allow independent randomization of color channels in different color spaces. All classes produce RGBA numpy arrays as output, making them directly compatible with MuJoCo's rgba attributes on geoms, materials, and other visual elements.
RgbVariation accepts independent variation objects (or constant values) for the R, G, B, and alpha channels. When called, it evaluates each channel variation and assembles them into a four-element RGBA numpy array. HsvVariation operates in the HSV color space, accepting variations for hue, saturation, value, and alpha. It evaluates the HSV components, converts to RGB using Python's colorsys.hsv_to_rgb, and appends the alpha channel. The HSV color space is particularly useful for controlled randomization because hue, saturation, and brightness can be independently varied in perceptually meaningful ways.
GrayVariation is a convenience subclass of HsvVariation that fixes hue and saturation to zero, leaving only the gray level (value channel) and alpha as variable parameters. This simplifies the creation of grayscale color variations.
Usage
Use these classes when building Composer tasks that require visual domain randomization of object colors. HsvVariation is preferred when you want to randomize hue independently from brightness, while RgbVariation gives direct control over individual RGB channels. Bind these variations to MJCF element rgba attributes through a MJCFVariator.
Code Reference
Source Location
- Repository: Google_deepmind_Dm_control
- File: dm_control/composer/variation/colors.py
- Lines: 1-107
Signature
class RgbVariation(base.Variation):
def __init__(self, r, g, b, alpha=1.0):
...
class HsvVariation(base.Variation):
def __init__(self, h, s, v, alpha=1.0):
...
class GrayVariation(HsvVariation):
def __init__(self, gray_level, alpha=1.0):
...
Import
from dm_control.composer.variation import colors
from dm_control.composer.variation.colors import RgbVariation, HsvVariation, GrayVariation
I/O Contract
Inputs (RgbVariation)
| Name | Type | Required | Description |
|---|---|---|---|
| r | Variation or float | Yes | Red channel value or variation (0.0 to 1.0) |
| g | Variation or float | Yes | Green channel value or variation (0.0 to 1.0) |
| b | Variation or float | Yes | Blue channel value or variation (0.0 to 1.0) |
| alpha | Variation or float | No | Alpha channel value or variation (default 1.0) |
Inputs (HsvVariation)
| Name | Type | Required | Description |
|---|---|---|---|
| h | Variation or float | Yes | Hue value or variation (0.0 to 1.0) |
| s | Variation or float | Yes | Saturation value or variation (0.0 to 1.0) |
| v | Variation or float | Yes | Value (brightness) value or variation (0.0 to 1.0) |
| alpha | Variation or float | No | Alpha channel value or variation (default 1.0) |
Inputs (GrayVariation)
| Name | Type | Required | Description |
|---|---|---|---|
| gray_level | Variation or float | Yes | Gray level value or variation (0.0 to 1.0) |
| alpha | Variation or float | No | Alpha channel value or variation (default 1.0) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | numpy.ndarray |
RGBA array of shape (4,) with values in [0.0, 1.0] |
Usage Examples
from dm_control.composer.variation import colors
from dm_control.composer.variation import distributions
# Randomize hue while keeping saturation and brightness fixed
random_hue_color = colors.HsvVariation(
h=distributions.Uniform(0.0, 1.0),
s=0.8,
v=0.9,
alpha=1.0
)
# Randomize each RGB channel independently
random_rgb = colors.RgbVariation(
r=distributions.Uniform(0.0, 1.0),
g=distributions.Uniform(0.0, 1.0),
b=distributions.Uniform(0.0, 1.0)
)
# Create a random gray variation
random_gray = colors.GrayVariation(
gray_level=distributions.Uniform(0.2, 0.8)
)
# Evaluate the variation
import numpy as np
rng = np.random.RandomState(42)
rgba = random_hue_color(initial_value=None, current_value=None, random_state=rng)
# rgba is a numpy array of shape (4,), e.g. [0.9, 0.18, 0.18, 1.0]