Skip to content

Color Sensors

NextColorDistanceSensor combines a color sensor and an optional distance sensor into one class.

// By Lynx Module and I2C bus, recommended
val sensor = NextColorDistanceSensor(RobotController.controlHub, 0)
// Using your configuration name
val sensor = NextColorDistanceSensor("colorSensor")

Both constructors accept an optional hasDistance parameter:

Parameter Type Default Description
hasDistance Boolean false Whether to also create a distance sensor from the same sensor.

Reads the color sensor (and distance sensor, if present) and refreshes the cache. Call this once per loop, before reading anything. It’s recommended to put this in your periodic for your Mechanism..

sensor.update()

True if the distance sensor senses an object within threshold, in the given unit.

if (sensor.isWithinDistance(4.0)) { ... } //defaults to CM

True if the cached color reading matches the given ColorProfile.

if (sensor.isColor(green)) { ... }

isColorWithinDistance(profile, threshold, unit)

Section titled “isColorWithinDistance(profile, threshold, unit)”

True if the cached color reading matches profile and an object is within threshold, in the given unit.

if (sensor.isColorWithinDistance(green, 4.0)) { ... } //defaults to CM

Single-line telemetry string showing the current RGB, HSV, and distance readings. Useful for calibrating ColorProfiles.

telemetry.addLine(sensor.debug())

NextColor stores a color, either as RGB or HSV. HSV is recommended for most cases, since it’s more stable under different lighting conditions.

val lime = NextColor.hsv(100f, 0.9f, 0.85f)
val sameColor = NextColor.rgb(lime.red, lime.green, lime.blue)

ColorProfile describes a target color and the tolerances, to match your readings. These can be used for isColor() or isColorWithinDistance().

val green = ColorProfile(
space = ColorSpace.HSV,
color = NextColor.hsv(130f, 0.7f, 0.6f),
tolerance = NextColor.hsv(20f, 0.3f, 1f),
)
Parameter Type Description
space ColorSpace The color space to compare in: RGB or HSV.
color NextColor The target color to match against.
tolerance NextColor How far each channel can deviate from color and still count as a match.